colinodowd
会员
- 已加入
- 2020年1月23日
- 留言内容
- 12
- 编程经验
- 1-3
将BagerBG8字节数组转换为RGB,然后将RGB图像填充到pictureBox时,我遇到很多麻烦。我相信下面的代码可以解决向RGB的转换,但是我无法使新的字节数组适合pictureBox。
C#:
int src_width = 3840;
int src_height = 2748;
int dst_width = src_width / 2;
int dst_height = src_height / 2;
int col = 0;
int a = 0;
byte[] destination = new byte[dst_width * dst_height * 3];
int pos = 0;
for (int row = 0; row <= dst_width; row += 2)
{
for (col = 0; col < dst_height; col += 2)
{
a = row * src_width + col;
//blue from this line
destination[pos++] = buffer[a];
//green average of two greens from this line and next line:
destination[pos++] = (byte) ((buffer[a + 1] + buffer[a + dst_width]) / 2);
//red from the next line
destination[pos++] = buffer[a + dst_width + 1];
}
}
Bitmap bitmap = bitmap = new Bitmap(1920, 1374, PixelFormat.Format24bppRgb);
BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(test, 0, pNative, test.Length);
bitmap.UnlockBits(bmData);
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
这导致仅1/3的pictureBox被填充。有人知道我在做什么错吗?
由主持人最后编辑: