How to put image in a picture box from Bitmap

后端 未结 3 1485
攒了一身酷
攒了一身酷 2021-02-18 13:09

Is it possible to load a picture from memory (byte[] or stream or Bitmap) without saving it to disk?

This is the code I use to tur

3条回答
  •  后悔当初
    2021-02-18 14:10

    I had some code resembling the accepted answer that caused a memory leak. Problem is that when you set the picture box image to the bitmap, you're still referring to the bitmap, rather than creating a copy. If you need to set the image multiple times you need to make sure you're disposing all the old bitmaps.

    This is for anyone who's looking to clone a bitmap to an image box. Try this:

    if (pictureBox.Image != null) pictureBox.Image.Dispose();
    pictureBox.Image = myBitmap.Clone(
        new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), 
        System.Drawing.Imaging.PixelFormat.DontCare);
    

提交回复
热议问题