I'm writing a program to convert one image type (for example, png or gif, or tif) to a jpg. What I want to do is to feed the file name into the program via the console, and it will spit out the converted image. How can I make sure that the file given in the argument is of the correct type? Preferably I'd like to have it do more than just check the extension, because that could be erroneous.

This is my code as it stands.

Stream imageStreamSource = new FileStream("image.png", FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            FileStream stream = new FileStream("new.jpg", FileMode.Create);
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();

            Console.WriteLine(decoder.CodecInfo.MimeTypes.ToString());
            Console.WriteLine(encoder.CodecInfo.MimeTypes.ToString());

            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
            encoder.Save(stream);

Compare file extension? If you don't trust the file extension then you'll need to read the headers in the file, and detect if it is a valid header for that image type!

OK then, I guess I should be more specific: How can I read the header of a file to make sure that it matches one of the supported image types? I realize that not every file has a header, but I would imagine at least the images (png, jpg, gif, bmp, tif) have one, and can be easily compared.

OK, I think I managed to solve it on my own through reading some examples online, and finding a table of "magic numbers"

OK, I think I managed to solve it on my own through reading some examples online, and finding a table of "magic numbers"

Care to share your "magic" find with the community?

Lots of file types have what is called a "magic number", which is a string of bytes at (usually) the start of the file. In the case of a PNG file, according to w3.org, the first 8 bytes are *always* 137 80 78 71 13 10 26 10 . In hex this would be 89 50 4E 47 0D 0A 1A 0A .

To test for this, I made a simple method:

private bool isPNGFile(string path) {
            byte[] bytearray = File.ReadAllBytes(path);
            
            return bytearray.Length >= 8
                && bytearray[0] == 137
                && bytearray[1] == 80
                && bytearray[2] == 78
                && bytearray[3] == 71
                && bytearray[4] == 13
                && bytearray[5] == 10
                && bytearray[6] == 26
                && bytearray[7] == 10;
        }

Many other "magic numbers" can be found on various websites. I found two pages so far. I'm sure there are more resources online.

http://en.wikipedia.org/wiki/Magic_number_(programming)
http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html

Thanks agent154!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.