none
super impose pictures RRS feed

  • Question

  • Hi:

    I am using VB.NET 2019.  I have two pictures on my desktop which ultimately will go inside my resources.  I want to know how to super impose them in a picturebox.  Please see the attached images.  The background colors of all the pictures are always the same.  The user will select image1 and image2 and click on a command button, Then the picturebox should show image3 (which is not on the resourses or the computer). I would appreciate the code for this, as I have absolutely no idea on how to do this or even start on it. thank you.

    Bob

    Tuesday, July 2, 2019 4:49 PM

Answers

  • Is this what you mean?

    :)

    The example makes the controls. To run just cut and paste the code into an empty form. Change the form name as required. Change the image names to your files.

    Public Class Form1    Private WithEvents Pic1 As New PictureBox With {.Parent = Me,
            .Location = New Point(10, 10), .Size = New Size(120, 120),
            .BackgroundImage = Image.FromFile("c:\bitmaps\a1a.png"),
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
        Private WithEvents Pic2 As New PictureBox With {.Parent = Me,
            .Location = New Point(150, 10), .Size = New Size(120, 120),
            .BackgroundImage = Image.FromFile("c:\bitmaps\a1b.png"),
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
        Private WithEvents Pic3 As New PictureBox With {.Parent = Me,
            .Location = New Point(150, 150), .Size = New Size(120, 120),
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
        Private WithEvents Button1 As New Button With {.Parent = Me,
            .Location = New Point(40, 200), .Size = New Size(80, 32), .Text = "Combine"}
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 300)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim bmp1 As Bitmap = CType(Pic1.BackgroundImage, Bitmap)
            Dim bmp2 As Bitmap = CType(Pic2.BackgroundImage, Bitmap)
            Dim bmp As New Bitmap(bmp1.Width, bmp1.Height)
    
            Using g As Graphics = Graphics.FromImage(bmp)
                'draw both images on bmp with white transparent color range
                Dim attr1, attr2 As New System.Drawing.Imaging.ImageAttributes
                attr1.SetColorKey(Color.FromArgb(250, 250, 250), Color.FromArgb(255, 255, 255))
    
                g.DrawImage(bmp1, New Rectangle(0, 0, bmp1.Width, bmp1.Height),
                            0, 0, bmp1.Width, bmp1.Height, GraphicsUnit.Pixel, attr1)
    
                g.DrawImage(bmp2, New Rectangle(0, 0, bmp2.Width, bmp2.Height),
                            0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel, attr1)
    
                'show the combined image in pic3
                If Pic3.BackgroundImage IsNot Nothing Then Pic3.BackgroundImage.Dispose()
                Pic3.BackgroundImage = bmp
    
            End Using
    
        End Sub
    End Class

    • Marked as answer by booboo_US Thursday, July 4, 2019 6:52 PM
    Thursday, July 4, 2019 12:37 PM

All replies

  • Hi,

    You can place the two pictures in two pictureboxes and then stitch the two pictureboxes.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Image = My.Resources.image1
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            PictureBox1.Size = New Size(200, 200)
            PictureBox2.Image = My.Resources.image2
            PictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            PictureBox2.Size = New Size(200, 200)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PictureBox2.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + PictureBox1.Height)
        End Sub
    End Class

    Best Regards,

    Alex


    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.


    Wednesday, July 3, 2019 3:16 AM
  • Is this what you mean?

    :)

    The example makes the controls. To run just cut and paste the code into an empty form. Change the form name as required. Change the image names to your files.

    Public Class Form1    Private WithEvents Pic1 As New PictureBox With {.Parent = Me,
            .Location = New Point(10, 10), .Size = New Size(120, 120),
            .BackgroundImage = Image.FromFile("c:\bitmaps\a1a.png"),
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
        Private WithEvents Pic2 As New PictureBox With {.Parent = Me,
            .Location = New Point(150, 10), .Size = New Size(120, 120),
            .BackgroundImage = Image.FromFile("c:\bitmaps\a1b.png"),
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
        Private WithEvents Pic3 As New PictureBox With {.Parent = Me,
            .Location = New Point(150, 150), .Size = New Size(120, 120),
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
        Private WithEvents Button1 As New Button With {.Parent = Me,
            .Location = New Point(40, 200), .Size = New Size(80, 32), .Text = "Combine"}
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 300)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim bmp1 As Bitmap = CType(Pic1.BackgroundImage, Bitmap)
            Dim bmp2 As Bitmap = CType(Pic2.BackgroundImage, Bitmap)
            Dim bmp As New Bitmap(bmp1.Width, bmp1.Height)
    
            Using g As Graphics = Graphics.FromImage(bmp)
                'draw both images on bmp with white transparent color range
                Dim attr1, attr2 As New System.Drawing.Imaging.ImageAttributes
                attr1.SetColorKey(Color.FromArgb(250, 250, 250), Color.FromArgb(255, 255, 255))
    
                g.DrawImage(bmp1, New Rectangle(0, 0, bmp1.Width, bmp1.Height),
                            0, 0, bmp1.Width, bmp1.Height, GraphicsUnit.Pixel, attr1)
    
                g.DrawImage(bmp2, New Rectangle(0, 0, bmp2.Width, bmp2.Height),
                            0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel, attr1)
    
                'show the combined image in pic3
                If Pic3.BackgroundImage IsNot Nothing Then Pic3.BackgroundImage.Dispose()
                Pic3.BackgroundImage = bmp
    
            End Using
    
        End Sub
    End Class

    • Marked as answer by booboo_US Thursday, July 4, 2019 6:52 PM
    Thursday, July 4, 2019 12:37 PM
  • Thank you very much.  This is exactly what I wanted. 
    Thursday, July 4, 2019 6:52 PM