Load a bitmap to a PictureBox control

蓝咒 提交于 2020-01-05 04:02:28

问题


This is not working for some reason. I'm not sure why:

        objBitmap = new Bitmap(Resource1.im);

        Stream stream;
        objBitmap.Save(stream, ImageFormat.Bmp);

        this.pictureBox2.Image = Image.FromStream(stream);

        objBitmap.Dispose();

Basically, I need to show an image in a PictureBox control and I am not sure how to do that.


回答1:


pictureBox2.Image = objBitmap;



回答2:


Well, it ought to go kaboom on the Save() method, the stream was never initialized. Not sure what the point of doing this was. There might be one but it isn't visible from your code. The normal version is:

if (this.pictureBox2.Image != null) this.pictureBox2.Dispose();
this.pictureBox2.Image = Properties.Resources.im;

With some question marks about what Resource1 might be. You get my version going by using Project + Properties, Resource tab and click the arrow on the Add Resource button, Add Existing File.




回答3:


You can change that to

pictureBox2.Image = Resource1.im;

To answer your question, you need to put a stream (probably a MemoryStream) in the stream variable.
You'll also need to "rewind" the stream before reading it back into a Bitmap. (stream.Position = 0)



来源:https://stackoverflow.com/questions/4120202/load-a-bitmap-to-a-picturebox-control

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