我正在尝试将基质颜色转换为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);
}
}
我该如何解决这个问题?
答案 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;
}