那里似乎有很多图片框问题,但我还没有找到任何处理将图片框的内容更改为不仅仅是从文件加载的位图的问题。
我的应用程序需要一个字节数组并从中生成一个位图。我真的想避免将文件写入作为中间处理步骤。
因为它是字节数组,而不是 2 字节字,所以我需要使用灰度调色板制作索引位图。
然后我将索引位图转换为普通位图(24 位 rgb)。
这是导致我出错的代码:
pictureBox1.Image = (System.Drawing.Image)bmp2;
当我查看表单(图片框尝试绘制)时,线程将简单地停止执行并显示一条消息:“System.Drawing.Image.get_RawFormat() 的参数无效”
我究竟做错了什么?如何为图片框创建安全位图?
这就是创建“bmp2”的原因:
//creating the bitmap from the array
System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(100, 100, 100, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, MyIntPtr);
//creating a proper indexed palette
System.Drawing.Imaging.ColorPalette GrayPalette = bmp1.Palette;
for (int i = 0; i < GrayPalette.Entries.Length; i++)
{
GrayPalette.Entries[i] = Color.FromArgb(i, i, i);
}
bmp1.Palette = GrayPalette;
//creating a non-indexed, 24bppRGB bitmap for picturebox compatibility
System.Drawing.Bitmap bmp2 = new Bitmap(bmp1.Width, bmp1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics gr = Graphics.FromImage(bmp2);
gr.DrawImage(bmp1, 0, 0);
gr.Dispose();
如果我使用 bmp1.Save(@"testfile.bmp") 我得到一个完全可以接受的位图,看起来没有异常。
为什么我不能使用我的位图作为我的picturebox.Image?在将新位图加载到其中之前,我需要更改图片框的其他参数吗?