如何将ImageSource转换为用于跨平台项目的位图(Xamarin)

时间:2019-02-19 11:00:59

标签: android xamarin bitmap converters imagesource

我找到了一个示例,但是此example 使用System.Windows类的“ Int32Rect”,我们无法将此库用于android或ios

对于位图,我使用System.Drawing.Common

public static System.Drawing.Bitmap BitmapSourceToBitmap2(BitmapSource srs)
        {
            int width = srs.PixelWidth;
            int height = srs.PixelHeight;
            int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);
            IntPtr ptr = IntPtr.Zero;
            try
            {
                ptr = Marshal.AllocHGlobal(height * stride);
                srs.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride);
                using (var btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr))
                {
                    // Clone the bitmap so that we can dispose it and
                    // release the unmanaged memory at ptr
                    return new System.Drawing.Bitmap(btm);
                }
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                    Marshal.FreeHGlobal(ptr);
            }
        }

那么,如何将ImageSource转换为用于跨平台项目的Bitmap?

我尝试关注guide,但其中包含的信息不完整,我试图自行填写低估的内容

该项目甚至可以编译!但是,当我尝试从GetBitmapFromImageSourceAsync(…)方法获取结果时,应用程序冻结(无限循环)

var test = GetBitmapFromImageSourceAsync(image, Android.App.Application.Context);
var bitmap = test.Result;

请查看为什么它不起作用?或者,请给我一个指向您项目的链接,我会解决的

https://github.com/StriBog45/XamarinImage

0 个答案:

没有答案