如何透明绘制图像的某些部分 [英] How to draw certain parts of an image transparent
本文介绍了如何透明绘制图像的某些部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在Vb.net上的一个程序上书写,我需要将图像的某些区域或某些像素设置为透明.我不是要在图像透明中设置相似的颜色,而是要将图像中的特定像素或区域设置为透明.
这是我尝试的代码,但是没有用:
公共 类 Form1
Dim 可以绘制 As 布尔值 = 错误
暗 g As 图形
昏暗目标目标 新建位图( 233 , 209 )
私有 子 Form1_Load( ByVal 发件人目标 对象, ByVal e 句柄 结束 子
私有 子 PictureBox1_LostFocus( ByVal 发件人目标 对象, ByVal e As System.EventArgs)错误
结束 子
私有 子 Picturebox1_MouseDown( ByVal 发件人目标 对象, ByVal e As System.Windows.Forms.MouseEventArgs)正确
结束 子
私有 子 Picturebox1_MouseMove(> ByVal 发件人目标 对象, ByVal e As System.Windows.Forms.MouseEventArgs)如果 canpaint = 真实 然后
g.FillEllipse(Brushes.Transparent,e.X,e.Y, 4 , 4 )
updateimage()
结束 如果
结束 子
私有 子 Picturebox1_MouseUp( ByVal 发件人目标 对象, ByVal e As System.Windows.Forms.MouseEventArgs)错误
结束 子
私有 Sub updateimage()
PictureBox1.Image =目标
结束 子
结束 类
解决方案
为了获得所需的结果,您需要使用setpixel方法清除像素.
在这里,我刚刚使用矩形(您将其指定为4x4)清除了像素.
导入 System.Drawing
公共 类 Form1
Dim 可以绘制 As 布尔值 = 错误
暗 g As 图形
昏暗目标目标 新建位图( 200 , 200 )
暗 b As 笔刷
枚举 dmode
d
Ë
结束 枚举
Dim m As dmode = dmode.d
私有 子 Form1_KeyDown( ByVal 发件人目标 对象, ByVal e 句柄 选择 案例 e.KeyCode
案例 Keys.R
m = dmode.d
b =笔刷红色
案例 Keys.G
m = dmode.d
b =画笔绿色
案例 Keys.K
m = dmode.d
b =笔刷.黑色
案例 Keys.T ' 透明度
m = dmode.e
结束 选择
结束 子
私人 子 Form1_Load( ByVal 发件人目标 对象, ByVal e 句柄 结束 子
私有 子 PictureBox1_LostFocus( ByVal 发件人目标 对象, ByVal e As System.EventArgs)错误
结束 子
私有 子 Picturebox1_MouseDown( ByVal 发件人目标 对象, ByVal e As System.Windows.Forms.MouseEventArgs)正确
结束 子
私有 子 Picturebox1_MouseMove(> ByVal 发件人目标 对象, ByVal e As System.Windows.Forms.MouseEventArgs)如果 canpaint = 真实 然后
如果 m = dmode.d 然后
g.FillEllipse(b,e.X,e.Y, 4 , 4 )
其他
对于 i As 整数 = eX 收件人 eX + 4
对于 j As 整数 = eY 收件人是+ 4
target.SetPixel(i,j,Color.Transparent)
下一步
下一步
结束 如果
updateimage()
结束 如果
结束 子
私有 子 Picturebox1_MouseUp( ByVal 发件人目标 对象, ByVal e As System.Windows.Forms.MouseEventArgs)错误
结束 子
私有 Sub updateimage()
PictureBox1.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
解决方案
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
这篇关于如何透明绘制图像的某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文