Hi all,

I have a picturebox with scalemode pixel

dim height as integer
dim width as integer

height = picbox.scaleHeight
width = picbox.scaleWidth

picbox.PaintPicture pic, 0, 0, height, width, 0, 0, picheight, picwidth

note: picheight and picwidth are the height and the width of pic (in pixel) that has been calculated with autosize picturebox in other form.

My problem is: the picture cannot be drawn for the entire picturebox... there is a gap on the right and bottom in the picturebox... why is it happens?

Note: my project is to zoom out the pic... to fit the picbox

Thanks

Dani AI

Generated

Nice catch by on keeping ScaleMode consistent and by on the PaintPicture parameter order. Two things typically cause the right/bottom gap you saw: (1) PaintPicture takes destination width and height first, then the source width and height; if width/height are swapped the image will not fill a wider-than-tall PictureBox. (2) PaintPicture uses the container’s ScaleMode for units; with vbPixels you must pass pixel values for both dest and source. See the PaintPicture signature and notes for the order and units. PaintPicture method (previous versions, Microsoft Learn).

One more subtlety that often bites folks later: StdPicture reports Width and Height in HIMETRIC (0.01 mm), not pixels. If you read dimensions from a StdPicture (e.g., after LoadPicture or from another control’s Picture), convert them with ScaleX/ScaleY before using PaintPicture. Microsoft’s IPicture docs call out HIMETRIC explicitly. IPicture (Win32), Width/Height types are OLE_*_HIMETRIC.

If you want a reliable “fit-to-box” (zoom out) that preserves aspect ratio and centers the image:

Private Sub DrawImageFit(pb As PictureBox, ByVal img As StdPicture)
    Dim srcW As Single, srcH As Single
    Dim k As Single, destW As Single, destH As Single
    Dim dx As Single, dy As Single

    ' Convert HIMETRIC (StdPicture) to pixels
    srcW = pb.ScaleX(img.Width, vbHimetric, vbPixels)
    srcH = pb.ScaleY(img.Height, vbHimetric, vbPixels)

    ' Largest uniform scale that fits
    k = pb.ScaleWidth / srcW
    If pb.ScaleHeight / srcH < k Then k = pb.ScaleHeight / srcH

    destW = srcW * k
    destH = srcH * k
    dx = (pb.ScaleWidth - destW) / 2
    dy = (pb.ScaleHeight - destH) / 2

    pb.Cls
    pb.PaintPicture img, dx, dy, destW, destH, 0, 0, srcW, srcH
End Sub

Tips: set pb.ScaleMode = vbPixels and, if you want the drawing to persist after repaints, set pb.AutoRedraw = True before calling the routine. If you prefer to stretch to completely fill the box (no letterboxing), pass pb.ScaleWidth and pb.ScaleHeight directly as dest width/height.

Recommended Answers

All 3 Replies

scalemode the same for all? because this works (as a test)

Picture2.PaintPicture Picture1.Picture, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight

and picture2 is set at double the size of picture1...

Properties changed at design time
AutoRedraw
ScaleMode = vbPixels
Picture1.AutoSize = True 'while picture2.autosize = false

Good Luck

The problem is from Height to ScaleHeight and ScaleHeight to Height

Get the Height and Width of the Image from the Picture object rather than from AutoSize

To Get the Height and Width of the Image

Private Sub Form_Load()
   
   Dim img As StdPicture
   Set img = LoadPicture("abc.jpg")
   'Image Height and Width is in HiMetrix Unit and Change it to Pixel
   MsgBox (" Height = " & HiMetrixToPixelY(img.Height) & " px Width = " & HiMetrixToPixelX(img.Width) & " px")
End Sub

'Function to Change Height from HiMetrix to Pixel
Private Function HiMetrixToPixelY(yHiM) As Long
   HiMetrixToPixelY = Me.ScaleY(yHiM, vbHimetric, vbPixels)
End Function
'Function to Change Width from HiMetrix to Pixel
Private Function HiMetrixToPixelX(xHiM) As Long
   HiMetrixToPixelX = Me.ScaleX(xHiM, vbHimetric, vbPixels)
End Function

I have shown u to convert one unit to another

The Problem may be from PaintPicture method call
Syntax is
PaintPicture picture, x1, y1, destWidth, destHeight, x2, y2, srcWidth, srcHeight, opcode

But u have interchanged the parameters width for height and height for width in the picbox.PaintPicture pic, 0, 0, height, width, 0, 0, picheight, picwidth I hope this may help u

good one thanks.. it has been solved now... thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.