Results 1 to 10 of 10

Thread: Resize pic file to a predetermined size

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Resize pic file to a predetermined size

    hello all,

    my user provides me (the application) with a picture file (jgp/bmp/Etc.).
    size = 250x400pxl.

    i then need to resize the picture file to a smaller size (!), i.e., make it into a 38x61pxl size. while keeping the right ratio & resolution...
    as target size is not very big, i cant afford distortion of picture.


    Thanks in advance for any ideas, suggestions, or help references.

    gooday,
    j.

  2. #2
    Addicted Member
    Join Date
    May 2004
    Location
    Nagpur, India
    Posts
    228

    Re: Resize pic file to a predetermined size

    Check StretchBlt API.
    I have a sub:
    Sub thumbnail(Width As Integer, Height As Integer, Source As PictureBox, dest As PictureBox)
    Source.Parent.ScaleMode = vbPixels
    dest.Parent.ScaleMode = vbPixels
    Source.ScaleMode = vbPixels
    dest.ScaleMode = vbPixels
    dest.Cls
    StretchBlt dest.hDC, 0, 0, dest.ScaleWidth, dest.ScaleHeight, Source.hDC, 0, 0, Source.ScaleWidth, Source.ScaleHeight, SRCCOPY

    End sub

  3. #3
    Addicted Member
    Join Date
    May 2004
    Location
    Nagpur, India
    Posts
    228

    Re: Resize pic file to a predetermined size

    So, load the Source piture box with your file.
    Make the dest picture box of the desired size

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Re: Resize pic file to a predetermined size

    tnx 4 your time in replying.

    4 some reason i get "variable not defined" msg, relating to SRCCOPY parameter in thumbnail sub.
    is there a reference needed?
    am i missin something?

  5. #5
    Addicted Member
    Join Date
    May 2004
    Location
    Nagpur, India
    Posts
    228

    Re: Resize pic file to a predetermined size

    Declare this:
    Public Const SRCCOPY As Long = &HCC0020

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Re: Resize pic file to a predetermined size

    have declared...
    now i have a "sub or function not defined" message regarding StretchBlt

    now?

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Resize pic file to a predetermined size

    just modified mayur's code a little bit at the highlighted part:
    VB Code:
    1. Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long _
    2.                                                 , ByVal x As Long _
    3.                                                 , ByVal y As Long _
    4.                                                 , ByVal nWidth As Long _
    5.                                                 , ByVal nHeight As Long _
    6.                                                 , ByVal hSrcDC As Long _
    7.                                                 , ByVal xSrc As Long _
    8.                                                 , ByVal ySrc As Long _
    9.                                                 , ByVal nSrcWidth As Long _
    10.                                                 , ByVal nSrcHeight As Long _
    11.                                                 , ByVal dwRop As Long) As Long
    12.                                                
    13. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
    14.  
    15. Sub thumbnail(Width As Integer, Height As Integer, Source As PictureBox, dest As PictureBox)
    16. Source.Parent.ScaleMode = vbPixels
    17. dest.Parent.ScaleMode = vbPixels
    18. Source.ScaleMode = vbPixels
    19. dest.ScaleMode = vbPixels
    20. dest.Cls
    21. StretchBlt dest.hdc, 0, 0, [hl]Width[/hl], [hl]Height[/hl], Source.hdc, 0, 0, Source.ScaleWidth, Source.ScaleHeight, SRCCOPY
    22.  
    23. End Sub
    24.  
    25. Private Sub Command1_Click()
    26. thumbnail 38, 61, Picture1, Picture2
    27. End Sub
    Show Appreciation. Rate Posts.

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Resize pic file to a predetermined size

    Here is another way without using APIs.
    VB Code:
    1. Sub ReduceSize(OrigFile As String, BMPFile As String, xWidth As Long, yHeight As Long)
    2. Dim AspectRatio As Single
    3.  
    4.     With Picture1
    5.         .AutoRedraw = True
    6.         .AutoSize = False
    7.         .BorderStyle = 0
    8.         .ScaleMode = vbPixels
    9.         .Parent.ScaleMode = vbPixels
    10.         .AutoSize = True
    11.    
    12.         'load original picture
    13.         'assumes OrigFile is path to picture
    14.         .Picture = LoadPicture(OrigFile)
    15.    
    16.         DoEvents
    17.    
    18.         'resize to your new size
    19.         'assumes xWidth and yHeight contain new size
    20.         'maintains aspect ratio
    21.         AspectRatio = .Width / .Height
    22.    
    23.         If AspectRatio > 1 Then
    24.             .Width = xWidth
    25.             .Height = xWidth / AspectRatio
    26.         Else
    27.             .Height = yHeight
    28.             .Width = yHeight * AspectRatio
    29.         End If
    30.    
    31.         'this takes the place of StretchBlt
    32.         .PaintPicture .Picture, 0, 0, .Width, .Height
    33.  
    34.         'save file to new bmp file
    35.         'Assumes BMPFile contains name of destination file
    36.         SavePicture .Image, BMPFile
    37.     End With
    38.  
    39. End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Exclamation Re: Resize pic file to a predetermined size

    answers::

    1st, thanks so much for your time in replying guys!

    unfortunatelly all the above methods of resizing the picture cause distortion of the original file.

    attached "target.jpg", is the result of both API and PaintPicture resizing methods.
    while "Dunhill_button_red_20s_small.jpg" is an ACDSee genereted resized copy.

    you can see the difference.

    i think maybe (if its possible at all) i need to include within my VB App an external exe of a picture editing software, (e,g,. AcDSee).
    which will handle picture resizing.

    doable?

    kind regards,
    josephine.
    Attached Images Attached Images   

  10. #10
    Addicted Member
    Join Date
    May 2004
    Location
    Nagpur, India
    Posts
    228

    Re: Resize pic file to a predetermined size

    check this code:
    http://www.freevbcode.com/ShowCode.Asp?ID=4997

    It is excellent!

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