我如何可以加载生成位图到一个PictureBox? [英] How can I load a generated Bitmap into a PictureBox?
问题描述
似乎有很多图片框的问题在那里,但我没有发现任何与更改图片框的内容,这不是简单地从文件加载的位图处理。
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.
由于它是一个字节数组,而不是2字节字,我需要作出的索引的位图与灰度调色板
Because it is an array of bytes, and not 2-byte words, I needed to make an indexed bitmap with a grayscale palette.
然后我转换的位图索引到一个正常的(24位RGB)。
I then converted the indexed bitmap to a normal one (24 bit rgb).
这是code,是造成错误对我来说:
This is the code that is causing an error for me:
pictureBox1.Image = (System.Drawing.Image)bmp2;
当我查看窗体(PictureBox的尝试画出),该线程将简单地停止执行一条消息:
在System.Drawing.Image.get_RawFormat()无效的参数
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()"
我是什么做错了吗?如何创建为PictureBox的一个安全的位图?
What am I doing wrong? How can I create a safe bitmap for the picturebox?
这是什么创造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),我得到的似乎是无异常的一个完全可以接受的位图。
If I use bmp1.Save(@"testfile.bmp") I get a perfectly acceptable bitmap that appears to be without anomaly.
为什么我不能用我的位图作为我的picturebox.Image?
是否有该图片的其他参数,我需要改变加载新的位图到它之前?
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?
推荐答案
好吧,我发现与我在做什么的问题。
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.
显然设置在PictureBox图像没有任何地方复制的记忆。后来在我的应用程序(的一些功能远离code段我发现)我处理变量BMP1的。
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".
我不知道,与BMP1相关的记忆是相同的内存处处功能传递给它,并摧毁它的错误在System.Drawing.Image.get_RawFormat无效参数()长大。我想它确实是因为每次PictureBox的时间重绘它使用它的图像属性,好了,画。因为我是用废弃的图像属性,我杀picturebox_Paint事件正常工作的一切希望相关联的内存。
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.
这篇关于我如何可以加载生成位图到一个PictureBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!