0

I am trying to convert matric color to BitmapImage.
I have code that converts BitmapImage to matric color as written below but it does not work:

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

How can I fix this?

Idos
  • 15,053
  • 14
  • 60
  • 75
  • In case you just want to convert a System.Drawing.Bitmap to BitmapSource, there are a lot of questions and answers on StackOverflow, e.g. [this](http://stackoverflow.com/q/30727343/1136211). – Clemens Jan 03 '16 at 19:42

1 Answers1

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

Community
  • 1
  • 1