Forum Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to make the Graphics filters all available to my bitmap without using MemoryStream method?

SannaSSannaS Member

Hi, I got a Android.Graphics.Bitmap and I need to be able to apply some filters to it.

I got this:

private Android.Graphics.Bitmap GetBitmap(Android.Graphics.Drawable d)
{
/// some code regarding width and height
var bd = d as BitmapDrawable;
if (bool)
{
return Android.Graphics.Bitmap.CreateScaledBitmap(src, width, height, true);
}
else
{
Android.Graphics.Bitmap bitmap = Android.Graphics.Bitmap.CreateBitmap(width, height, Android.Graphics.Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap);
d.SetBounds(0, 0, width, height);
d.Draw(canvas);
return bitmap;
}
}

I found out that the System.Drawing.Graphics can do that, it has multiple filters in InterpolationMode, such as HighQualityBilinear and HighQualityBilinear.
Is it possible to apply those filters to my bitmap?

I would also accept any other method in must not be the one using a graphics object.

I know that the CreateScaledBitmap has the last parameter in the constructor who decides whis of the ONLY two filters may be applied to the bitmap while creating it, and those are Bilinear and Nearest-neighbor, but I need those filters in Graphics.

Also, for some reason, once I use a MemoryStream to compres my bitmap, and to make an Image out of it, using this method:

    public static Stream RaiseImage(Android.Graphics.Bitmap bitmap)
    {
        MemoryStream ms = new MemoryStream();
        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, ms);
        return ms;
    }

and

MemoryStream ms = new MemoryStream();
    src.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, ms);
    Stream stream = RaiseImage(src);
    Image image = System.Drawing.Image.FromStream(stream);    // <--------------error described bellow
    Graphics myGraphics = Graphics.FromImage(image);
    myGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
return Android.Graphics.Bitmap.CreateScaledBitmap(src, width, height, true);

I get this error: Operation is not supported on this platform.
It occurs on the Image image = System.Drawing... line (marked above)

Thank You for your time.
I hope You can help me.

Also, I have posted this question on (cannot post links, search:) stackoverflow 64099087/how-to-convert-android-graphics-bitmap-to-system-drawing-bitmap-and-back

Sign In or Register to comment.