2016-01-03 94 views
0

我想將基色轉換爲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); 
    } 
} 

我該如何解決這個問題?

+0

如果你只是想轉換System.Drawing.Bitmap到的BitmapSource,也有不少的StackOverflow上,例如問題和答案[本](http://stackoverflow.com/q/30727343/1136211)。 – Clemens

回答

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

組織:Converting BitmapImage to Bitmap and vice versa