我正在尝试转换具有 Rgb24 作为 pixelFormat 的 WriteableBitmap。我想将相同的图像存储到具有 Bgr 格式的 EmguCV 图像中。我编写了以下代码,但没有给出适当的结果。
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");
}
提前致谢。