0

I have a problem when I put an image as Stream to a Bitmap.

The code swaps the width and height of the image.

The real dimensions of the image are:
Real width: 3216px
Real height: 4288px

But the code below swaps it so Height is 3216px and Width: 4288px
What happens is that the image also gets rotated when I save the bitmap.

FileStream fs = new FileStream("C:/image.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Bitmap bitmap = new Bitmap(fs);
int width = bitmap.Width;
int height = bitmap.Height;
bool hasresized = false;
double ratio = Convert.ToDouble(width) / Convert.ToDouble(height);
while (true) {
  if (width <= 900 && height <= 675) {
    break;
  }
  height--;
  width = Convert.ToInt32(ratio * Convert.ToDouble(height));
}
if (width != bitmap.Width || height != bitmap.Height) {
  //We need to resize
  bitmap = ResizeImage(bitmap, width, height);
  hasresized = true;
}

SaveImage(bitmap, "C:/test.jpg", ImageFormat.Jpeg, quality);
fs.Close();


public void SaveImage(Bitmap bitmap, String fileName, ImageFormat imageFormat, long quality = 100 L) {
  using(var encoderParameters = new EncoderParameters(1))
  using(encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)) {
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

    bitmap.Save(fileName, codecs.Single(codec => codec.FormatID == imageFormat.Guid), encoderParameters);
  }
}
public Bitmap ResizeImage(Image image, int width, int height) {
  var destRect = new Rectangle(0, 0, width, height);
  var destImage = new Bitmap(width, height);

  destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
  using(var graphics = Graphics.FromImage(destImage)) {
    graphics.CompositingMode = CompositingMode.SourceCopy;
    graphics.CompositingQuality = CompositingQuality.HighQuality;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

  }
  return destImage;
}

This is the image when it is saved. It has rotated 90 to the right: enter image description here

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • 2
    check the (exif) rotation tags! – TaW Nov 02 '19 at 21:17
  • I am not sure how to check the rotation tags. Even If I do that, how can I know if/why the image do rotate as it does which it shouldn't in the first place? – Andreas Nov 02 '19 at 21:17
  • 1
    see [here](https://stackoverflow.com/questions/42971094/getting-correct-image-rotation/42972969?r=SearchResults&s=1|45.8608#42972969) - Of course you can't know if those tags are correct. – TaW Nov 02 '19 at 21:18
  • I am not sure if I understand. I have tried to run the function in the URL: void Rotate(Bitmap bmp). That returns o == 1 which doesn't seem to be very good which means that no rotation to the correct rotation can occur? – Andreas Nov 02 '19 at 21:39
  • I run this code and it returns 1 which means that I can't take any action as I do not get any information on how to rotate the image correctly? `int Id = 0x0112; PropertyItem pi = bitmap.PropertyItems.Select(x => x).FirstOrDefault(x => x.Id == Id); MessageBox.Show(pi.Value[0].ToString());` – Andreas Nov 02 '19 at 22:11
  • The problem has to be something with your source image. I cannot reproduce the problem using the code you have in your question with one of my own images; it properly reduces the size and preserves the aspect ratio and rotation. Would it be possible for you to share the original source image? – Herohtar Nov 02 '19 at 22:24
  • That is strange. Yes that was a good idéa. I have uploaded the image to this URL: http://webbinglife.com/image.jpg – Andreas Nov 02 '19 at 22:28
  • When I run my code on your image it comes out as o==8 that is RotateFlipType.Rotate90FlipXY. So yes, you need to switch width and height. – TaW Nov 02 '19 at 22:38
  • @TaW, This sounds interesting how you could get o == 8 as I get o == 1, what is your code. Do you use the same code as I do? – Andreas Nov 02 '19 at 22:41
  • 1
    Yep, that's the problem... the EXIF data has "Orientation: Rotate 270 CW". So the actual orientation of your image is what you are seeing in the output and it has to be rotated 270° clockwise to show the "correct" orientation. – Herohtar Nov 02 '19 at 22:42
  • 1
    I did this: downloaded your image to disk, set it as a picturebox image and wrote: `Bitmap bmp = (Bitmap)pictureBox3.Image; PropertyItem pi = bmp.PropertyItems.Select(x => x) .FirstOrDefault(x => x.Id == 0x0112); byte o = pi.Value[0];` which I copied directly from my link. – TaW Nov 02 '19 at 22:42
  • 2
    @TaW and Herohtar I did now get o == 8 as you did and now it seems to actually work. I can't thank you enough. Thank you very much for your great help in this! – Andreas Nov 02 '19 at 22:59

0 Answers0