Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Visual-Studio VB.NET image , +
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:
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 6-Jan-12 13:55pm
CoolC22449
Edited 6-Jan-12 14:33pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
 
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
 
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100