Hi kana250688,
Sorry for the delayed reply.
Here is the solution to your problem.
The Image Height 7.3cm is equal to 2.87402 inches and Image Width of 5.2cm is equal to 2.04724.
So you need to set the mention size in the Bitmap and add a emptySpace to the size. So that empty space will be added to the top, buttom, left and right.
Refer below code.
Public Class Form1
Dim folderPath As String = "D:\"
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
Dim a5Width As Integer = CInt(2.04724 * 300) ' 5.2cm
Dim a5Height As Integer = CInt(2.87402 * 300) ' 7.3cm
' 25px empty space
Dim emptySpace As Integer = 25
' Create a new bitmap for the A5 canvas
Using canvas As New Bitmap(a5Width + emptySpace, a5Height + emptySpace)
'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(emptySpace, emptySpace, halfWidth, halfHeight)) ' Top-left
g.DrawImage(images(1), New Rectangle(halfWidth, emptySpace, halfWidth, halfHeight)) ' Top-right
g.DrawImage(images(2), New Rectangle(emptySpace, 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
End Class