Reputation: 117
I want to set the background color of an image when take an image from the gallery because if i take transparency image it set automatically black background behind image.
But if i keep transparency image in Resources-->drawable folder then it show given red background
<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;
});
}
Upvotes: 1
Views: 554
Reputation: 117
@saamer and @leo thanks for help me :)
I resolved my issue "set background color when take transparency image from gallery", when i used PhotoSize = PhotoSize.Medium then it's set black background behind transparent image but if i used PhotoSize = PhotoSize.Full it's set transparency background behind transparent image.
this is my get image from gallery code :
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;
});
}
Upvotes: 1
Reputation: 15001
You can see if the effect is what you want
1.create a CustomImage.cs :
public class CustomImage:Image
{
}
2.create a CustomImageRenderer in Droid project:
[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);
}
}
}
}
finally use CustomImage load image
Upvotes: 1