将matric颜色转换为BitmapImage

时间:2016-01-03 17:35:18

标签: wpf image bitmap

我正在尝试将基质颜色转换为BitmapImage 我有代码将BitmapImage转换为matric颜色,如下所示,但它不起作用:

Bitmap b = new Bitmap(@"c:\aaa.jpg");
System.Drawing.Color[,] mat = new System.Drawing.Color[(int)b.Width,  (int)b.Height];
for (int i = 0; i < b.Width; i++) {
    for (int j = 0; j < b.Height; j++){
        mat[i, j] = b.GetPixel(i, j);
    }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

private BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
    IntPtr hBitmap = bitmap.GetHbitmap();
    BitmapImage retval;

    try
    {
        retval = Imaging.CreateBitmapSourceFromHBitmap(
                     hBitmap,
                     IntPtr.Zero,
                     Int32Rect.Empty,
                     BitmapSizeOptions.FromEmptyOptions());
    }
    finally
    {
        DeleteObject(hBitmap);
    }

    return retval;
}

org:Converting BitmapImage to Bitmap and vice versa