Hi
I want below code to be written only once in Visual Basic Project.
Dim lSourceX As Long, lSourceY As Long
Dim lOldWidth As Long, lOldHeight As Long
Dim lNewWidth As Long, lNewHeight As Long
Picture1.AutoRedraw = True
Picture1.Picture = LoadPicture(App.Path & "\Images\O_0_" & tno & ".jpg")
Picture1.Width = Picture1.Picture.Width
Picture1.Height = Picture1.Picture.Height
Dim SrcWidth As Long, SrcHeight As Long
Dim DestWidth As Long, DestHeight As Long
Dim ScaleFactor As Double
Dim NewWidth As Long, NewHeight As Long
If Picture1.Picture Is Nothing Then
MsgBox "Please load an image into Picture1 first.", vbExclamation, "Error"
Exit Sub
End If
Picture1.ScaleMode = 3 ' Pixels
Picture2.ScaleMode = 3 ' Pixels
SrcWidth = Picture1.ScaleWidth ' 3400
SrcHeight = Picture1.ScaleHeight ' 1900
DestWidth = 196
DestHeight = 163
Dim SrcAspect As Double, DestAspect As Double
SrcAspect = SrcWidth / SrcHeight
DestAspect = DestWidth / DestHeight
If SrcAspect > DestAspect Then
ScaleFactor = DestWidth / SrcWidth
Else
ScaleFactor = DestHeight / SrcHeight
End If
NewWidth = 350 'CLng(SrcWidth * ScaleFactor)
NewHeight = 300 'CLng(SrcHeight * ScaleFactor)
Picture2.Width = DestWidth * Screen.TwipsPerPixelX
Picture2.Height = DestHeight * Screen.TwipsPerPixelY
Picture2.AutoRedraw = True
Picture2.Refresh
Picture2.Picture = Picture2.Image
Thanks
Muhammad Imran AnsariPosted Apr 21, 2025, 7:13 AM
Hello Ramco,
To ensure this block of code is reusable and only written once in your Visual Basic 6 (VB6) project, you can encapsulate it into a procedure or function within a module or a common class. Here's how you can do it using a Sub procedure.
Step 1: Create a Module
In your project, add a new module (e.g.,
modImageHelper).Step 2: Move the Code into a Sub
Step 3: Call the Sub from Anywhere
In your form or wherever needed:
Good Luck!
Eliana BlakePosted Apr 20, 2025, 1:09 AM
Hi there! It seems like you've shared a block of Visual Basic code where you want to ensure that it's written only once in your project. This code snippet appears to handle image processing and scaling between two Picture controls.
To organize and reuse this code in your Visual Basic project, you can encapsulate it within a Sub or Function. By doing this, you can call this Sub or Function whenever you need to perform this specific set of operations without duplicating the code across your project.
Here's an example of how you can encapsulate the provided code within a Subroutine:
Once you create this Subroutine in a module within your Visual Basic project, you can call it wherever needed by passing the appropriate parameters. This approach ensures that the code logic is centralized, making it easier to maintain and modify in the future.
Feel free to reach out if you need further assistance or have any specific questions about integrating this code snippet into your Visual Basic project. I'm here to help!