Deepak yogi
Deepak yogi

Reputation: 117

How can I set background color when take transparency image from gallery

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.

enter image description here

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

enter image description here This is my take image 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.Medium
                   });
               }
           }
           else
           {
               return;
           }
           if (file == null)
               return;
            Stream s = file.GetStream();
           RestaurantImage.Source = ImageSource.FromStream(() =>
           {
               file.Dispose();
               return s;
           });
}

Upvotes: 1

Views: 554

Answers (2)

Deepak yogi
Deepak yogi

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

Leo Zhu
Leo Zhu

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

Related Questions