Results 1 to 12 of 12

Thread: Resize an Image Properly?

  1. #1

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551

    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:
    1. Function ResizeImage(ByVal MaxWidth As Integer, ByVal MaxHeight As Integer, ByVal Image As Bitmap) As Bitmap
    2.  
    3.         Dim ScaleFactor As Decimal
    4.         Dim NewSize As SizeF
    5.  
    6.         If CInt(Image.Size.Width) > MaxWidth Or CInt(Image.Size.Height) > MaxHeight Then
    7.  
    8.             If CInt(Image.Size.Width) > CInt(Image.Size.Height) Then
    9.                 'Image Width is too big
    10.                 ScaleFactor = CDec(MaxWidth / CInt(Image.Size.Width))
    11.  
    12.             Else
    13.                 'Image Height is too big
    14.                 ScaleFactor = CDec(MaxHeight / CInt(Image.Size.Height))
    15.             End If
    16.  
    17.             NewSize.Width = Image.Size.Width * ScaleFactor
    18.             NewSize.Height = Image.Size.Height * ScaleFactor
    19.  
    20.             'Return the resized image
    21.             ResizeImage = Image.GetThumbnailImage(NewSize.Width, NewSize.Height, Nothing, Nothing)
    22.  
    23.         End If
    24.     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!

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Try saving it as a bitmap, see if the quality is there. If it is, it means the jpg encoding is messing up somewhere.

  4. #4

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    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?

  5. #5

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    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....

  6. #6

  7. #7

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    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:
    1. Function ResizeImage(ByVal MaxWidth As Integer, ByVal MaxHeight As Integer, ByVal Image As Drawing.Image) As Bitmap
    2.  
    3.         Dim ScaleFactor As Decimal
    4.         Dim NewSize As SizeF
    5.  
    6.         If CInt(Image.Size.Width) > MaxWidth Or CInt(Image.Size.Height) > MaxHeight Then
    7.  
    8.             If CInt(Image.Size.Width) > CInt(Image.Size.Height) Then
    9.                 'Image Width is too big
    10.                 ScaleFactor = CDec(MaxWidth / CInt(Image.Size.Width))
    11.  
    12.             Else
    13.                 'Image Height is too big
    14.                 ScaleFactor = CDec(MaxHeight / CInt(Image.Size.Height))
    15.             End If
    16.  
    17.             'Calculate the New height and width
    18.             NewSize.Width = Image.Size.Width * ScaleFactor
    19.             NewSize.Height = Image.Size.Height * ScaleFactor
    20.  
    21.             'Rotate the image.  Apparently this gives it better quality somehow
    22.             Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
    23.             Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
    24.  
    25.             'The returned image is a new bitmap based on the one given, but with new dimensions
    26.             ResizeImage = New Bitmap(Image, NewSize.Width, NewSize.Height)
    27.  
    28.         End If
    29.     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.

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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?

  9. #9

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    here's what i'm using to save with adjustable image quality...

    VB Code:
    1. ' Get an array of ImageEncoders..array item 1 is Jpeg.
    2. Dim imgCodecs() As Drawing.Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()
    3.  
    4. ' Set quality Parameter for the Jpeg codec
    5. Dim imgParams As Imaging.EncoderParameters = New Imaging.EncoderParameters(1)
    6. Dim imgQuality As Imaging.EncoderParameter = New Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100)
    7.  
    8. ' Set quality
    9. imgParams.Param(0) = imgQuality
    10.  
    11. 'Here's how you save with jpeg settings
    12. myImage.Save("C:\MyImage.jpg", imgCodecs(1), imgParams)

  10. #10

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    remember, the 100 number is the quality...

    so that can be adjusted from 0 to 100 .... 100 being the best quality...

  11. #11
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  12. #12
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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
  •  



Click Here to Expand Forum to Full Width