CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    byte[] from filestream or from image have different lengths

    I want to get the byte[] from an image file.
    Code:
                   byte[] fromFile;
                   using (FileStream fs = new FileStream("myImage.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read)) {
                      fromFile= new byte[fs.Length];
                      fs.Read(fromFile, 0, Convert.ToInt32(fs.Length));
                   }
                   // fromFile has a length of 5308
    
                   using (MemoryStream ms = new MemoryStream()) {
                      ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                      ImageCodecInfo _codecInfo = null;
                      foreach (ImageCodecInfo codec in codecs) {
                         if (codec.MimeType == "image/jpeg")
                            _codecInfo = codec;
                      }
    
                      byte[] fromImage;
                      using (Image img = Image.FromFile("myImage.jpg")) {
                          img.Save(ms, ImageFormat.Jpeg);
                          fromImage = ms.ToArray();
                      }
                   }
                   //fromImage has a length of  5835
    why does the 2 byte arrays have a different length? it is the same file.

    I also tried
    Code:
                   using (MemoryStream ms = new MemoryStream()) {
                      ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                      ImageCodecInfo _codecInfo = null;
                      foreach (ImageCodecInfo codec in codecs) {
                         if (codec.MimeType == "image/jpeg")
                            _codecInfo = codec;
                      }
    
                      using (EncoderParameters _encoderParameters = new EncoderParameters()) {
                         _encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (long)100);
    
                         img.Save(ms, _codecInfo, _encoderParameters);
                         fromImage = ms.ToArray();
                      }
    and some other encoderParameters, but they all give a different length

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: byte[] from filestream or from image have different lengths

    Maybe each image is stored in different format? E.g. bmp vs. jpg.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: byte[] from filestream or from image have different lengths

    No, the image is not stored in an different format. The image is saved via Photoshop as an jpg file.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: byte[] from filestream or from image have different lengths

    I thought in memory as .NET object. The first is read as raw bytes from file, the second is an instance of Image object asked for its bytes representation. There can be some metadata in the second case, or some transformation could come into play while the image was instantinated. Try to save the image to file and read raw data from the file again, or compare the files.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured