Question How can I check what type of image the file is ?

chocolade

New member
Joined
Dec 15, 2020
Messages
2
Programming Experience
3-5
The code for checking :


C#:
public enum ImageFormat
        {
            bmp,
            jpeg,
            gif,
            tiff,
            png,
            unknown
        }

        public static ImageFormat GetImageFormat(byte[] bytes)
        {
            var bmp = Encoding.ASCII.GetBytes("BM");     // BMP
            var gif = Encoding.ASCII.GetBytes("GIF");    // GIF
            var png = new byte[] { 137, 80, 78, 71 };    // PNG
            var tiff = new byte[] { 73, 73, 42 };         // TIFF
            var tiff2 = new byte[] { 77, 77, 42 };         // TIFF
            var jpeg = new byte[] { 255, 216, 255, 224 }; // jpeg
            var jpeg2 = new byte[] { 255, 216, 255, 225 }; // jpeg canon

            if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
                return ImageFormat.bmp;

            if (gif.SequenceEqual(bytes.Take(gif.Length)))
                return ImageFormat.gif;

            if (png.SequenceEqual(bytes.Take(png.Length)))
                return ImageFormat.png;

            if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
                return ImageFormat.tiff;

            if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
                return ImageFormat.tiff;

            if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
                return ImageFormat.jpeg;

            if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
                return ImageFormat.jpeg;

            return ImageFormat.unknown;
        }

but I'm not sure how to use it :

C#:
images = Directory.GetFiles(@"d:\satimages", "*.jpg");

            int counter = 0;

            foreach (string file in images)
            {
                using (var img = Image.FromFile(file))
                {
                    GetImageFormat(                
                }
            }

I want to make that if the image file is not gif type then convert the file to gif type.
I have already the conversion method and it's converting fine to gif but I want to check first if the file is not gif and only if it's not gif then convert it to gif but I'm not sure how to use the GetImageFormat.
 
That method is expecting a byte array. How do you get a byte array from a file? It's not by calling Image.FromFile. There's no mention of an Image object anywhere in that code.
 
The code for checking :


C#:
public enum ImageFormat
        {
            bmp,
            jpeg,
            gif,
            tiff,
            png,
            unknown
        }

        public static ImageFormat GetImageFormat(byte[] bytes)
        {
            var bmp = Encoding.ASCII.GetBytes("BM");     // BMP
            var gif = Encoding.ASCII.GetBytes("GIF");    // GIF
            var png = new byte[] { 137, 80, 78, 71 };    // PNG
            var tiff = new byte[] { 73, 73, 42 };         // TIFF
            var tiff2 = new byte[] { 77, 77, 42 };         // TIFF
            var jpeg = new byte[] { 255, 216, 255, 224 }; // jpeg
            var jpeg2 = new byte[] { 255, 216, 255, 225 }; // jpeg canon

            if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
                return ImageFormat.bmp;

            if (gif.SequenceEqual(bytes.Take(gif.Length)))
                return ImageFormat.gif;

            if (png.SequenceEqual(bytes.Take(png.Length)))
                return ImageFormat.png;

            if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
                return ImageFormat.tiff;

            if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
                return ImageFormat.tiff;

            if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
                return ImageFormat.jpeg;

            if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
                return ImageFormat.jpeg;

            return ImageFormat.unknown;
        }

but I'm not sure how to use it :

C#:
images = Directory.GetFiles(@"d:\satimages", "*.jpg");

            int counter = 0;

            foreach (string file in images)
            {
                using (var img = Image.FromFile(file))
                {
                    GetImageFormat(              
                }
            }

I want to make that if the image file is not gif type then convert the file to gif type.
I have already the conversion method and it's converting fine to gif but I want to check first if the file is not gif and only if it's not gif then convert it to gif but I'm not sure how to use the GetImageFormat.
This should do it, if you have concerns about performance you should not read the complete image file like this but rather just bytes needed for GetImageFormat method, but this is a way to do it...
C#:
images = Directory.GetFiles(@"d:\satimages", "*.jpg");

            int counter = 0;

            foreach (string fileInfo in images)
            {
                    GetImageFormat(File.ReadAllBytes(fileInfo.FullPath));
           
            }
 
Why ReadAllBytes when only first 4 bytes is ever tested? The code reads all .jpg images in a folder, there could be megabytes in each file and lots of files, it could be a tremendous waste of memory and processing resources.
 
Why ReadAllBytes when only first 4 bytes is ever tested? The code reads all .jpg images in a folder, there could be megabytes in each file and lots of files, it could be a tremendous waste of memory and processing resources.
This should do it, if you have concerns about performance you should not read the complete image file like this but rather just bytes needed for GetImageFormat method, but this is a way to do it...
 
I wouldn't even suggest it as a viable solution. Image files it generally large, and code is targeting "all files". The premise is just so wasteful.
 
It seems in general that there is a notion that it is better to point out that something is wrong instead of just providing an alternative (better) solution as an answer in this forum. It make me feel like yeah I don't know... The tone in this forum is overloaded by negativity/bad wibes?
Read some bytes from file:
byte[] buffer = new byte[4];
try
{
     using (FileStream fs = new FileStream(image.Name, FileMode.Open, FileAccess.Read))
     {
          var bytes_read = fs.Read(buffer, 0, buffer.Length);
          fs.Close();

          if (bytes_read != buffer.Length)
          {
              // Couldn't read 4 bytes
          }
     }
}
catch (System.UnauthorizedAccessException ex)
{
     Debug.Print(ex.Message);
}
 
It seems in general that there is a notion that it is better to point out that something is wrong instead of just providing an alternative (better) solution as an answer in this forum.
It's almost like we want to push people to think for themselves with a point in. the right direction rather than just handing them something to copy and paste. How exactly is that negative? Your opinion may differ but it's pretty much a proven fact that the best way to learn is to do. If you try and fail then you can always ask but if you don't try then you're not going to progress as a developer all that much. For instance, I often tend to couch my "answers" as questions because I'm trying to indicate to people the sort of questions they should be asking themselves. Personally, my aim in posting on forums is not simply to solve peoples problems but rather to help people become the best developer they can be. Sometimes that involves providing an explicit solution and sometimes it involves getting them to think in a different way for themselves. Expecting a copy/paste solution rather than having to think for oneself seems fairly negative to me.
 
instead of just providing an alternative (better) solution as an answer
See post 3. Open the stream and read 4 bytes.
 
@Joakim, your contributions are welcomed. :)

But you must also acknowledge their may be better suggestions than what you perceive to be a "better" answer, and ultimately if your solution is not liked, you will likely receive replies telling you why. And given the logic outlined to your proposed solution from the other guys, you should perceive these replies as tips to offer an improvement to your current answer rather than take it as harsh criticism. Both John's have made good points which could help you propose a better answer.

We are here to mostly give pointers to the OP, and also to help others who provide proposed answers, and to help them by offering suggestions on how a given solution could be made into a better solution and an accepted answer with some alterations.

As for :
The tone in this forum is overloaded by negativity/bad wibes?
If you have more proof of that, I've opened dialog with you VIA PM if you'd like to report anything specific, and I will forward it onto the admin of the board if necessary.
 
As far as I know, I'm the only negative Nancy on this board. Most everybody else here is very very encouraging and tries to cultivate new developers. As I've jokingly put it, I'm @Sheepings ' "evil twin". :)
 

Latest posts

Back
Top Bottom