Click here to Skip to main content
13,050,429 members (133,623 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want make a program in VB which can rotate images with 90 degrees and then saves them as rotated (while viewing them, like the windows Picture Viewer).

If I try to make an Image object, then rotate it and then save it then its size will differ (e.g. I had a file of 1.21 MB and after I rotated it I got the same image rotated with 3.20 MB).

How can I work directly on jpg?
Or is there any other method of rotating an image and keeping the resolution the same (because HorizontalResolution and VerticalResolution are ReadOnly properties)
Posted 21-Jun-12 5:41am
Updated 22-Jun-12 5:07am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you rotate an image, then there are three problems which may affect the image size.
1) If you do not rotate it to a width and height which are the same as the original. For example, if you rotate a square image by 45 degrees, the resulting image needs a bigger square in which to sit - and all images are based on a rectangular box.
2) Jpeg files use a lossy compression - when you rotate an image by any amount, you may well end up with an image which does not compress as well. This can make the file significantly bigger.
3) When you write a jpeg file, the compression amount can be adjusted. If the routine used to save the image is not the same as the original image was saved with, or if the compression parameters are different in any way, teh resulting file will not be the same size.

Generally speaking it is a bad idea to do any image processing on a jpeg file (including rotating it). Jpeg is a lossy compression algorithm, so any sbsequent processing is already working on a degraded image. Work directly from an original bitmap or other non-lossy compressed format) if you can, and save jpegs from that as separate files.
muhtarqong 21-Jun-12 12:32pm
Great answer. I vote five.
CIDev 21-Jun-12 12:38pm
Worthy of +5 :)
Tiberiu I. 22-Jun-12 11:03am
Thanks for your answer.
1)I rotate it 90 degrees
2, 3)I studied my files and the difference is the resolution, because after I rotate it its resolution becomes much bigger. But I don't know how to change that to a bitmap because it's HorizontalResolution and VerticalResolution are ReadOnly properties.

I have to work on jpeg, because almost all photos are jpeg and not bitmaps.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Tiberiu I. 22-Jun-12 11:04am
Thanks for links, but they are not very helpful because they use the Image object of VB.NET which means a bitmap file. I'd like to work directly on jpeg files somehow.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Please try the following (after seeing your comment in solution 2):

1) If "you are inside .NET" you can only work with "Image" or "Bitmap" (in GDI or GDI+).
2) You can use ".jpg" or ".jpeg" when you are saving images. However, all image processings happen with "Image" and "Bitmap" in the .NET environment (GDI or GDI+).

3) Load the image as a Bitmap.
4) Get original image's Ver. and Hor. resolutions and hold them in some variables (xDpi, yDpi).
5) After rotate is completed, set the resolution again (xDpi, yDpi).

But, rotating 90 degrees, I think can be completed with RotateFlip property of the Bitmap or Image object and it won't change the resolution:

Tiberiu I. 22-Jun-12 12:12pm
I cannot set the resolution again because HorisontalResolution and VerticalResolution are readonly properties.

But I think I just found the answer to my problem here
muhtarqong 22-Jun-12 12:16pm
It is good for you you got your answer. If you are trying to set resolution, you can set it in Bitmap.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100