如何将生成的位图加载到 PictureBox 中?
那里似乎有很多图片框问题,但我还没有发现任何涉及将图片框内容更改为不仅仅是从文件加载的位图的问题。
我的应用程序采用字节数组并从中生成位图。我真的想避免将写入文件作为中间处理步骤。
因为它是一个字节数组,而不是 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? 在将新位图加载到图片框之前,我是否需要更改图片框的其他参数?
There seem to be many picturebox questions out there, but I haven't found any that deal with changing the contents of a picturebox to a bitmap that was not simply loaded from file.
My application takes an array of bytes and generates a bitmap from them. I really want to avoid writing to a file as an intermediate processing step.
Because it is an array of bytes, and not 2-byte words, I needed to make an indexed bitmap with a grayscale palette.
I then converted the indexed bitmap to a normal one (24 bit rgb).
This is the code that is causing an error for me:
pictureBox1.Image = (System.Drawing.Image)bmp2;
When I view the form (the picturebox tries to draw), the thread will simply halt execution with a message:
"invalid parameter at System.Drawing.Image.get_RawFormat()"
What am I doing wrong? How can I create a safe bitmap for the picturebox?
This is what creates "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();
If I use bmp1.Save(@"testfile.bmp") I get a perfectly acceptable bitmap that appears to be without anomaly.
Why can't I use my bitmap as my picturebox.Image?
Are there additional parameters of the picturebox I need to change prior to loading the new bitmap into it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我发现我所做的事情有问题。
我会尝试解释哪里出了问题,但作为一个非专家,我不确定我的解释有多准确。
显然,在 pictureBox 中设置图像不会将内存复制到任何地方。后来在我的应用程序中(远离我展示的代码片段的几个函数)我处理了变量“bmp1”。
我不知道与 bmp1 关联的内存在函数传递它的所有地方都是相同的内存,并且在销毁它时引发了错误“System.Drawing.Image.get_RawFormat() 的无效参数”。我想它这样做是因为每次重绘 pictureBox 时,它都会使用它的“Image”属性来绘制。因为我正在处理与“Image”属性相关的内存,所以我扼杀了 picturebox_Paint 事件正常工作的所有希望。
我只希望现在不会因为从未丢弃我创建的任何位图而出现内存泄漏。
Well, I've found the problem with what I was doing.
I'll try to explain what was wrong, but as a non-guru I am not sure how accurate I will be.
Apparently setting the image in a pictureBox does not copy memory anywhere. Later on in my application (a few functions away from the code snippet I showed) I was disposing of the variable "bmp1".
I was not aware that the memory associated with bmp1 was the same memory everywhere the function passes it, and in destroying it the error "invalid parameter at System.Drawing.Image.get_RawFormat()" was raised. I suppose it does this because every time the pictureBox is redrawn it uses it's "Image" property to, well, draw. Because I was disposing of the memory associated with the "Image" property I was killing all hopes of the picturebox_Paint event working properly.
I only hope that I don't have memory leaks now as a result of never disposing of any bitmaps I create.