将矩阵颜色转换为BitmapImage



我正在尝试将矩阵颜色转换为BitmapImage
我有代码可以将BitmapImage转换为矩阵颜色,如下所述,但它不起作用:

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);
    }
}

我该怎么解决这个问题?

[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:将位图图像转换为位图,反之亦然

最新更新