Dear Sir
I'm trying to combine 4 resized images each into one page A5 using vb.net
The code below successfully combines 4 image files into A5 but at full size. So I want to manually resize each image so that when printing, the result matches the manual resize. And one more thing, the code I posted does not change the dpi in the output; there might be something wrong with the code I posted.
Info on how to resize the image below:
Height Image : 7.3 cm
Width Image : 5.2 cm
Please Guide
Thanks
Link image file
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim imagePaths As String() = {
folderPath + "\image1.png",
folderPath + "\image2.png",
folderPath + "\image3.png",
folderPath + "\image4.png"
}
Dim outputPath As String = folderPath + "\merged_image.png"
MergeImages(outputPath, imagePaths)
MessageBox.Show("Images merged successfully!")
End Sub
Public Sub MergeImages(outputPath As String, imagePaths As String())
' Ensure there are exactly 4 images
If imagePaths.Length <> 4 Then
Throw New ArgumentException("You must provide exactly 4 image paths.")
End If
' Define A5 size in pixels (at 300 DPI)
'Dim a5Width As Integer = CInt(1748) ' 148mm at 300 DPI
'Dim a5Height As Integer = CInt(2480) ' 210mm at 300 DPI
Dim a5Width As Integer = CInt(5.83 * 300) ' 5.8 inches
Dim a5Height As Integer = CInt(8.27 * 300) ' 8.3 inches
' Create a new bitmap for the A5 canvas
Using canvas As New Bitmap(a5Width, a5Height)
'set resolution
canvas.SetResolution(300, 300)
Using g As Graphics = Graphics.FromImage(canvas)
' Set background to white
g.Clear(Color.White)
' Load the images
Dim images As Image() = imagePaths.Select(Function(path) Image.FromFile(path)).ToArray()
' Calculate positions and sizes for the 4 images (2x2 grid)
Dim halfWidth As Integer = a5Width \ 2
Dim halfHeight As Integer = a5Height \ 2
' Draw each image in its respective quadrant
g.DrawImage(images(0), New Rectangle(0, 0, halfWidth, halfHeight)) ' Top-left
g.DrawImage(images(1), New Rectangle(halfWidth, 0, halfWidth, halfHeight)) ' Top-right
g.DrawImage(images(2), New Rectangle(0, halfHeight, halfWidth, halfHeight)) ' Bottom-left
g.DrawImage(images(3), New Rectangle(halfWidth, halfHeight, halfWidth, halfHeight)) ' Bottom-right
' Dispose of the images
For Each img In images
img.Dispose()
Next
End Using
' Save the final merged image
canvas.Save(outputPath, Imaging.ImageFormat.Png)
End Using
End Sub