Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleDisplay a picture at different scales by using PaintPicture
DescriptionThis example shows how to display a picture at different scales by using PaintPicture in Visual Basic 6.
Keywordsresize, PaintPicture, bitmap, scale
CategoriesGraphics, Controls
 
When the user selects a scale, the program sets the ScaleFactor variable and then calls subroutine DrawPicture.

The hidden PictureBox HiddenPict holds the original image. Subroutine DrawPicture makes the visible PictureBox Pict big enough to hold the scaled image and then uses PaintPicture to copy the image onto it at the new scale.

 
Private ScaleFactor As Single

Private Sub mnuSetScale_Click(Index As Integer)
Dim i As Integer

    For i = 0 To 4
        mnuSetScale(i).Checked = False
    Next i
    mnuSetScale(Index).Checked = True
    
    Select Case Index
        Case 0
            ScaleFactor = 0.25
        Case 1
            ScaleFactor = 0.5
        Case 2
            ScaleFactor = 1
        Case 3
            ScaleFactor = 2
        Case 4
            ScaleFactor = 4
    End Select
    
    ' Redraw.
    DrawPicture
End Sub

' Display the picture at the correct scale.
Private Sub DrawPicture()
Dim wid As Single
Dim hgt As Single

    wid = HiddenPict.ScaleWidth * ScaleFactor
    hgt = HiddenPict.ScaleHeight * ScaleFactor
    Pict.Cls
    Pict.Move 0, 0, wid, hgt
    DoEvents
    Pict.PaintPicture HiddenPict.Picture, _
        0, 0, wid, hgt, 0, 0, _
        HiddenPict.ScaleWidth, _
        HiddenPict.ScaleHeight
End Sub
 
Note that scaling images by using thue PaintPicture method can cause "aliasing" which gives the image the blocky appearance. My book Visual Basic Graphics Programming explains how to resize images smoothly without aliasing.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated