bharathi
bharathi

Reputation: 6271

C# how to get a bitmap from a picturebox

I have a image in picturebox. I want to get that image as a Bitmap.

My one line code is:

Bitmap default_image = (Bitmap)pictureBox5.Image.Clone();

But what i am getting is:

default_image value=null;

Can anyone help me.

Upvotes: 10

Views: 37744

Answers (3)

adrianwadey
adrianwadey

Reputation: 1759

If you got the image into the PictureBox by using imageLocation

pbSourceImage.ImageLocation = openFile.FileName;

then PictureBox.Image will be null.

Instead, load the picture using

pbSourceImage.Image = Image.FromFile(openFile.FileName);

Then you will be able to clone from the Image property.

Upvotes: 2

IR_IR
IR_IR

Reputation: 184

This is because you do not have image, probably you have BackgroundImage. You need to have Image properties fill with your picture.

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55334

Bitmap default_image = new Bitmap(pictureBox5.Image);

You are never instantiating a Bitmap which is why it is null.

Upvotes: 19

Related Questions