Private Function RenderStdPicture(theTarget As Variant, thePic As StdPicture, _
Optional ByVal destX As Single, Optional ByVal destY As Single, _
Optional ByVal destWidth As Single, Optional ByVal destHeight As Single, _
Optional ByVal srcX As Single, Optional ByVal srcY As Single, _
Optional ByVal srcWidth As Single, Optional ByVal srcHeight As Single, _
Optional ByVal ParamScaleMode As ScaleModeConstants = vbUser, _
Optional ByVal Centered As Boolean = False, Optional ByVal ZoomFactor As Single = 1&) As Boolean
' Return Value [out]
' If no errors occur, return value is True. If error or invalid parameters passed, value is False
' Parameters [in]
' theTarget: a VB form, picturebox, usercontrol or a valid hDC (no error checking for valid DC)
' ... If Object, then it must expose a ScaleMode and hDC property
' ... and if centering and an object, must also expose ScaleWidth & ScaleHeight properties
' thePic: a VB ImageControl, stdPicture object, or VB .Picture property
' destX: horizontal offset on theTarget where drawing begins, default is zero
' destY: vertical offset on theTarget where drawing begins, default is zero
' destWidth: rendered image width & will be multiplied against ZoomFactor; default is thePic.Width
' destHeight: rendered image height & will be multiplied against ZoomFactor; default is thePic.Height
' srcX: horizontal offset of thePic to begin rendering from; default is zero
' srcY: vertical offset of thePic to begin rendering from; default is zero
' srcWidth: thePic width that will be rendered; default is thePic.Width
' srcHeight: thePic height that will be rendered; default is thePic.Height
' ParamScaleMode: Scalemode for passed parameters.
' If vbUser, then theTarget scalemode is used if theTarget is an Object else vbPixels if theTarget is an hDC
' Centered: If True, rendered image is centered in theTarget, offset by destX and/or destY
' If theTarget is a DC, then Centered is ignored. You must pass the correct destX,destY values
' ZoomFactor: Scaling option. Values>1 zoom out and Values<1||>0 zoom in
' Tip: To stretch image to a picturebox dimensions, pass destWidth & destHeight
' as the picturebox's scalewidth & scaleheight respectively and ZoomFactor of 1
If thePic Is Nothing Then Exit Function ' sanity checks first
If thePic.Handle = 0& Then Exit Function
If ZoomFactor <= 0! Then Exit Function
Dim Width As Single, Height As Single, destDC As Long
' the stdPicture.Render method requires vbPixels for destination and vbHimetrics for source
Width = ScaleX(thePic.Width, vbHimetric, vbPixels) ' image size in pixels
Height = ScaleY(thePic.Height, vbHimetric, vbPixels)
On Error Resume Next
If IsObject(theTarget) Then ' passed object? If so, set scalemode if needed
If theTarget Is Nothing Then Exit Function
If ParamScaleMode = vbUser Then ParamScaleMode = theTarget.ScaleMode
destDC = theTarget.hDC
ElseIf IsNumeric(theTarget) Then ' passed hDC? If so, set scalemode if needed
If ParamScaleMode = vbUser Then ParamScaleMode = vbPixels
destDC = Val(theTarget)
Centered = False ' only applicable if theTarget is a VB object
Else
Exit Function ' unhandled; abort
End If
If Err Then ' checks above generated an error; probably passing object without scalemode property?
Err.Clear
Exit Function
End If
If destWidth Then ' calculate destination width in pixels from ParamScaleMode
destWidth = ScaleX(destWidth, ParamScaleMode, vbPixels) * ZoomFactor
Else
destWidth = Width * ZoomFactor
End If
If destHeight Then 'calculate destination height in pixels from ParamScaleMode
destHeight = ScaleY(destHeight, ParamScaleMode, vbPixels) * ZoomFactor
Else
destHeight = Height * ZoomFactor
End If
' get destX,destY in pixels from ParamScaleMode
If destX Then destX = ScaleX(destX, ParamScaleMode, vbPixels)
If destY Then destY = ScaleY(destY, ParamScaleMode, vbPixels)
If Centered Then ' Offset destX,destY if centering
destX = (ScaleX(theTarget.ScaleWidth, theTarget.ScaleMode, vbPixels) - destWidth) / 2 + destX
destY = (ScaleY(theTarget.ScaleHeight, theTarget.ScaleMode, vbPixels) - destHeight) / 2 + destY
End If
' setup source coords/bounds and convert to vbHimetrics
If srcX Then srcX = ScaleX(srcX, ParamScaleMode, vbHimetric)
If srcY Then srcY = ScaleY(srcY, ParamScaleMode, vbHimetric)
If srcWidth Then srcWidth = ScaleX(srcWidth, ParamScaleMode, vbHimetric) Else srcWidth = thePic.Width
If srcHeight Then srcHeight = ScaleY(srcHeight, ParamScaleMode, vbHimetric) Else srcHeight = thePic.Height
If Err Then ' passed bad parameters or
Err.Clear ' passed object that has no ScaleMode property (i.e., VB Frame)
Else
With thePic ' render, the (destDC) below and variables declared as Single needed, else type mismatch errors occur
.Render (destDC), destX, destY, destWidth, destHeight, _
srcX, .Height - srcY, srcWidth, -srcHeight, ByVal 0&
End With ' return success/failure
If Err Then Err.Clear Else RenderStdPicture = True
End If
On Error GoTo 0
End Function