调用UnmanagedImage.FromManagedImage(位图位图)时源图像的像素格式不受支持

时间:2015-11-11 07:34:20

标签: c# android bitmap xamarin aforge

我正在创建一个使用Portable AForge Imaging Library的应用。我有一个Android.Graphics.Bitmap图像,它被转换为System.Drawing.Bitmap。

System.Drawing.Bitmap photo = (System.Drawing.Bitmap)androidPhoto;

然后我尝试像这样创建UnmanagedImage BitmapData:

UnmanagedImage data = UnmanagedImage.FromManagedImage(photo);

抛出AForge.Imaging.UnsupportedImageFormatException:源图像不支持的像素格式。

我对导致这个问题的原因进行了广泛的研究,我发现我需要一个PixelFormat为24bpp或8bpp的Bitmap,但我的图像是16bpp。 我寻找转换为24bpp或8bpp的方法,我试过

var photo8bpp = photo.Clone (System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

var photo24bpp = photo.Clone (System.Drawing.Imaging.PixelFormat.Format24bppRgb);

我甚至尝试过:

AForge.Imaging.Image.FormatImage (ref photo);

我的最后一次尝试是这种转换方法:

private System.Drawing.Bitmap Convert(System.Drawing.Bitmap oldbmp) 
{
    using (var ms = new MemoryStream()) 
    {
        oldbmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        ms.Position = 0;
        return (System.Drawing.Bitmap)(System.Drawing.Image.FromStream(ms));
    }
}

我尝试的一切都导致了这个:

  

System.AggregateException:发生了一个或多个错误。 --->   System.NullReferenceException:未将对象引用设置为实例   (at包装器未知)   ImagePixelEnumerator.Helpers.Pixels.NonIndexed.PixelDataRgb565:累得PtrToStructure   (intptr,object)at at(包装器托管到原生)   System.Runtime.InteropServices.Marshal:累得PtrToStructure   (intptr,System.Type)at   ImagePixelEnumerator.Helpers.Pixel.ReadRawData(IntPtr imagePointer)   [0x00000]在d:\ Users \ Anders \ Documents \ Visual Studio中   2013 \项目\ aforge \源头\ System.Drawing中\助手\ Pixel.cs:272

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我想我修好了它:

var data = new UnmanagedImage(androidPhoto.LockPixels(), androidPhoto.Width,
            androidPhoto.Height, androidPhoto.Width,  
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);