9

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.

Chiragkumar Thakar
  • 3,616
  • 5
  • 37
  • 49
bharathi
  • 6,019
  • 23
  • 90
  • 152

3 Answers3

18
Bitmap default_image = new Bitmap(pictureBox5.Image);

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

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • If he's getting null out of Image.Clone(), then calling the constructor with the same image property isn't going to do very much either. – MGZero Aug 09 '11 at 13:44
  • 2
    @MGZero: It is my understanding that it is null because you cannot cast a Image object to a Bitmap. – Evan Mulawski Aug 09 '11 at 13:45
  • Bitmap is inherited from Image..sooo...yes, you are correct. +1 now that I'm certain :) – MGZero Aug 09 '11 at 13:48
2

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.

adrianwadey
  • 1,719
  • 2
  • 11
  • 17
0

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

IR_IR
  • 184
  • 1
  • 1
  • 11