Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently writing on a program in Vb.net where I need certain regions or certain pixels of an image to be set transparent. I am not looking for setting a similar color in an image transparent, but specific pixels or regions in the image to be set transparent.

Here's the code I tryed, but didn't work:
VB
Public Class Form1
    Dim canpaint As Boolean = False
    Dim g As Graphics
    Dim target As New Bitmap(233, 209)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        target = PictureBox1.Image
        g = Graphics.FromImage(target)
    End Sub

    Private Sub PictureBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.LostFocus
        canpaint = False
    End Sub

    Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        canpaint = True
    End Sub

    Private Sub Picturebox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If canpaint = True Then
            g.FillEllipse(Brushes.Transparent, e.X, e.Y, 4, 4)
            updateimage()
        End If
    End Sub

    Private Sub Picturebox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        canpaint = False
    End Sub

    Private Sub updateimage()
        PictureBox1.Image = target
    End Sub
End Class
Posted
Updated 6-Jan-12 14:33pm
v2

1 solution

Hi Actually painting with Transparant brush will not draw/overwrite any thing.
To get the desired result you need to clear the pixels using the setpixel method.
Here I've just cleared the pixels using the rectangle (which you've given as 4x4).

VB
Imports System.Drawing
Public Class Form1
    Dim canpaint As Boolean = False
    Dim g As Graphics
    Dim target As New Bitmap(200, 200)
    Dim b As Brush
    Enum dmode
        d
        e
    End Enum
    Dim m As dmode = dmode.d

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.R
                m = dmode.d
                b = Brushes.Red
            Case Keys.G
                m = dmode.d
                b = Brushes.Green
            Case Keys.K
                m = dmode.d
                b = Brushes.Black
            Case Keys.T 'for Transparancy
                m = dmode.e
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PictureBox1.Image = target
        b = Brushes.Black
        g = Graphics.FromImage(target)
    End Sub

    Private Sub PictureBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.LostFocus
        canpaint = False
    End Sub

    Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        canpaint = True
    End Sub

    Private Sub Picturebox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If canpaint = True Then
            If m = dmode.d Then
                g.FillEllipse(b, e.X, e.Y, 4, 4)
            Else
                For i As Integer = e.X To e.X + 4
                    For j As Integer = e.Y To e.Y + 4
                        target.SetPixel(i, j, Color.Transparent)
                    Next
                Next
            End If
            updateimage()
        End If
    End Sub

    Private Sub Picturebox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        canpaint = False
    End Sub

    Private Sub updateimage()
        PictureBox1.Image = target
    End Sub

End Class
 
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