Results 1 to 8 of 8

Thread: how to stretch a picture?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    how to stretch a picture?

    how to stretch a picture?

    I have a pic in an image control.

    I need to stretch it to a smaller size

    plz help

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: how to stretch a picture?

    Have you set the image control's Stretch property to True?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to stretch a picture?

    Quote Originally Posted by ultra2
    I need to stretch it to a smaller size
    This sounds contridictory. Do you need to 'stretch' it or 'shrink' it?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: how to stretch a picture?

    Quote Originally Posted by Hack
    This sounds contridictory. Do you need to 'stretch' it or 'shrink' it?
    Obviously he wanted to shrink it but it doesn't matter because the same code would be used.

    If you only want to display the image in a smaller size you could as dee-u suggested just set the Stretch property of the Image control. However that would fit the picture inside the image control and not keep the aspect ratio between the width and the height of the image. To do that you need some simple calculations. However if you want to stretch or shrink an image and save it as a new image you would need to either use the StretchBlt API function or the VB built in PaintPicture method.

    I will assume you only want to display like a thumbnail of the image on the screen, so first set the Stretch property of the image control to True and then use code simular to this to stretch or shrink the image control while still keeping the correct aspect ratio between the width and the height:
    VB Code:
    1. Public Sub StretchImage(img As Image, dblRatio As Double)
    2.     Dim pic As StdPicture
    3.     Dim w As Long, h As Long
    4.     Dim nScale As Long
    5.    
    6.     Set pic = img.Picture
    7.     nScale = img.Parent.ScaleMode
    8.     img.Width = ScaleX(pic.Width, vbHimetric, nScale) * dblRatio
    9.     img.Height = ScaleY(pic.Height, vbHimetric, nScale) * dblRatio
    10. End Sub
    You can then call this Sub in the following manner:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call StretchImage(Image1, 0.5)
    3. End Sub
    That will shrink Image1 to half the width and height of the origional picture. You can pass a number higher then 1 to stretch the image. Just keep in mind that the ratio will always be from the origional picture size and not compared to the current width and height of the image control. So passing 1 as the aspect ratio will always resize the image control to the size the picture actually have.

    One more warning. Make sure there is actually a picture loaded into the image control before calling this Sub or it will fail.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: how to stretch a picture?

    Hi

    Look, interesting example.
    www.vb-helper.com/howto_stretch_picturebox.html
    Last edited by Tamgovb; Jul 6th, 2005 at 06:51 AM.
    I know, I know, my English is bad, sorry .....

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: how to stretch a picture?

    I need to save it as a new picture!

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: how to stretch a picture?

    This code snippet at Planet Source Code might help.

    After using StretchBlt, just try to save Picture.Image

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: how to stretch a picture?

    thx a lot, it works

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