I am trying to convert a WriteableBitmap which is having Rgb24 as a pixelFormat. I want to store the same image into a EmguCV Image having Bgr format. I have written a following code but it is not giving appropriate results.
public unsafe void Convert(WriteableBitmap bitmap)
{
byte[] retVal = new byte[bitmap.PixelWidth * bitmap.PixelHeight * 4];
bitmap.CopyPixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), retVal, bitmap.PixelWidth * 4, 0);
Bitmap b = new Bitmap(bitmap.PixelWidth, bitmap.PixelHeight);
int k = 0;
byte red, green, blue, alpha;
for (int i = 0; i < bitmap.PixelWidth; i++)
{
for (int j = 0; j < bitmap.PixelHeight && k<retVal.Length; j++)
{
alpha = retVal[k++];
blue = retVal[k++];
green = retVal[k++];
red = retVal[k++];
System.Drawing.Color c = new System.Drawing.Color();
c = System.Drawing.Color.FromArgb(alpha, red, green, blue);
b.SetPixel(i, j, c);
}
}
currentFrame = new Image<Bgr, byte>(b);
currentFrame.Save("Converted.jpg");
}
Thanks in advance.