Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 22-Jun-12 5:07am
v2

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.
 
Share this answer
 
Comments
MuhtarQong 21-Jun-12 12:32pm    
Great answer. I vote five.
BillW33 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.
 
Share this answer
 
Comments
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.
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).

NOTICE:
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:

image.RotateFlip(RotateFlipType.Rotate90FlipNone);
 
Share this answer
 
v7
Comments
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 http://www.codeproject.com/Articles/4861/An-Image-Viewer-with-Lossless-Rotation-EXIF-and-Ot
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)



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