Click here to Skip to main content
15,895,779 members
Please Sign up or sign in to vote.
4.60/5 (5 votes)
See more:
I'm working on a project where I find JPG files using a folder dialog and list the images in a list.

Now, I've extended the program so the user can just drag and drop files in the list. But I want the images to be JPG.

I check for image size, width and height, and other as shown below....

C#
public bool IsValid(string FileName)
       {
           Photo JPGFile = new Photo(FileName);

           //Check if the file exists.
           if (!System.IO.File.Exists(JPGFile.FullPath)) { return false; }

           //Check if the File is a JPG File.
           if (System.IO.Path.GetExtension(FileName).ToLower() != ".jpg")
           {
               //<-------------------------------Need a better validation------------>
               return false;
           }

           //Check if the FilePath Matches any other file in the List.
           foreach (Photo Picture in Photos)
           {
               if (FileName == Picture.FullPath)
               {
                   return false;
               }
               else
               {
                   continue;
               }
           }

           //Check if the File Size is greater than 450kb.

           if (JPGFile.GetRawSize() > 450)
           {
               return false;
           }

           //Check if the Height or width of the image is greater than that of the screen size.

           if (JPGFile.ImageWidth > SystemParameters.VirtualScreenWidth || JPGFile.ImageHeight > SystemParameters.VirtualScreenHeight)
           {
               return false;
           }

           return true;
       }


When I was testing, I just renamed a desktop.ini file to Desktop.jpg and it passed validation. Can anyone help please on how I can really check if the image is a VALID jpg file.

Thanks.
Posted

The easiest way is to open it as an image and check it's a JPG type when it's open. However, if it isn't then you will get an exception which may not always be catchable.

A safer way is to open the file as a binary stream and check the start and end: JPG files start with the two bytes 0xFF and 0xD8, and end with the two bytes 0xFF and 0xD9. There are also some specific ASCII strings in JPG files: http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Magic_numbers_in_files[^]
If these are present, it probably is a JPG. But the only true way to be absolutely sure is to load it as an image!
 
Share this answer
 
try this:

public  bool IsJpegImage(string filename)
{
	try
	{
		System.Drawing.Image img = System.Drawing.Image.FromFile(filename);

					   
		return img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg);
	}
	catch (OutOfMemoryException)
	{
		return false;
	}
} 
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 16-Jul-12 5:13am    
Exact one +5!
Manas Bhardwaj 16-Jul-12 5:54am    
thx!
Akinmade Bond 15-Oct-12 12:11pm    
+5. Do you know any WPF approach to do the exact same thing?

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