Joining multiple images together to form 1 single .jpg image

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi,

I am having trouble getting my VB app to join images on top of each
other to form a single file.

All my images are the same width and height and I would like to join
them together, one of top of the other.

My code compiles but does not give the right result!

Dim jpg1 As String = "c:\existing_image_1.jpg"
Dim jpg2 As String = "c:\existing_image_2.jpg"
Dim jpg3 As String = "c:\new_image.jpg"


Dim img1 As Image = Image.FromFile(jpg1)
Dim img2 As Image = Image.FromFile(jpg2)

Dim width As Integer = Math.Max(img1.Width, img2.Width)
Dim height As Integer = img1.Height + img2.Height + 20

Dim img3 As New Bitmap(width, height)
'MsgBox(img3.Width & " width, " & img3.Height & " height ")
Dim g As Graphics = Graphics.FromImage(img3)

g.Clear(Color.Empty)
g.DrawImage(img1, New Point(0, 0))
g.DrawImage(img1, New Point(0, img1.Height))
g.Dispose()

img1.Dispose()
img2.Dispose()

img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg)
img3.Dispose()


Its showing it like this.. Please excuse the crude diagram!

_________________________
| 1st image | |
| displays | |
|__here _____| |
| |
| |
| black background |
| |
| |
|____________ |
| 2nd image | |
|displays here | |
|___________ |_____________|

This is how I would like to to appear...



____________________
| |
| 1st image |
| here |
| |
| |
|___________________|
| |
| |
| 2nd image |
| here |
| |
|___________________|


Any help is much appreciated.

Thanks

Paul
 
Hi,

I am having trouble getting my VB app to join images on top of each
other to form a single file.

All my images are the same width and height and I would like to join
them together, one of top of the other.

My code compiles but does not give the right result!

        Dim jpg1 As String = "c:\existing_image_1.jpg"
        Dim jpg2 As String = "c:\existing_image_2.jpg"
        Dim jpg3 As String = "c:\new_image.jpg"

        Dim img1 As Image = Image.FromFile(jpg1)
        Dim img2 As Image = Image.FromFile(jpg2)

        Dim width As Integer = Math.Max(img1.Width, img2.Width)
        Dim height As Integer = img1.Height + img2.Height + 20

        Dim img3 As New Bitmap(width, height)
        'MsgBox(img3.Width & " width, " & img3.Height & " height ")
        Dim g As Graphics = Graphics.FromImage(img3)

        g.Clear(Color.Empty)
        g.DrawImage(img1, New Point(0, 0))
        g.DrawImage(img1, New Point(0, img1.Height))
        g.Dispose()

        img1.Dispose()
        img2.Dispose()

        img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg)
        img3.Dispose()

Its showing it like this.. Please excuse the crude diagram!

_________________________
|  1st image   |                        |
| displays      |                        |
|__here _____|                        |
|                                           |
|                                           |
|          black background         |
|                                           |
|                                           |
|____________                       |
| 2nd image    |                       |
|displays here |                       |
|___________ |_____________|

This is how I would like to to appear...

____________________
|                                  |
|         1st image           |
|           here                 |
|                                  |
|                                  |
|___________________|
|                                  |
|                                  |
|          2nd image         |
|            here                |
|                                  |
|___________________|

Any help is much appreciated.

Thanks

Paul

Hi Paul,

This code should do what you want, if all pictures have the same
width, specifying width of one of them is enough to specify the entire
picture's width, and the entire height is calculated by adding all the
heights of pictures, that is img3 as follows:

Dim jpg1 As String = "c:\existing_image_1.jpg"
Dim jpg2 As String = "c:\existing_image_2.jpg"
Dim jpg3 As String = "c:\new_image.jpg"

Dim img1 As Image = Image.FromFile(jpg1)
Dim img2 As Image = Image.FromFile(jpg2)

' If all images have same width
' specifiying one's width and
' is enough, eg: img1

Dim width As Integer = img1.Width
' If All the pictures have same height, multiplying
' one of pic's height with picture amount is enough
Dim height As Integer = img1.Height * 2

Dim img3 As New Bitmap(width, height)

Dim g As Graphics = Graphics.FromImage(img3)

g.DrawImage(img1, 0, 0, img1.Width, img1.Height)
g.DrawImage(img2, 0, img1.Height, img2.Width, img2.Height)


img1.Dispose()
img2.Dispose()

img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg)


Hope this helps,

Onur Güzel
 
Hi Paul,

This code should do what you want, if all pictures have the same
width, specifying width of one of them is enough to specify the entire
picture's width, and the entire height is calculated by adding all the
heights of pictures, that is img3 as follows:

Dim jpg1 As String = "c:\existing_image_1.jpg"
Dim jpg2 As String = "c:\existing_image_2.jpg"
Dim jpg3 As String = "c:\new_image.jpg"

Dim img1 As Image = Image.FromFile(jpg1)
Dim img2 As Image = Image.FromFile(jpg2)

' If all images have same width
' specifiying one's width and
' is enough, eg: img1

Dim width As Integer = img1.Width
' If All the pictures have same height, multiplying
' one of pic's height with picture amount is enough
Dim height As Integer = img1.Height * 2

Dim img3 As New Bitmap(width, height)

Dim g As Graphics = Graphics.FromImage(img3)

g.DrawImage(img1, 0, 0, img1.Width, img1.Height)
g.DrawImage(img2, 0, img1.Height, img2.Width, img2.Height)

img1.Dispose()
img2.Dispose()

img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg)

Hope this helps,

Onur Güzel- Hide quoted text -

- Show quoted text -

Hi Onur,

Thank you very much for the working code! It works like a charm. Now
my next step is to give support for more than 2 images and add a nice
GUI! Thanks for setting me on my way!

Paul
 
Back
Top