开发者

Load a bitmap to a PictureBox control

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

        objBitmap = new Bitmap(Reso开发者_高级运维urce1.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.


pictureBox2.Image = objBitmap;


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜