|
-
Apr 8th, 2003, 11:48 PM
#1
Thread Starter
Fanatic Member
Resize an Image Properly?
It's me again...
I've spent a few hours today trying to figure out how to properly resize a jpg image programmatically...
hellswraith has helped me out a bunch already, giving me a very nice example to start off with... but i've found that using GDI+ (at least in the way i'm using it) doesn't properly.. my resized image looks to be very low quality, even when i try saving it with the jpg quality at 100...
here's the sub i'm using to resize the image:
VB Code:
Function ResizeImage(ByVal MaxWidth As Integer, ByVal MaxHeight As Integer, ByVal Image As Bitmap) As Bitmap
Dim ScaleFactor As Decimal
Dim NewSize As SizeF
If CInt(Image.Size.Width) > MaxWidth Or CInt(Image.Size.Height) > MaxHeight Then
If CInt(Image.Size.Width) > CInt(Image.Size.Height) Then
'Image Width is too big
ScaleFactor = CDec(MaxWidth / CInt(Image.Size.Width))
Else
'Image Height is too big
ScaleFactor = CDec(MaxHeight / CInt(Image.Size.Height))
End If
NewSize.Width = Image.Size.Width * ScaleFactor
NewSize.Height = Image.Size.Height * ScaleFactor
'Return the resized image
ResizeImage = Image.GetThumbnailImage(NewSize.Width, NewSize.Height, Nothing, Nothing)
End If
End Function
Now, here's the original image i'm starting with:

Here's the image that my code resizes: (Keep in mind, the way i saved this image, i used jpeg encoder at a quality of 100)

And Finally, here's what Photoshop ends up with when it resizes my image to the same size as my code:

So, as you can see, my code doesn't do the job... So, i'm still looking for a proper way to do this, be it through a 3rd party .NET library, or code... i would PREFER to have code to do this in .NET myself, but if nothing else, i guess 3rd party would have to suffice... thanks for the help guys!
-
Apr 9th, 2003, 09:17 AM
#2
yay gay
well welcome to the club, u are not alone, a LOT of ppl asked already how to make that and couldnt figure it out, maybe the best thing u(we) can do is do it via api
anyways if u find a way plz share it with us (even if it's via api)
\m/  \m/
-
Apr 9th, 2003, 10:47 AM
#3
PowerPoster
Try saving it as a bitmap, see if the quality is there. If it is, it means the jpg encoding is messing up somewhere.
-
Apr 9th, 2003, 03:15 PM
#4
Thread Starter
Fanatic Member
i tried saving it as an Imaging.ImageFormat.bmp
however, i get a General GDI+ error when i do that... it only lets me save it as a jpeg... :/ maybe because the input file is a jpeg?
-
Apr 10th, 2003, 04:53 PM
#5
Thread Starter
Fanatic Member
can anyone offer other solutions? I really think this issue is largely unaddressed, and it would be great to have this information readily available to others who are looking for it as well....
-
Apr 10th, 2003, 05:57 PM
#6
PowerPoster
-
Apr 10th, 2003, 07:21 PM
#7
Thread Starter
Fanatic Member
those links were good hellswraith 
i've now come up with a function for resizing images PROPERLY.... it takes a Drawing.Image, and max width and max height... it then resizes it to constrain to the max width and max height, keeping the image in proportion to the original... it returns a bitmap, which in turn can be saved as jpeg, or whatever... but remember if you're saving as jpeg, you can have control over the quality setting as well...
VB Code:
Function ResizeImage(ByVal MaxWidth As Integer, ByVal MaxHeight As Integer, ByVal Image As Drawing.Image) As Bitmap
Dim ScaleFactor As Decimal
Dim NewSize As SizeF
If CInt(Image.Size.Width) > MaxWidth Or CInt(Image.Size.Height) > MaxHeight Then
If CInt(Image.Size.Width) > CInt(Image.Size.Height) Then
'Image Width is too big
ScaleFactor = CDec(MaxWidth / CInt(Image.Size.Width))
Else
'Image Height is too big
ScaleFactor = CDec(MaxHeight / CInt(Image.Size.Height))
End If
'Calculate the New height and width
NewSize.Width = Image.Size.Width * ScaleFactor
NewSize.Height = Image.Size.Height * ScaleFactor
'Rotate the image. Apparently this gives it better quality somehow
Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
'The returned image is a new bitmap based on the one given, but with new dimensions
ResizeImage = New Bitmap(Image, NewSize.Width, NewSize.Height)
End If
End Function
this gives a GOOD result.. so, it is possible to do this with just the GDI, and quite simple too. .i just think a LOT of people are misinformed about how this exactly works... so hopefully this will help everyone out...
Last edited by Redth; Apr 10th, 2003 at 07:25 PM.
-
Apr 10th, 2003, 08:00 PM
#8
PowerPoster
That is cool you got it working right. The code I started you off with was for me to create thumbnails with, so the quality wasn't an issue. I am glad you got this, now I can use it to for some other things.
I am still suspicious of this part though:
Code:
'Rotate the image. Apparently this gives it better quality somehow
Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
Seems a little wierd that doing that would cause a better quality.
Could you also post how you save to jpg with setting the quality?
-
Apr 10th, 2003, 08:47 PM
#9
Thread Starter
Fanatic Member
here's what i'm using to save with adjustable image quality...
VB Code:
' Get an array of ImageEncoders..array item 1 is Jpeg.
Dim imgCodecs() As Drawing.Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()
' Set quality Parameter for the Jpeg codec
Dim imgParams As Imaging.EncoderParameters = New Imaging.EncoderParameters(1)
Dim imgQuality As Imaging.EncoderParameter = New Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100)
' Set quality
imgParams.Param(0) = imgQuality
'Here's how you save with jpeg settings
myImage.Save("C:\MyImage.jpg", imgCodecs(1), imgParams)
-
Apr 10th, 2003, 08:51 PM
#10
Thread Starter
Fanatic Member
remember, the 100 number is the quality...
so that can be adjusted from 0 to 100 .... 100 being the best quality...
-
Apr 10th, 2003, 08:55 PM
#11
Originally posted by hellswraith
Could you also post how you save to jpg with setting the quality?
http://www.vbforums.com/showthread.p...hlight=quality
edit: eeh sounds like someone already answered you
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Apr 11th, 2003, 03:52 AM
#12
yay gay
Originally posted by hellswraith
That is cool you got it working right. The code I started you off with was for me to create thumbnails with, so the quality wasn't an issue. I am glad you got this, now I can use it to for some other things.
I am still suspicious of this part though:
Code:
'Rotate the image. Apparently this gives it better quality somehow
Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
Seems a little wierd that doing that would cause a better quality.
Could you also post how you save to jpg with setting the quality?
when doing web pages thumbnails on the fly with this method doesnt it take a LOT compared to getthumbnailsize?
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|