none
Combining two images inside PictureBoxes in VB.NET RRS feed

  • Question

  • I have two images inside two separate pictureboxes in vb.net I want to overlay them, and was referred by someone to use draw a picture on one picturebox, since that will only take a very short time. I have not found a way to combine two pictureboxes together yet, so how can I do that?
    Sunday, April 5, 2020 12:46 AM

Answers

  • I'm sorry that I didn't clarify it enough. I have 2 images with transparent backgrounds and I want to stack them on top of each other.

    Hi

    Do you mean like this?

    If so, then this code produced it.

    Option Strict On
    Option Explicit On
    Public Class Form1
      ' I have 2 images (1.png and 2.png)
      ' in this folder
      Dim ImPath As String = IO.Path.Combine(Application.StartupPath, "Images")
    
      Dim im1 As New Bitmap(IO.Path.Combine(ImPath, "1.png"))
      Dim im2 As New Bitmap(IO.Path.Combine(ImPath, "2.png"))
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Image = im1
        PictureBox2.Image = im2
        PictureBox3.SizeMode = PictureBoxSizeMode.Zoom
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox3.Image = MakeNewImage(New Bitmap(im1), New Bitmap(im2))
      End Sub
      Function MakeNewImage(ByVal i1 As Bitmap, ByVal i2 As Bitmap) As Bitmap
        Dim g As Graphics = Graphics.FromImage(i1)
        g.DrawImage(i2, New Point(0, 0))
        Return i1
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by greatbetta Friday, April 10, 2020 11:37 PM
    Wednesday, April 8, 2020 12:49 AM

All replies

  • Hi,

    Do you want to connect the two pictures together (upper and lower or left and right), or do you want to adjust their transparency and compound the two pictures together (combination of the top and bottom layers)?

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, April 5, 2020 8:27 AM
    Moderator
  • Hello GreatBetta,

    Telling that you want two separate images in one picturebox tells as much as ordering two separate meals in a restaurant without telling anything more. 

    It tells almost nothing.

    Therefore a lot can be done, but at least tell what kind of picturebox (forms, Wpf, Asp, ……) and how the 2 pictures have to be combined. For the last there are endless ways, not only how they are placed in a picturebox.  


    Success
    Cor


    Sunday, April 5, 2020 9:41 AM
  • If you want to Blend 2 Picture Boxes, you can use the MSDN code from :

    How to: Use a Color Matrix to Set Alpha Values in Images

    Test =>

    Sunday, April 5, 2020 10:27 AM
  • I want to connect the two pictures together, not blend them.
    Sunday, April 5, 2020 8:33 PM
  • I want to connect the two pictures together, not blend them.

    Hi

    If you would be as clear with what you DO want as you are with what ypu don't want then you would probably get answers.

    I mean; what on this earth does 'connect the two pictures together' mean? Do you even kbnow what connecting two pictures together means?

    You need to define clearly what you want - a wishy washy question will likely go unanswed.

    In your original post, you said 'combine two pictureboxes together' - well, you rejected the post which DID provide the answer. It is a pity that the real question remains in your thoughts alone.


    Regards Les, Livingston, Scotland

    Sunday, April 5, 2020 9:33 PM
  • I was responding to someone else.
    Monday, April 6, 2020 3:39 AM
  • Hi greadbetta,

    I'm afraid you didn't answer the leshay's question.
    We want to know the meaning of "connect the two pictures together".
    Is it like this?
        

    Regards,

    Ashidacchi -- http://hokusosha.com

    Tuesday, April 7, 2020 1:12 AM
  • Hi,

    Is the following result that you want?

    If so, you can try my code as follows.

    Imports System.Drawing.Imaging
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim image1 As New Bitmap(PictureBox1.Image)
            Dim image2 As New Bitmap(PictureBox2.Image)
            Dim width As Integer = image1.Width + image2.Width
            Dim height As Integer = Math.Max(image1.Height, image2.Height)
            Dim Image3 As New Bitmap(width, height)
    
            Using g As Graphics = Graphics.FromImage(Image3)
    
                g.DrawImage(image1, New Point(0, 0))
                g.DrawImage(image2, New Point(image1.Width, 0))
    
            End Using
            PictureBox3.Image = Image3
        End Sub
    
    End Class
    

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 7, 2020 2:29 AM
    Moderator
  • I'm sorry that I didn't clarify it enough. I have 2 images with transparent backgrounds and I want to stack them on top of each other.
    Tuesday, April 7, 2020 9:33 PM
  • I'm sorry that I didn't clarify it enough. I have 2 images with transparent backgrounds and I want to stack them on top of each other.

    Hi

    Do you mean like this?

    If so, then this code produced it.

    Option Strict On
    Option Explicit On
    Public Class Form1
      ' I have 2 images (1.png and 2.png)
      ' in this folder
      Dim ImPath As String = IO.Path.Combine(Application.StartupPath, "Images")
    
      Dim im1 As New Bitmap(IO.Path.Combine(ImPath, "1.png"))
      Dim im2 As New Bitmap(IO.Path.Combine(ImPath, "2.png"))
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Image = im1
        PictureBox2.Image = im2
        PictureBox3.SizeMode = PictureBoxSizeMode.Zoom
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox3.Image = MakeNewImage(New Bitmap(im1), New Bitmap(im2))
      End Sub
      Function MakeNewImage(ByVal i1 As Bitmap, ByVal i2 As Bitmap) As Bitmap
        Dim g As Graphics = Graphics.FromImage(i1)
        g.DrawImage(i2, New Point(0, 0))
        Return i1
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by greatbetta Friday, April 10, 2020 11:37 PM
    Wednesday, April 8, 2020 12:49 AM
  • Yes, that's perfect
    Friday, April 10, 2020 11:37 PM