Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
For the last 13 years I have been importing photos to my web application.
As soon as I downloaded the image, I created a smaller thumbnail.
I use this in a small danish web log, to save history and pictures.
An example of it is: www.bagergyden.dk.
It is written in VB.NET with some c# using VS2019.

My problem is that the image suddenly reads the same with the same width and height, although it comes in two variants: landscape and portrait mode.
I often have to rotate a photo 90 degrees to straighten it up.
It is all sound and clear when you see the pictures in Windows and the cursor clearly shows the different dimensions.

But suddenly I lose the aspect!

What I have tried:

When I debug the code and reads the file into a Bitmap, the width and height is the same for both the two variants though one of them has - should have been - turned 90 degrees.

Any explanation or sugestions about what is happening?
Posted
Updated 21-Dec-23 2:04am
v3
Comments
Richard MacCutchan 21-Dec-23 7:28am    
without seeing the code you use it is impossible for anyone here to guess what is happening. Please use the Improve question link above, and add complete details of what is not working.
Bo Vistisen 2023 22-Dec-23 7:25am    
Thank you Richard.

I can boil it down to only one line of code!
In VB: Dim bmpFoto As New System.Drawing.Bitmap(FullPathFilename)

I am running Windows 11 Home 23H2 from which I guess the Bitmap function comes from.
I have compiled my solution to .NET framework 4.0 and for 4.6.1.

My test is simple. From my smartphone I uploaded a picture to the desktop. It was taken in 3264x2488 and let us call this horizontal. Then I turned it 90 degrees and saves it as a 2488x3264 vertical copy. As I said, screen and curser showes the correct thumbnails and sizes.

Trying to read the vertical with Bitmap it keep given me a picture with the original horizontal size. I also tried it on a Windows 10 Home 22H2 with the same result.

All my computers is slowly but surely updated by Microsoft. Last correct version was, as I remember, created on a Windows 10 in VS2019 using .NET 4.0.

But where is this error?
Dave Kreskowiak 22-Dec-23 9:45am    
Again, without seeing the code, it's impossible to tell you what's going on.
[no name] 22-Dec-23 13:30pm    
There are subtle changes in frameworks; e.g. compressing in one and using another to decompress can result in differences in file length (bad). One usually suggests to the encoder a particular dimension (via decoding) for "one side" of the image. (It figures out the "other" side). As far as the best time to rotate ... no idea. If I rotate at all, I use "run time rotation" (of the image control) around a given point (0-360).

For JPEG images taken with a smartphone, the information on how the phone was held at the time the image was taken is saved in the EXIF information in the orientation feature. While older programs in particular always assume an orientation of 0 degrees, newer display programs evaluate the image orientation. If you want to use both older and newer programs, you should set the orientation flag to 0 and rotate the image if necessary.
 
Share this answer
 
Comments
0x01AA 28-Dec-23 19:15pm    
+5
Thank you! You nailed it spot on! 
I had to read up on EXIF. I found a good article by Hannes DuPreez: "Getting Image Data from Picture Files in .NET" from 2018.
It is 20 years old code and your advise is right. I rotate the picture on the computer so it looks right. Often it is a question of 90 degrees left or right. When the program uploads the picture and the turning only has been in the EXIF information, I physically turn it and clear the orientation flag back to 1 (TopLeft)

 <pre>           
Dim bmpSource As New System.Drawing.Bitmap(FullPathFilename)

Dim EW As New EXIFData(bmpSource)
Dim iOrientation As Integer
iOrientation = EW.Orientation()

  ' If iOrientation is 1 called TopLeft it is Ok
  ' If iOrientation is 8 it is fliped 90 clockwise then flip it 270 degrees more
  If iOrientation = 8 Then bmpSource.RotateFlip(RotateFlipType.Rotate270FlipNone)
  ' If iOrientation is 3 it is flip upside down then flip it 180 degrees
  If iOrientation = 3 Then bmpSource.RotateFlip(RotateFlipType.Rotate180FlipNone)
  ' If iOrientation is 6 it is 90 against the clock then flip it 90 degrees
  If iOrientation = 6 Then bmpSource.RotateFlip(RotateFlipType.Rotate90FlipNone)
  ' Then set the Orientation to 1 and save the rotated bitmap to disk
  iOrientation = 1
  EW.Orientation() = iOrientation
  bmpSource.Save(FullPathFilename)

  ' Verify width, height and Orientation
  Dim txtEXIF As String = EW.ToString()

  EW.Dispose()

And thank you to CodeProject.com
 
Share this answer
 
Comments
0x01AA 28-Dec-23 10:37am    
Thanks that you posted what finally helped you, 5 ;)
merano99 28-Dec-23 17:40pm    
"read up on EXIF" was my suggestion, it is strange that the questioner posts a solution on this basis, which then also gets points, while my suggestion is neither marked as a solution nor gets points.
merano99 28-Dec-23 18:03pm    
It's nice that my suggestion was able to contribute to solving the question. It would be customary to mark helpful suggestions as solutions. Then the question will also be displayed as solved. An own (final) solution could better be written as an additional paragraph with "improve question" instead of as an own solution.
Bo Vistisen 29-Dec-23 5:32am    
Merano99, I am sorry I might have responded a wrong way.
I give all credit to your response and advice to me! I don't want points. I just wanted to correct an upcoming problem with smartphones. Read my profile. I am long gone retired. In fact, I have trouble maintaining my VS2019 enviroment and the constantly updating of Windows just to correct an error.
merano99 30-Dec-23 20:45pm    
Since my answer has helped, it would be nice if you could mark my answer as a solution. Otherwise it could give the impression that there is no solution yet. Points would also be appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900