Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET size image JPEG
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 6:41am
Edited 22-Jun-12 6:07am
v2
Rate this: bad
good
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.
  Permalink  
Comments
muhtarqong at 21-Jun-12 12:32pm
   
Great answer. I vote five.
CIDev at 21-Jun-12 12:38pm
   
Worthy of +5 :)
Tiberiu I. at 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
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Tiberiu I. at 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
good
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).
 
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);
  Permalink  
v7
Comments
Tiberiu I. at 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 at 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
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 544
2 Maciej Los 325
3 DamithSL 223
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Jun 2012
Copyright © CodeProject, 1999-2014
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