C# BitmapSource - 结果垂直翻转
C# BitmapSource - result is vertically flipped
我正在用 C#/WPF 实现一个软件,该软件正在从相机记录图像。
结果是一个位图,我正在将其复制到 byte[].
pixelFormat = System.Windows.Media.PixelFormats.Bgr24;
int size = buffer.FrameType.BufferSize;
byte[] img = new byte[size];
Marshal.Copy(buffer.GetImageDataPtr(), img, 0, size);
我正在使用这个 byte[] 创建一个 BitmapSource,但我得到的结果是垂直翻转的。
int stride = width * (pixelFormat.BitsPerPixel / 8);
image = BitmapSource.Create(width,
height,
96,
96,
pixelFormat,
BitmapPalettes.Gray256Transparent,
img,
stride);
对我来说可能有问题的一点是 PixelFormat。相机使用 system.drawing.imaging.pixelformat 而我使用 System.Windows.Media.pixelformat。
相机使用的是 RGB24,但文档说是 "uses BGR order for the RGB24 pixel format. The organization of the pixels in the image buffer is from left to right and bottom up. "
可能是什么问题?我是否使用了错误的像素格式?
Peter Duniho 和 Clemens 是对的。
这是解决方案:
int size = buffer.FrameType.BufferSize;
int height = buffer.FrameType.Height;
byte[] img = new byte[size];
int lineSize = buffer.BytesPerLine;
//for each row of the image
for (int row = 0; row < height; row++)
{
//For each byte on a row
for (int col = 0; col < lineSize; col++)
{
int newIndex = (size - (lineSize * (row + 1))) + col;
img[newIndex] = buffer[col, row];
}
}
我正在用 C#/WPF 实现一个软件,该软件正在从相机记录图像。 结果是一个位图,我正在将其复制到 byte[].
pixelFormat = System.Windows.Media.PixelFormats.Bgr24;
int size = buffer.FrameType.BufferSize;
byte[] img = new byte[size];
Marshal.Copy(buffer.GetImageDataPtr(), img, 0, size);
我正在使用这个 byte[] 创建一个 BitmapSource,但我得到的结果是垂直翻转的。
int stride = width * (pixelFormat.BitsPerPixel / 8);
image = BitmapSource.Create(width,
height,
96,
96,
pixelFormat,
BitmapPalettes.Gray256Transparent,
img,
stride);
对我来说可能有问题的一点是 PixelFormat。相机使用 system.drawing.imaging.pixelformat 而我使用 System.Windows.Media.pixelformat。 相机使用的是 RGB24,但文档说是 "uses BGR order for the RGB24 pixel format. The organization of the pixels in the image buffer is from left to right and bottom up. "
可能是什么问题?我是否使用了错误的像素格式?
Peter Duniho 和 Clemens 是对的。 这是解决方案:
int size = buffer.FrameType.BufferSize;
int height = buffer.FrameType.Height;
byte[] img = new byte[size];
int lineSize = buffer.BytesPerLine;
//for each row of the image
for (int row = 0; row < height; row++)
{
//For each byte on a row
for (int col = 0; col < lineSize; col++)
{
int newIndex = (size - (lineSize * (row + 1))) + col;
img[newIndex] = buffer[col, row];
}
}