locked
Resize script is rotating image sometimes? RRS feed

  • Question

  • User1986916315 posted

    I have this create thumbnail script and it works fine most of the time, but sometimes it causes the image to rotate sideways. Will someone please let me know how to adjust it to prevent that?

    private void Page_Load(object sender, System.EventArgs e)
          {
                string Image = Request.QueryString["Image"];
                if (Image == null)  
                {
                      this.ErrorResult();
                      return;
                }
                string sSize = Request["Size"]; 
                int Size = 150;
                if (sSize != null)
                      Size = Int32.Parse(sSize);
                string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image;
                Bitmap bmp = CreateThumbnail(Path,Size,Size);
                if (bmp == null) 
                {
                      this.ErrorResult();
                      return;
                }
                string OutputFilename = null;
                OutputFilename = Request.QueryString["OutputFilename"];
                if (OutputFilename != null) 
                {
                      if (this.User.Identity.Name == "")  
                      {
                            // *** Custom error display here
                            bmp.Dispose();
                            this.ErrorResult();
                      }
                      try 
                      {
                            bmp.Save(OutputFilename);
                      }
                      catch(Exception ex) 
                      {
                            bmp.Dispose();
                            this.ErrorResult();
                            return;
                      }
                }
                // Put user code to initialize the page here
                Response.ContentType = "image/jpeg";
                bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
                bmp.Dispose();
          }
          private void ErrorResult()
          {
                Response.Clear();
                Response.StatusCode = 404; 
                Response.End();
          }
          /// 
          /// Creates a resized bitmap from an existing image on disk.
          /// Call Dispose on the returned Bitmap object
          /// 
          /// 
          /// 
          /// 
          /// Bitmap or null
          public static Bitmap CreateThumbnail(string lcFilename,int lnWidth, int lnHeight)
          {
                System.Drawing.Bitmap bmpOut = null;
                try 
                {
                      Bitmap loBMP = new Bitmap(lcFilename);
                      ImageFormat loFormat = loBMP.RawFormat;
                      decimal lnRatio;
                      int lnNewWidth = 0;
                      int lnNewHeight = 0;
    
                      //*** If the image is smaller than a thumbnail just return it
                      if (loBMP.Width < lnWidth && loBMP.Height < lnHeight) 
                            return loBMP;
                      if (loBMP.Width > loBMP.Height)
                      {
                            lnRatio = (decimal) lnWidth / loBMP.Width;
                            lnNewWidth = lnWidth;
                            decimal lnTemp = loBMP.Height * lnRatio;
                            lnNewHeight = (int)lnTemp;
                      }
                      else 
                      {
                            lnRatio = (decimal) lnHeight / loBMP.Height;
                            lnNewHeight = lnHeight;
                            decimal lnTemp = loBMP.Width * lnRatio;
                            lnNewWidth = (int) lnTemp;
                      }
    
                      // System.Drawing.Image imgOut = 
                      //      loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
                      //                              null,IntPtr.Zero);
                      // *** This code creates cleaner (though bigger) thumbnails and properly
                      // *** and handles GIF files better by generating a white background for
                      // *** transparent images (as opposed to black)
                      bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                      Graphics g = Graphics.FromImage(bmpOut);
                      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                      g.FillRectangle( Brushes.White,0,0,lnNewWidth,lnNewHeight);
                      g.DrawImage(loBMP,0,0,lnNewWidth,lnNewHeight);
                      loBMP.Dispose();
                }
                catch 
                {
                      return null;
                }
                return bmpOut;
          }

    EDIT: I think maybe the photos don't contain some kind of data sometimes, because if I take an image that the thumbnail script is rotating and run it through photoshop, it works fine without rotating it after that. I've also noticed this mostly (maybe always) with images that are taller than they are wide.

    Friday, October 31, 2014 9:49 AM

Answers

  • User1986916315 posted

    I believe I ended up finding the answer and worked it into this code. It appears the code wasn't compensating for exif data for the thumbnail. So if a photo was taken with the camera sideways, the thumbnail was displaying sideways, while my computer was reading that exif data and automatically rotating it in windows to display it as it's suppose to be. So this updated code has a new section that reads the exif data and rotates accordingly.

    private void Page_Load(object sender, System.EventArgs e)
          {
                string Image = Request.QueryString["Image"];
                if (Image == null)  
                {
                      this.ErrorResult();
                      return;
                }
                string sSize = Request["Size"]; 
                int Size = 150;
                if (sSize != null)
                      Size = Int32.Parse(sSize);
                string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image;
                Bitmap bmp = CreateThumbnail(Path,Size,Size);
                if (bmp == null) 
                {
                      this.ErrorResult();
                      return;
                }
                string OutputFilename = null;
                OutputFilename = Request.QueryString["OutputFilename"];
                if (OutputFilename != null) 
                {
                      if (this.User.Identity.Name == "")  
                      {
                            // *** Custom error display here
                            bmp.Dispose();
                            this.ErrorResult();
                      }
                      try 
                      {
                            bmp.Save(OutputFilename);
                      }
                      catch(Exception ex) 
                      {
                            bmp.Dispose();
                            this.ErrorResult();
                            return;
                      }
                }
                // Put user code to initialize the page here
                Response.ContentType = "image/jpeg";
                bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
                bmp.Dispose();
          }
          private void ErrorResult()
          {
                Response.Clear();
                Response.StatusCode = 404; 
                Response.End();
          }
          /// 
          /// Creates a resized bitmap from an existing image on disk.
          /// Call Dispose on the returned Bitmap object
          /// 
          /// 
          /// 
          /// 
          /// Bitmap or null
          public static Bitmap CreateThumbnail(string lcFilename,int lnWidth, int lnHeight)
          {
                System.Drawing.Bitmap bmpOut = null;
                try 
                {
                      Bitmap loBMP = new Bitmap(lcFilename);
                      ImageFormat loFormat = loBMP.RawFormat;
                      decimal lnRatio;
                      int lnNewWidth = 0;
                      int lnNewHeight = 0;
    
                    //Check for exif data to determin orientation of camera when photo was taken and rotate to what's expected
                      if (loBMP.PropertyIdList.Contains(0x112)) //0x112 = Orientation
                      {
                          var prop = loBMP.GetPropertyItem(0x112);
                          if (prop.Type == 3 && prop.Len == 2)
                          {
                              UInt16 orientationExif = BitConverter.ToUInt16(loBMP.GetPropertyItem(0x112).Value, 0);
                              if (orientationExif == 8)
                              {
                                  loBMP.RotateFlip(RotateFlipType.Rotate270FlipNone);
                              }
                              else if (orientationExif == 3)
                              {
                                  loBMP.RotateFlip(RotateFlipType.Rotate180FlipNone);
                              }
                              else if (orientationExif == 6)
                              {
                                  loBMP.RotateFlip(RotateFlipType.Rotate90FlipNone);
                              }
                          }
                      }
    
                      //*** If the image is smaller than a thumbnail just return it
                      if (loBMP.Width < lnWidth && loBMP.Height < lnHeight) 
                            return loBMP;
                      if (loBMP.Width > loBMP.Height)
                      {
                            lnRatio = (decimal) lnWidth / loBMP.Width;
                            lnNewWidth = lnWidth;
                            decimal lnTemp = loBMP.Height * lnRatio;
                            lnNewHeight = (int)lnTemp;
                      }
                      else 
                      {
                            lnRatio = (decimal) lnHeight / loBMP.Height;
                            lnNewHeight = lnHeight;
                            decimal lnTemp = loBMP.Width * lnRatio;
                            lnNewWidth = (int) lnTemp;
                      }
    
                      // System.Drawing.Image imgOut = 
                      //      loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
                      //                              null,IntPtr.Zero);
                      // *** This code creates cleaner (though bigger) thumbnails and properly
                      // *** and handles GIF files better by generating a white background for
                      // *** transparent images (as opposed to black)
                      bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                      Graphics g = Graphics.FromImage(bmpOut);
                      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                      g.FillRectangle( Brushes.White,0,0,lnNewWidth,lnNewHeight);
                      g.DrawImage(loBMP,0,0,lnNewWidth,lnNewHeight);
                      loBMP.Dispose();
                }
                catch 
                {
                      return null;
                }
                return bmpOut;
          }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 31, 2014 4:00 PM