How can I load a generated Bitmap into a PictureBox?

时光总嘲笑我的痴心妄想 提交于 2019-12-13 13:31:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2189384/how-can-i-load-a-generated-bitmap-into-a-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!