Convert an image in a PictureBox to a bitmap

I used the following code to convert an image in a PictureBox to a bitmap:

bmp = (Bitmap)pictureBox2.Image; 

But I get the result as bmp = null . Can someone tell me how I do this?

+7
source share
3 answers

According to my understanding, you did not assign the PictureBox Image property, so it returns null for the cast type.

The PictureBox property automatically converts the image format, and if you see a tooltip for the Image property, it displays System.Drawing.Bitmap. Check your image property correctly assigned.

Check it out, it works on my side.

 private void button1_Click(object sender, EventArgs e) { Bitmap bmp = (Bitmap)pictureBox1.Image; } private void TestForm12_Load(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("c:\\url.gif"); } 

/// Using the BitMap class

  Bitmap bmp = new Bitmap(pictureBox2.Image); 

You can directly use pictureBox2.Image for Bitmap, how you do it, and also use the Bitmap class to convert to an object of the Bitmap class.

Link: Raster constructor (image) .

Here you can find more options with a raster class.

+5
source
 Bitmap bitmap = new Bitmap(pictureBox2.Image) 

http://msdn.microsoft.com/en-us/library/ts25csc8.aspx

+7
source

I think you are looking for this:

 Bitmap bmp = new Bitmap(pictureBox2.Image) 
+2
source

All Articles