C# how to get a bitmap from a picturebox
Asked Answered
T

3

10

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.

Tympany answered 9/8, 2011 at 13:35 Comment(0)
B
19
Bitmap default_image = new Bitmap(pictureBox5.Image);

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

Barny answered 9/8, 2011 at 13:39 Comment(3)
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.Irrefragable
@MGZero: It is my understanding that it is null because you cannot cast a Image object to a Bitmap.Barny
Bitmap is inherited from Image..sooo...yes, you are correct. +1 now that I'm certain :)Irrefragable
C
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.

Clisthenes answered 6/6, 2016 at 11:20 Comment(0)
F
0

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

Fur answered 1/3, 2016 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.