0

I've follow this post answer to reduce the uploaded image size. The image size is reduced but it also rotated the image itself (which it should not being rotated).

I don't see any part of the code is doing a rotation to the image and having no clue what's going on with the code.

Therefore, I am hoping may get some further explanation on what the code does and why the image is being rotated when reducing only the quality.

For convenience, I've copy the post answer here

// Make sure to include this at the top
   using System.Drawing.Imaging;


/// <summary> 
/// Saves an image as a jpeg image, with the given quality 
/// </summary> 
/// <param name="path"> Path to which the image would be saved. </param> 
/// <param name="quality"> An integer from 0 to 100, with 100 being the highest quality. </param> 
    public static void SaveJpeg (string path, Image img, int quality) 
    { 
        if (quality<0  ||  quality>100) 
        throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); 

        // Encoder parameter for image quality 
        EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); 
       // JPEG image codec 
       ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); 
       EncoderParameters encoderParams = new EncoderParameters(1); 
       encoderParams.Param[0] = qualityParam; 
       img.Save(path, jpegCodec, encoderParams); 
    } 

/// <summary> 
/// Returns the image codec with the given mime type 
/// </summary> 
    private static ImageCodecInfo GetEncoderInfo(string mimeType) 
    { 
        // Get image codecs for all image formats 
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

        // Find the correct image codec 
        for(int i=0; i<codecs.Length; i++) 
           if(codecs[i].MimeType == mimeType) 
               return codecs[i]; 

         return null; 
     } 
JackyLoo
  • 309
  • 5
  • 13
  • 4
    Maybe the code loses an exif rotation tag. test e.g. in irfanview! – TaW Aug 05 '18 at 12:37
  • Possible duplicate of [Get Image Orientation and rotate as per orientation](https://stackoverflow.com/questions/27835064/get-image-orientation-and-rotate-as-per-orientation) – mjwills Aug 05 '18 at 12:41
  • @TaW You are right! I uploaded original and re-qualify image to exifdata.com, the exif rotation tag is missing in the latter. May I know is there a way to preserve this exif rotation tag when changing the quality of the image? – JackyLoo Aug 05 '18 at 15:26
  • @mjwills The [Get Image Orientation and rotate as per orientation](https://stackoverflow.com/questions/27835064/get-image-orientation-and-rotate-as-per-orientation) did not answer my question as I didn't change the rotation of the image, but somehow the rotation of the image has changed after I reduce the image quality. – JackyLoo Aug 05 '18 at 15:45
  • 1
    [This post](https://stackoverflow.com/questions/42971094/getting-correct-image-rotation/42972969#42972969) shows how to modify the exif rotation tag. [Here](https://stackoverflow.com/questions/35295284/how-to-add-custom-exif-tags-to-a-image/35298122#35298122).is another post to show how to add a new propertyitem. Not sure where the code you have loses the propertyitems or exif tags, though.. – TaW Aug 05 '18 at 16:01
  • Hm, I just tested the code you posted and the results are confusing. While not all tags seem to be preserved most are the Orientation tag most definitely is. I used a simple test: `string infile = "D:\\xtest.jpg"; - How did you read the file? int q = trackBar1.Value; Image img = Image.FromFile(infile); SaveJpeg("D:\\xtest" + q + ".jpg", img, q);` – TaW Aug 05 '18 at 16:19
  • I doubt if is has something to do with the OS. I am currently using Mac Book to run VS for Mac when testing the code. Here is the sample I read the file and pass to the SaveJpeg method: `Image myImage = Image.FromFile("/Users/name/img.jpg"); SaveJpeg("/Users/name/newimg.jpg", myImage,50);` – JackyLoo Aug 05 '18 at 16:27
  • 1
    I also checked the links you share here. I try to look into the image object after I read from the directory, the `PropertyItems` are empty, I think it has already lose the exif data when reading the image file. – JackyLoo Aug 05 '18 at 16:32
  • `did not answer my question` You have two broad approaches if something is rotated via exif rotation data. One is to re-apply the exif after changing image quality (which most of the other comments are advocating). The other approach is to rotate **while changing image quality** (which removes the need for the exif data in the new image). My link may be helpful if you prefer that latter approach. – mjwills Aug 05 '18 at 21:13

0 Answers0