从图库中拍摄图像时,我想设置图像的背景颜色,因为如果我拍摄透明图像,它将自动在图像后面设置黑色背景。
但是,如果我将透明图像保留在Resources-> drawable文件夹中,则会显示 给出红色背景
<Grid Grid.Column="1" BackgroundColor=">
<Image x:Name="RestaurantImage" Source="trans.png" BackgroundColor="Red"/>
</Grid
private async void ImageTapped(object sender, EventArgs e)
{
string action = await UserDialogs.Instance.ActionSheetAsync("PickPhoto", "Cancel", null, null, "Take Photo", "Pick From Gallery");
MediaFile file = null;
if (action == "Take Photo")
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
UserDialogs.Instance.Alert("No Camera", ":( No camera avaialble.", "OK");
return;
}
file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
Directory = "Sample",
Name = "test.png"
});
}
else if (action == "Pick From Gallery")
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
UserDialogs.Instance.Alert("PhotosNotSupported", "PermissionNotGrantedToPhotos.", "OK");
return;
}
else
{
file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
{
PhotoSize = PhotoSize.Medium
});
}
}
else
{
return;
}
if (file == null)
return;
Stream s = file.GetStream();
RestaurantImage.Source = ImageSource.FromStream(() =>
{
file.Dispose();
return s;
});
}
答案 0 :(得分:1)
@saamer和@leo感谢您的帮助:)
我解决了我的问题“从图库中获取透明图像时设置背景颜色” ,当我使用 PhotoSize = PhotoSize.Medium 时,它在透明图像后面设置了黑色背景,但是如果我使用PhotoSize = PhotoSize.Full,它将透明背景设置在透明图像后面。
这是我从图库代码获取的图片:
private async void ImageTapped(object sender, EventArgs e)
{
string action = await UserDialogs.Instance.ActionSheetAsync("PickPhoto", "Cancel", null, null, "Take Photo", "Pick From Gallery");
MediaFile file = null;
if (action == "Take Photo")
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
UserDialogs.Instance.Alert("No Camera", ":( No camera avaialble.", "OK");
return;
}
file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
Directory = "Sample",
Name = "test.png"
});
}
else if (action == "Pick From Gallery")
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
UserDialogs.Instance.Alert("PhotosNotSupported", "PermissionNotGrantedToPhotos.", "OK");
return;
}
else
{
file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
{
PhotoSize = PhotoSize.Full
});
}
}
else
{
return;
}
if (file == null)
return;
Stream s = file.GetStream();
s.Position = 0;
RestaurantImage.Source = ImageSource.FromStream(() =>
{
file.Dispose();
return s;
});
}
答案 1 :(得分:0)
您可以查看效果是否是您想要的
1。创建一个 CustomImage.cs :
public class CustomImage:Image
{
}
2。在Droid项目中创建 CustomImageRenderer :
[assembly: ExportRenderer(typeof(CustomImage), typeof(CustomImageRenderer))]
namespace App18.Droid
{
class CustomImageRenderer:ImageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (Control != null)
{
ImageView image = Control as ImageView;
image.SetColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.DstOver);
}
}
}
}
最终使用 CustomImage 加载图像