Results 1 to 11 of 11

Thread: High Quality Image Resizing

  1. #1

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    High Quality Image Resizing

    By changing interpolation mode (the algorithm used to resize) on a graphics object, you can resize your images and keep their quality.

    It will use more cpu time then usual, that's why "nearest neighbour" is the traditionally used algorithm, as it is a low cpu cost method.

    Here's is a function to resize your Images at a high quality

    Code:
        Private Function SizeImage(ByVal img As Bitmap, ByVal width As Integer, ByVal height As Integer) As Bitmap
            Dim newBit As New Bitmap(width, Height) 'new blank bitmap
            Dim g As Graphics = Graphics.FromImage(newBit)
            'change interpolation for reduction quality
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.DrawImage(img, 0, 0, width, Height)
            Return newBit
        End Function
    Last edited by Phill64; Dec 17th, 2007 at 03:29 PM.

  2. #2
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: High Quality Image Resizing

    Hi Phill,
    This is a really handy code.. really cool...Im gonna need to be able to zoom and stuff for one of my softwares you know about..Its gonna be quite useful to me atleast and also to tonnes of other people
    Keep coding....
    Godwin

    Help someone else with what someone helped you!

  3. #3
    New Member
    Join Date
    Feb 2006
    Posts
    13

    Re: High Quality Image Resizing

    Can you do it also with an image? in use of a bitmap

  4. #4

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: High Quality Image Resizing

    Bitmap is an inheritance of Image, so all you need to do is modify the header to input&output of type Image

    Code:
    Private Function SizeImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
    Last edited by Phill64; Dec 17th, 2007 at 03:32 PM.

  5. #5
    Member
    Join Date
    Feb 2006
    Posts
    45

    Re: High Quality Image Resizing

    Can I take an image from the server and change the dimension with this function?

  6. #6
    Member
    Join Date
    Feb 2006
    Posts
    45

    Re: High Quality Image Resizing

    The function does not change the image dimension. I get no errors. Probably using the function wrong. Any ideas?

    VB Code:
    1. Sub UploadImage()
    2.  
    3.             Dim url As String = "/images/artikkelBilder/"
    4.             Dim bildeTeller As Integer = 0
    5.  
    6.             If Not (fileupload1.PostedFile Is Nothing) Then 'Is there something to upload?
    7.  
    8.  
    9.  
    10.                 Dim strLongFilePath As String = fileupload1.PostedFile.FileName
    11.                 Dim intFileNameLength As Integer = InStr(1, StrReverse(strLongFilePath), "\")
    12.                 Dim strFileName As String
    13.  
    14.  
    15.                 If intFileNameLength > 0 Then
    16.                     strFileName = Mid(strLongFilePath, (Len(strLongFilePath) - intFileNameLength) + 2)
    17.                 Else
    18.                     strFileName = strLongFilePath
    19.                 End If
    20.  
    21.                 Select Case fileupload1.PostedFile.ContentType
    22.                     Case "image/pjpeg", "image/jpeg", "image/png", "image/gif"  'Valid file?
    23.                         fileupload1.PostedFile.SaveAs(Server.MapPath(url) & strFileName)
    24.                         Dim bilde As Image = Image.FromFile(Server.MapPath(url) & strFileName)
    25.                         SizeImage(bilde, 150, 100)
    26.                         lbStatus.Text += "Image path: " & "http://www.dyrebeskyttelsenhordaland.ord/images/artikkelBilder/" & strFileName & "<br>"
    27.                     Case Else
    28.                         'Not valid file
    29.                         lbStatus.Text += "Ugyldig bildeformat! <br>"
    30.                 End Select
    31.             End If
    32.  
    33.         End Sub
    34.  
    35.         Private Function SizeImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
    36.             Dim newBit As New Bitmap(width, height) 'new blank bitmap
    37.             Dim g As Graphics = Graphics.FromImage(newBit)
    38.             'change interpolation for reduction quality
    39.             g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    40.             g.DrawImage(img, 0, 0, width, height)
    41.             Return newBit
    42.         End Function
    43.  
    44.     End Class

  7. #7

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: High Quality Image Resizing

    I think you were under the assumption it is a sub which takes an object reference and simply changes your image. Infact it is a functon which accept your image & dimension and RETURNS a new image.

    so you should change this line
    VB Code:
    1. SizeImage(bilde, 150, 100)
    To
    VB Code:
    1. bilde = SizeImage(bilde, 150, 100)

  8. #8
    Member
    Join Date
    Feb 2006
    Posts
    45

    Re: High Quality Image Resizing

    Thanks for quick reply.
    It did'nt work, no difference.

    After it returns the new image, i'm supposed to save it again on the server?

  9. #9

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: High Quality Image Resizing

    if you want it changed on the file, yes.
    You will have to dispose the original image to do this otherwise you will get access errors

    Code:
    Dim temp As Bitmap = SizeImage(bilde, 150, 100)
    bilde.dispose
    bilde = temp
    bilde.Save(filename)
    Last edited by Phill64; Dec 17th, 2007 at 03:31 PM.

  10. #10
    Member
    Join Date
    Feb 2006
    Posts
    45

    Re: High Quality Image Resizing

    Thanks Phill64, worked like a charm

  11. #11
    Member
    Join Date
    Feb 2006
    Posts
    45

    Re: High Quality Image Resizing

    One more question

    I want the last added picture to be deleted if the user uploads a new one.

    Looks like it does'nt go through the IF, here is the code:
    http://pastebin.com/578210

    Solved it, had to use try catch and session
    Last edited by Spanjis; Mar 2nd, 2006 at 06:11 AM.

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