0

I got this code in my Xamarin app (API 28):

I tried to find a way to convert a bitmap to a graphic object (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics?view=dotnet-plat-ext-3.1), with the goal to use the InterpolationMode and to apply the filter, and then to revert the change, but I failed to find a single example on how to do it.

I found this: Convert graphics object to bitmap object But it is not what I am looking for, because, I got this code:

private Android.Graphics.Bitmap GetBitmap(Drawable d, System.Drawing.Size? bounds)
{
    ...some code regarding width and height, and then
    var bd = d as BitmapDrawable;
    if (bd != null)
    {
        Android.Graphics.Bitmap src = bd.Bitmap;
        if (width == src.Width && height == src.Height)
        {
            return src;
        }
        else
        {

            Graphics myGraphics = Graphics.FromImage(src);  // here is the problem
            // USE G TO SET InterpolationMode 
            // convert resulting G somehow back to Android.Graphics.Bitmap, and return it insted of the bottom line
            return Android.Graphics.Bitmap.CreateScaledBitmap(src, width, height, true);
        }
    }

}

Error code:

Cannot convert from Android.Graphics.Bitmap to System.Drawing.Bitmap

I need some suggestions on how to solve this, any help will be much appreciated.

Thank you for your time.

Elydasian
  • 2,016
  • 5
  • 23
  • 41

1 Answers1

1

You need to convert the src to Steram firstly . Check the following code

   public static Stream RaiseImage(Android.Graphics.Bitmap bitmap)
    {
    
        MemoryStream ms = new MemoryStream();
        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, ms);
        return ms;
    }
    private Android.Graphics.Bitmap GetBitmap(Drawable d, System.Drawing.Size? bounds)
    {
       // ...some code regarding width and height, and then
        var bd = d as BitmapDrawable;
        Android.Graphics.Bitmap bitmap = bd.Bitmap;
        Stream src=  RaiseImage(bitmap);
        Image image = System.Drawing.Image.FromStream(src);
        Graphics myGraphics = Graphics.FromImage(image);  // here is the problem
                                                        // USE G TO SET InterpolationMode 
                                                        // convert resulting G somehow back to Android.Graphics.Bitmap, and return it insted of the bottom line
        return Android.Graphics.Bitmap.CreateScaledBitmap(src, width, height, true);
       



    }
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • thank you for your time, I applied this solution to my project, and I got this error: Operation is not supported on this platform. It's from this line: Image image = System.Drawing.Image.FromStream(src); Also, change the src parameter in the return statement from src to bitmap. – Elydasian Sep 29 '20 at 12:10
  • I belive this would work if I had access to the images, but that I have not, I get them I belive from the package manager. So technically, this answer is correct, but not in my case, have an upvote anyway :) – Elydasian Sep 29 '20 at 12:31
  • Remove the `?` from System.Drawing.Size? bounds .\ – Lucas Zhang Sep 29 '20 at 12:52
  • It behaves the same, with and without it. – Elydasian Sep 29 '20 at 14:02
  • It works fine on my side .What platform did you work , xamarin.forms for Android or Xamarin.Android ? – Lucas Zhang Sep 30 '20 at 06:27
  • I belive it is Xamarin.Android, it is installed in the NuGet Packages – Elydasian Sep 30 '20 at 06:53
  • It works fine on my side . And I could found the cause from the code that you post as it is imcomplete . – Lucas Zhang Sep 30 '20 at 08:39
  • I belive you :), I removed the whole System.Drawing.Size? bounds from the function, and the problem remained. Anyway, thank you for your time. – Elydasian Sep 30 '20 at 08:52
  • @LucasZhang-MSFT, the variables width and height are not defined in the second function, what do they stand for? – Son of Man Dec 02 '21 at 05:50