Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to compare height and width of two images i am geeting height and width of first images as follow :
C#
public int imageWidth()
        {




            {

                int w=0;
                w = pictureBox1.Image.Width;
                return w;

            }
        }
        public int imageheight()
        {
            int h=0;
           h = pictureBox1.Image.Height;
            return h;

        }

and in secode class i am using.
CreateSheet cs = new CreateSheet();
if (bmp.Width != cs.imageWidth() && bmp.Height != cs.imageheight())
{
MessageBox.Show("width and height of both images are not equal");
ImageResize(bmp,cs.imageWidth(),cs.imageheight());

}
but geeting exception
Object reference not set to an instance of an object. at line :
C#
w = pictureBox1.Image.Width; how it can be solved??

             return w;
Posted

What about no Image loaded in the PictureBox? I.e. pictureBox1.Image is null, and consequently you cannot acces its width property.
By the way, you do not need to load an image into a PictureBox to get its size. The Image and Bitmap classes have these properties too.
 
Share this answer
 
Comments
irfanniazi 23-Apr-13 2:34am    
so i should have pictureBox1.Image!=null before that line??
Nilesh Rokade 26-Apr-13 9:14am    
Still have problem ?
Yes check Null before using also in order to avoid exception you can also put not null check on image you are assigning to Picture Box

public int imageWidth()
{
{

int w=0;
if(pictureBox1&&pictureBox1.Image)
w = pictureBox1.Image.Width;
return w;

}
}
public int imageheight()
{
int h=0;
if(pictureBox1&&pictureBox1.Image)
h = pictureBox1.Image.Height;
return h;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900