I hope someone can help

I'm trying to learn VB.net - migrating from VB6 and have a problem with overlapping images.

what I want to achieve is the following

  1. I have picture box on the form - call this picturebox1 containing an image
  2. I have 2 buttons button - let's call these red and blue
  3. if red is clicked I want to overlay a red disk (png) onto picturebox1
  4. if blue is clicked I want to overlay a blue disk (png) onto picturebox1
    Note! red and blue are the same size and so mask each other & it's important that the background of picturebox1 is still seen when the disks are overlayed onto it.

I'm having real problems with this! In VB6 I would simply make 3 pictureboxes (1 for each image) and manipulate the background style to be transparent, then manipulate their visible, left and top properties.

A real head scratcher,can someone help please

Many, many thanks in advance

6 Months
Discussion Span
Last Post by Xavier_5

I would suggest drawing to a bitmap object, in the order required, then setting the picture box's image to that, something like this:

Private Sub draw()
        Dim newBM As Bitmap     'the "canvas" to draw on
        Dim imgBG As Bitmap     'the background image
        Dim imgChar As Bitmap   'the character image

        imgBG = My.Resources.bg3    'using resource image
        imgChar = My.Resources.ban  'same

        newBM = New Bitmap(600, 200)    'new canvas
        Using g As Graphics = Graphics.FromImage(newBM)
            g.DrawImage(imgBG, 0, 0, 600, 200)  'draw a 600 x 200 rectangle on the canvas at point 0,0
            g.DrawImage(imgChar, 10, 10, 100, 100)  'draw a 100 x 100 rectangle at point 10,10 (the "character")
        End Using
        PictureBox1.Image = New Bitmap(newBM)   'set the image of the picture box to the newly drawn picture
    End Sub

Then change the opacity of the image -

PictureBox1.Image = ChangeOpacity(PictureBox1.Image, 0,5) ''Play with opacity here

This is just a quick sample, read up a bit more on ChangeOpacity.

One way would be to employ Graphics.FillEllipse, but already having the images and fixing the form's background image looks like this

Imports System.Drawing
Imports System.Drawing.Imaging

Public Class changeOpacityClass

    Dim bRed As Boolean = False
    Dim bmpBlue As Bitmap = Image.FromFile("C:\Users\Public\Pictures\blue.png")
    Dim bmpRed As Bitmap = Image.FromFile("C:\Users\Public\Pictures\Red.png")

    Private Sub btnRed_Click(sender As System.Object, e As System.EventArgs) Handles btnRed.Click
        bRed = True
    End Sub
    Private Sub btnBlue_Click(sender As System.Object, e As System.EventArgs) Handles btnBlue.Click
        bRed = False
    End Sub

    Private Sub changeOpacityClass_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim bmp As Bitmap = IIf(bRed, bmpRed, bmpBlue)
            Dim transparency As Single = 0.2
            Dim values(4)() As Single
            values(0) = New Single() {1, 0, 0, 0, 0}
            values(1) = New Single() {0, 1, 0, 0, 0}
            values(2) = New Single() {0, 0, 1, 0, 0}
            values(3) = New Single() {0, 0, 0, transparency, 0}
            values(4) = New Single() {0, 0, 0, 0, 1}
            Dim colormatrix As New ColorMatrix(values)
            Dim imgAttribute As New ImageAttributes
            imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
            e.Graphics.DrawImage(bmp, _
                          New Rectangle(140, 125, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, _
             GraphicsUnit.Pixel, imgAttribute)
        Catch ex As Exception

        End Try
    End Sub
End Class
Attachments ChgOpacity2.PNG 75.52 KB
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.