I create the object myImage using the sentence below.
Image myImage=Image.FromFile(fileName);
I wonder the picture type of myImage, such as GIF, BMP, JPG, etc.
What do I do
I create the object myImage using the sentence below.
Image myImage=Image.FromFile(fileName);
I wonder the picture type of myImage, such as GIF, BMP, JPG, etc.
What do I do
How can I know the picture type from Image? thanks!
MWade_MS
mcmcom has the right idea.
In developing my Outlook Express picture sorting program (it ignores email and treats the .dbx files as images databases) of necessity I analyzed a lot of email and images. My list is perhaps twice as long mcmcom's and the code a bit more involved. It is particularly ugly when someone posts a Mac image because of the peculiarities of Mac file handling. (Applie got it right, but let's not go there. "Bill Gates won", is my motto.)
I haven't looked lately but IIRC there were a couple occasionally observered file signatures that I never figured out.
For more information on this consult http://www.3bears.biz and while you are at it read the story of how my company got its name. It should give you a yuck.
Incidently, the Vista version of my program for Windows Mail is now under development. It will be sooooo much simpler to write thanks to .net and the changes for Windows Mail. It's unfortunate that .net won't handle a few of the rarer image types. But they are so rare that I think I'm going to drop support of them.
Regards,
Al Christoph
Three Bears Software, LLC
just right software @ just right prices @ 3bears.biz
Patrick Grimme
string
strExtension = filename.Substring(filename.Length - 3);Once you have the fileextension, you know the type of image and can use it
Regards,
Vikram
Jaimi
Stem_penguin
you can look into the mime type by examining the MimeType property of the Image object (in .net) but if they change the file extension then guess what, so does the mime type (or .net's interpretation of it)
i use this to make sure the file is indeed an image file. (but this also has some problems, mostly with the creation of certain images in certain programs, they do not always have the appropriate bytes in the right order)
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
//do error checking
string myStr = System.Text.Encoding.ASCII.GetString(myData);
//check if its definately an image.
if(myStr.Substring(6,4)!="JFIF")
{
//its not a jpeg
if(myStr.Substring(1,3) != "BPS")
{
//its not a psd
if(myStr.Substring(0,3) != "GIF")
{
//its not a gif
if(myStr.Substring(0,2) != "BM")
{
//its not a .bmp
if(myStr.Substring(1,3) != "PNG")
{
//its not a png
if(myStr.Substring(0,2) != "II")
{
if(this.myFile.PostedFile.ContentType.Substring(0,5) != "image")
{
ProcessErrors("notImage");
myFile.InputStream.Close();
return;
}
}
}
}
}
Yavuz Bogazci
System.Drawing.Bitmap img = new System.Drawing.Bitmap(@"C:\mypic.JPG");
if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid)
Console.WriteLine("Bitmap");
else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Gif.Guid)
Console.WriteLine("Gif");
else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Icon.Guid)
Console.WriteLine("Icon");
else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)
Console.WriteLine("Jpeg");
else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Png.Guid)
Console.WriteLine("Png");
else if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Tiff.Guid)
Console.WriteLine("Tiff");
Joe Rappo
Vikram is making a huge, and in the wonderful world of passing around Windows files, often inappropriate assumption! The extension tells you what someone THINKS the file format is, not what it actually is.
WRONG!
All too many folk think that by changing the extension of a file using F2 in Explorer that they change the format of the file. Want a JPG instead of a BMP, just change the extension. (Even describing this you have to be careful. By change extension I mean what you can do with the F2 key, NOT what you can do with File Save As commands in image processing programs.)
The extension of any windows file is worse than useless when it comes to determining the underlying format of the file.
Perhaps the single biggest mistake on day one when DOS was created was not providing a signature block at the beginning (or end) of a file that would uniquely determine the structure of the file for all ages to come. (The second biggest mistake IMHO was not implementing main frame like code protection schems early in the 80x86 life cycle. Lot's of malware grief might have been prevented.)
Regards,
Al