how to get the size and type of images?

hi

i have a windows form . users can send their images , from that.
but i want to chek the size and type of that files to be in appropriate formats.
for ex. they should be able to send only .jpg files , with a defined sizes, and not else.
how can i do that

thank u


Answer this question

how to get the size and type of images?

  • MSDevUser

    You can load the image using the Image class.  On this class are properties to tell you the format and size of the picture.  You might consider making an enhancement to automatically convert images to the correct size and format.  You can do this through the same Image class. 

    Size - Size of image in pixels.
    RawFormat - Will be ImageFormat.Jpeg for JPEG.

    Image doesn't work with all image files so you'll want to wrap it in an exception handler.  You also need to make sure you dispose of the Image object otherwise you'll leak memory and the file will remain locked.

    Michael Taylor - 10/24/05

  • amber1

    thank u for your reply

    would u please give me some refrences
    i don know how to use Image class

  • slackr007

    hi michael

    i wrote in vb and i wanted to convert your example to vb.
    but i don't know how to convert this :

     using (Image img = new Image(imageFile))
     
    i tried :
     imports(dim img as new image(imagefile))

    but it's not true

  • Marco Ferrero

    Your best reference is the MSDN library.  It contains example code for this class along with most of the others.  If you want to do even more with this class then you should get a good book like Programming Windows Forms with C# by Charles Petzold.  It contains an entire chapter on the topic and is a really good reference.  As for your direct question here is some sample code that might help:

    using System.Drawing;

    public bool IsValidFormat ( string imageFile )
    {
       using (Image img = new Image(imageFile))
       {
          //Validate resolution (ensure it is 1024x768)
          if ((img.Width != 1024) || (img.Height != 768))
             return false;

          //Validate format (ensure it is a jpeg)
          if (img.RawFormat != ImageFormat.Jpeg)
             return false;

          //Must be valid
          return true;
       };
    }

    This function verifies the file is a jpeg of 1024x768 size.  Of course I would recommend that if the file isn't a proper format that you simply modify it to be valid and then send it.  You could prompt the user first.  Of course your requirements might not be this flexible but users may get annoyed if they have to open the image in Paint just to resize and resave it.

    Michael Taylor - 10/30/05

  • Guardian-ND

    thank u for yor reply

    would u please give me some references
    i don know how to use Image class


  • how to get the size and type of images?