Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Read width & Height of jpeg file

Read width & Height of jpeg file

Scheduled Pinned Locked Moved C#
data-structuresbeta-testingquestion
25 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Alaric_

    ...I was thinking that I was going to have to find a way to pull the FileHeader off, store it in the .Net equivalent of a BITMAPFILEHEADER. Then, pull the InfoHeader off of the file and store it in the .NET equivalent of a BITMAPINFOHEADER...& pull the width & height from it ...or by using WMI. That was more along my line of thinking.

    Welcome my son...Welcome..to the Machine

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #10

    Hi, The structs you mentioned exist in a .bmp file, not in a JPEG file. And before you ask, no I dont have a BMP Viewer yet. As for WMI, I dont think WMI would help looking at JPEGs; it is involved in system resources. :)

    Luc Pattyn [My Articles] [Forum Guidelines]

    A 2 Replies Last reply
    0
    • L Luc Pattyn

      Hi, The structs you mentioned exist in a .bmp file, not in a JPEG file. And before you ask, no I dont have a BMP Viewer yet. As for WMI, I dont think WMI would help looking at JPEGs; it is involved in system resources. :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      A Offline
      A Offline
      Alaric_
      wrote on last edited by
      #11

      I know that WMI is for system resources, but I saw an example while I was doing online research where somebody went to the WMI classes for the owner of a specific file (I think he went to one of the Operating System's Win32_Security classes) ...I was just thinking that the information might be included with it *somewhere*

      Welcome my son...Welcome..to the Machine

      1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, The structs you mentioned exist in a .bmp file, not in a JPEG file. And before you ask, no I dont have a BMP Viewer yet. As for WMI, I dont think WMI would help looking at JPEGs; it is involved in system resources. :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        A Offline
        A Offline
        Alaric_
        wrote on last edited by
        #12

        right...I knew that the BITMAPINFOHEADER was for a bmp & not a jpg ;P ...but I was hoping that something could be done to mimic its usage. (My bmp viewer is in C++...never ported it to .NET) ...bmp's are actually extremely simple, barely a step up from a PPM. I'm sure you could find it yourself, but the header just looks like this:

             height field
             bits per pixel field
             compression field
             colors field
             important colors field
        

        ...then you just have to remember that bitmaps are encoded as BGR values instead of RGB, so you have to map them accordingly

        Welcome my son...Welcome..to the Machine

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, the only way to get JPEG size without using Image class is by reading (part of) the JPEG file yourself, as in the following code:

          public static Size GetJpegImageSize(string filename) {
          FileStream stream=null;
          BinaryReader rdr=null;
          try {
          stream=File.OpenRead(filename);
          rdr=new BinaryReader(stream);
          // keep reading packets until we find one that contains Size info
          for(; ; ) {
          byte code=rdr.ReadByte();
          if(code!=0xFF) throw new ApplicationException(
          "Unexpected value in file "+filename);
          code=rdr.ReadByte();
          switch(code) {
          // filler byte
          case 0xFF:
          stream.Position--;
          break;
          // packets without data
          case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4:
          case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:
          break;
          // packets with size information
          case 0xC0: case 0xC1: case 0xC2: case 0xC3:
          case 0xC4: case 0xC5: case 0xC6: case 0xC7:
          case 0xC8: case 0xC9: case 0xCA: case 0xCB:
          case 0xCC: case 0xCD: case 0xCE: case 0xCF:
          ReadBEUshort(rdr);
          rdr.ReadByte();
          ushort h=ReadBEUshort(rdr);
          ushort w=ReadBEUshort(rdr);
          return new Size(w, h);
          // irrelevant variable-length packets
          default:
          int len=ReadBEUshort(rdr);
          stream.Position+=len-2;
          break;
          }
          }
          } finally {
          if(rdr!=null) rdr.Close();
          if(stream!=null) stream.Close();
          }
          }

          private static ushort ReadBEUshort(BinaryReader rdr) {
          ushort hi=rdr.ReadByte();
          hi<<=8;
          ushort lo=rdr.ReadByte();
          return (ushort)(hi|lo);
          }

          A JPEG file contains a lot of packets (with Hufman tables and everything). The above code does not know much about JPEG, it simply tries to jump from one packet to the next until it finds one with size info. :)

          Luc Pattyn [My Articles] [Forum Guidelines]

          A Offline
          A Offline
          Alaric_
          wrote on last edited by
          #13

          ok...one thing I absolutely hate is how .NET throws this for the code you provided:

          Error 6 Control cannot fall through from one case label ('case 207:') to another D:\XNA\Files\Projects\Terrain\TerrainWIP\Terrain\Terrain\JPEG.cs 61 58 Terrain

          Was your code C#.NET code? How were you able to force it to fall through the cases?

          Welcome my son...Welcome..to the Machine

          L 1 Reply Last reply
          0
          • A Alaric_

            ok...one thing I absolutely hate is how .NET throws this for the code you provided:

            Error 6 Control cannot fall through from one case label ('case 207:') to another D:\XNA\Files\Projects\Terrain\TerrainWIP\Terrain\Terrain\JPEG.cs 61 58 Terrain

            Was your code C#.NET code? How were you able to force it to fall through the cases?

            Welcome my son...Welcome..to the Machine

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #14

            Hi, I did not actually run the code I posted, but it seems OK to me. cases should be empty (that is how you can list cases to share all their code) or end on a change-of-flow (break, return, throw...) Did you somehow change the code and violate the above ? I guess you did, since 207 is 0xCF and that one ended on return... :)

            Luc Pattyn [My Articles] [Forum Guidelines]

            A 2 Replies Last reply
            0
            • L Luc Pattyn

              Hi, I did not actually run the code I posted, but it seems OK to me. cases should be empty (that is how you can list cases to share all their code) or end on a change-of-flow (break, return, throw...) Did you somehow change the code and violate the above ? I guess you did, since 207 is 0xCF and that one ended on return... :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              A Offline
              A Offline
              Alaric_
              wrote on last edited by
              #15

              no...identical code. Cut & pasted it. .NET's saying that it won't let a case fall through to execute code from another case. ...OxCF was the case that actually had code to execute. ..the others fell through to 0xCF.

              Welcome my son...Welcome..to the Machine

              A 1 Reply Last reply
              0
              • A Alaric_

                no...identical code. Cut & pasted it. .NET's saying that it won't let a case fall through to execute code from another case. ...OxCF was the case that actually had code to execute. ..the others fell through to 0xCF.

                Welcome my son...Welcome..to the Machine

                A Offline
                A Offline
                Alaric_
                wrote on last edited by
                #16

                lol...I saw what it was. You forgot your break in the 0xCF code, so it was saying it couldn't fall from 0xCF to the default code.

                Welcome my son...Welcome..to the Machine

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, I did not actually run the code I posted, but it seems OK to me. cases should be empty (that is how you can list cases to share all their code) or end on a change-of-flow (break, return, throw...) Did you somehow change the code and violate the above ? I guess you did, since 207 is 0xCF and that one ended on return... :)

                  Luc Pattyn [My Articles] [Forum Guidelines]

                  A Offline
                  A Offline
                  Alaric_
                  wrote on last edited by
                  #17

                  ...even if you have a return, it still wants you to have the break after it

                  Welcome my son...Welcome..to the Machine

                  L 2 Replies Last reply
                  0
                  • A Alaric_

                    ...even if you have a return, it still wants you to have the break after it

                    Welcome my son...Welcome..to the Machine

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #18

                    My Visual Studio C# 2005 Express Edition is happy without a break after a return, and produces a warning "unreachable code" if there is such a break; both seem logical to me. Are you using an different, maybe older, IDE ? :)

                    Luc Pattyn [My Articles] [Forum Guidelines]

                    1 Reply Last reply
                    0
                    • A Alaric_

                      ...even if you have a return, it still wants you to have the break after it

                      Welcome my son...Welcome..to the Machine

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #19

                      I have now tried with Visual Studio 7.1 and it behaves identically: return without break is fine, return+break gives warning. :)

                      Luc Pattyn [My Articles] [Forum Guidelines]

                      A 2 Replies Last reply
                      0
                      • L Luc Pattyn

                        I have now tried with Visual Studio 7.1 and it behaves identically: return without break is fine, return+break gives warning. :)

                        Luc Pattyn [My Articles] [Forum Guidelines]

                        A Offline
                        A Offline
                        Alaric_
                        wrote on last edited by
                        #20

                        I'm using VS 2005. return without break gives the error that I provided. ...break after return compiles with a warning

                        Welcome my son...Welcome..to the Machine

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          I have now tried with Visual Studio 7.1 and it behaves identically: return without break is fine, return+break gives warning. :)

                          Luc Pattyn [My Articles] [Forum Guidelines]

                          A Offline
                          A Offline
                          Alaric_
                          wrote on last edited by
                          #21

                          ...same in Visual C# 2005 Express

                          Welcome my son...Welcome..to the Machine

                          L 1 Reply Last reply
                          0
                          • A Alaric_

                            ...same in Visual C# 2005 Express

                            Welcome my son...Welcome..to the Machine

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #22

                            Well, that's a small mystery, one we will not solve easily, but I guess you can live with it ? Anyway, I trust you got the code up and running... :)

                            Luc Pattyn [My Articles] [Forum Guidelines]

                            A 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Well, that's a small mystery, one we will not solve easily, but I guess you can live with it ? Anyway, I trust you got the code up and running... :)

                              Luc Pattyn [My Articles] [Forum Guidelines]

                              A Offline
                              A Offline
                              Alaric_
                              wrote on last edited by
                              #23

                              yeah, I can live with it...I'm having a new problem though:

                              if (code != 0xFF) throw new ApplicationException(
                              "Unexpected value in file " + filename);

                              throws ....code equaled 216. What exactly is that check for? Do you know what the value of 216 means in this context? The first pass through, "code" was 255, then the next value was ...[Fixed it before I finished the post] ...Since the only thing I'm wanting to do is determine the width & height of the file, I just place a check for them inside the code check so it's now looking for

                              if(code != 0xFF && width == 0 && height == 0)
                              {
                              throw;
                              }

                              so now it will only throw if the width & height haven't been set yet..if they've been set, then I simply break when the code is not 0xFF. ...Is 0xFF like a key value for the header or something like that?

                              Welcome my son...Welcome..to the Machine

                              L 1 Reply Last reply
                              0
                              • A Alaric_

                                yeah, I can live with it...I'm having a new problem though:

                                if (code != 0xFF) throw new ApplicationException(
                                "Unexpected value in file " + filename);

                                throws ....code equaled 216. What exactly is that check for? Do you know what the value of 216 means in this context? The first pass through, "code" was 255, then the next value was ...[Fixed it before I finished the post] ...Since the only thing I'm wanting to do is determine the width & height of the file, I just place a check for them inside the code check so it's now looking for

                                if(code != 0xFF && width == 0 && height == 0)
                                {
                                throw;
                                }

                                so now it will only throw if the width & height haven't been set yet..if they've been set, then I simply break when the code is not 0xFF. ...Is 0xFF like a key value for the header or something like that?

                                Welcome my son...Welcome..to the Machine

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #24

                                Hi, the FF-check is for protection (I want the code to fail on something that isnt a JPEG at all!); so far all valid packets have a two-byte code that looks like 0xFFXX, and my code did return as soon as size was seen; you should not continue scanning the file after that ! (typically the size info is in the first few % of the file, and the scanner as is probably is unable to handle everything that might follow it). If there is any more trouble, please publish the entire method again. If you think there are some valid JPEG files that my code does not handle well, then mail me one or two of them. :)

                                Luc Pattyn [My Articles] [Forum Guidelines]

                                A 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  Hi, the FF-check is for protection (I want the code to fail on something that isnt a JPEG at all!); so far all valid packets have a two-byte code that looks like 0xFFXX, and my code did return as soon as size was seen; you should not continue scanning the file after that ! (typically the size info is in the first few % of the file, and the scanner as is probably is unable to handle everything that might follow it). If there is any more trouble, please publish the entire method again. If you think there are some valid JPEG files that my code does not handle well, then mail me one or two of them. :)

                                  Luc Pattyn [My Articles] [Forum Guidelines]

                                  A Offline
                                  A Offline
                                  Alaric_
                                  wrote on last edited by
                                  #25

                                  yeah...I didn't remove the return from your case code, but it was definitely not hitting it, which is extremely odd. ...It was definitely continuing past the point that it determined the width & height, which is why doing the assignment checks for width & height automagically switched the block. I'll look into what I did when I get home from work...and I'll post a screenshot of what I got completed.

                                  Welcome my son...Welcome..to the Machine

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups