Hi
I want below code to be written only once in Visual Basic Project.
VB.NET
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

Eliana BlakeRank #1,595 · 993 reputationPosted Apr 19, 2025, 9:09 PM EDT
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:
Sub ProcessAndScaleImages(tno As Integer) ' Provided code snippet here ' Simply paste the entire code block within this sub ' Ensure to replace any specific variables, like 'tno' in the Picture1.Picture line End SubOnce 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!
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Apr 21, 2025, 3:13 AM EDT
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
Public Sub LoadAndScaleImage(ByVal tno As String, ByRef Picture1 As PictureBox, ByRef Picture2 As PictureBox) 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") If Picture1.Picture Is Nothing Then MsgBox "Please load an image into Picture1 first.", vbExclamation, "Error" Exit Sub End If 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 Picture1.ScaleMode = 3 ' Pixels Picture2.ScaleMode = 3 ' Pixels SrcWidth = Picture1.ScaleWidth SrcHeight = Picture1.ScaleHeight 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 ' Or: CLng(SrcWidth * ScaleFactor) NewHeight = 300 ' Or: CLng(SrcHeight * ScaleFactor) Picture2.Width = DestWidth * Screen.TwipsPerPixelX Picture2.Height = DestHeight * Screen.TwipsPerPixelY Picture2.AutoRedraw = True Picture2.Refresh Picture2.Picture = Picture2.Image End SubStep 3: Call the Sub from Anywhere
In your form or wherever needed:
Call LoadAndScaleImage(tno, Picture1, Picture2)Good Luck!