Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want merge two pictureboxes and provide a new picturebox in visual studio 2008

this code work for me but it doesn't keep the size of both the images.

Dim image1 As New Bitmap(picbox1.Image)
Dim image2 As New Bitmap(picbox2.Image)
Dim Image3 As New Bitmap(483, 338)
Dim xcount, ycount as integer

Using g As Graphics = Graphics.FromImage(Image3)

    'make 2nd bmp translucent
    Dim xcount, ycount As Integer

    For xcount = 0 To image2.Width - 1
        For ycount = 0 To image2.Height - 1
            Dim c As Color = image2.GetPixel(xcount, ycount)
            c = Color.FromArgb(125, c.R, c.G, c.B) '50% alpha
            image2.SetPixel(xcount, ycount, c)

    g.DrawImage(image1, New Point(0, 0))

    g.DrawImage(image2, New Point(0, 0))

End Using

picbox3.Image = Image3

Any Help Please.
glen205 26-Jan-16 6:44am    
If I understand your code, it does this:
- copies image1 into image3
- overlays a 50% alpha copy of image 2 onto image 3
- the desired outcome is image 3 = a blend/overlay of the two?

Some questions then, about the meaning of your issue : "it doesn't keep the size of both the images".
- in what way is the size "lost" ?
- are your source images a different size to each other, or to the fixed 483x338 pixel output?
- have you tried changing the fixed size of image 3: instead of hard-coded pixel sizes, perhaps the size of image 3 could be X = Math.Max(image1.Width, image2.Witdh), Y = Math.Max(image1.Height, image2.Height). Then the output image is always as large as required to contain both images.
Richard MacCutchan 26-Jan-16 6:45am    
Please explain what you mean by "but it doesn't keep the size of both the images.".
Engr. S.M. Inuwa 26-Jan-16 14:43pm    
i mean, the picbox3 doesn't keep the width and the height of the copied images and fit the container even if the size of the picbox3 is more the size of the pixbox1 and pixbox2
Richard MacCutchan 26-Jan-16 15:19pm    
Then you need to look at the sizes of both images that you start with and allow space for the larger of the two. Alternatively you can adjust the images to the same size.
Sinisa Hajnal 27-Jan-16 6:21am    
Check zoom property of the picture box, maybe it just distorts it because it is set to stretch ...or zoom.

Also, you should either resize both images to target size before merging or create image3 based on max sizes of both images (as glen205 suggests) and resize final result.

1 solution

this works for me

Dim logo As New Bitmap(picturebox1.Image)

' Display the result.
picturebox1.Image = logo

' Copy the label onto the main picture.
Dim template As New Bitmap(picturebox2.Image)
Dim gr As Graphics = Graphics.FromImage(template)
gr.DrawImage(logo, template.Width - 2007, (template.Height - 1800) \ 6, 500, 450)

' Display the result.
picturebox2.Image = template

thank you
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900