Results 1 to 6 of 6

Thread: How to resize a picture and its picturebox?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    844

    How to resize a picture and its picturebox?

    Hi everybody,

    I have a picturebox in VB6 on a form that I load normally from a bmp file on the hard disk by calling the LoadPicture function like this:

    Picture1.Picture = LoadPicture(mypath + "\" + myfilename)

    I need to have buttons on the screen so the user can enlarge or reduce the picture.

    I cannot use an image box, and it has to be a picturebox.

    How can I enlarge or reduce the picture (and its picture box) easily and without losing the picture quality (or with minimum possible quality loss)?

    Please help.

    thanks,

  2. #2

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    844

    Re: How to resize a picture and its picturebox?

    But, when I have some pictures on the hard drive and I just double-click on one of them, Windows shows it with good quality, and I can use the + and - signs to enlarge and reduce the size many times and the picture never gets distorted.

    So ther should be a way to do it programmically too.

    After all the operating system does it (behind the scenes) programmically, so if I want to do it programmically, it should be possible.

    Any comment on that?

    Any help will be appreciated.

    thanks,

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to resize a picture and its picturebox?

    When scaling an image, there are several algorithms that have been created over time. VB by default, uses what is called the Nearest Neighbor algorithm. It is extremely fast but can produce questionable quality.

    Other common algorithms are BiLinear and BiCubic. GDI+ has 5 algos and various other quality settings that can produce very good quality scaled images. You don't have to use GDI+, the bilinear and bicubic algorithms can be found on the web and would require you to extract the pixel data, send it through your own scaling algorithm, and then paint the result to the picturebox. GDI+ is very fast when compared to doing this manually with VB.

    But as RhinoBull mentioned, distortion is always in play, but higher quality scaling algorithms can minimize the bad effects. Your O/S most likely using GDI+ when resizing the image.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    844

    Re: How to resize a picture and its picturebox?

    I use this code:

    Picture2.Height = Picture1.Height * 2
    Picture2.Width = Picture1.Width * 2

    ' Resize the picture.
    Picture2.AutoRedraw = True
    Picture2.PaintPicture Picture1.Image, _
    Picture2.ScaleLeft, Picture2.ScaleTop, _
    Picture2.ScaleWidth, Picture2.ScaleHeight, _
    Picture1.ScaleLeft, Picture1.ScaleTop, _
    Picture1.ScaleWidth, Picture1.ScaleHeight

    And it works.

    But my problem is that I don't want to have a separate picturebox named "Picture2".
    I want to resize the picture within the same picturebox "Picture1", and however I try to experiment with the above code, it doesn't do the job.

    Can you please help me?

    On a side note: Please note that in the line below:

    Picture2.PaintPicture Picture1.Image
    I cannot use Picture1.Picture, because the picturebox is loaded from an array in memory, and not directly from hard disk.

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to resize a picture and its picturebox?

    Simple search (at least through my posts) would you get this very simple procedure...
    And here is how to use it:
    Code:
    Option Explicit
    
    Public Sub ZoomPicture(pct As PictureBox, zoom As Double)
        With pct
            .AutoRedraw = True
            .Width = .Width * zoom
            .Height = .Height * zoom
            .PaintPicture .Picture, 0, 0, .ScaleWidth, .ScaleHeight
            .Refresh
        End With
    End Sub
    
    Private Sub Command1_Click()
        ZoomPicture Picture1, 0.9 'shrink it
    End Sub
    
    Private Sub Command2_Click()
        ZoomPicture Picture1, 1.1 'stretch it
    End Sub

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