如何避免在graphics.drawrectangle()上重叠不透明的矩形?

时间:2017-09-26 05:20:26

标签: vb.net system.drawing mousemove

在谷歌搜索失败后,我想在这里请求专家帮助我解决这个问题,因为这个网站总是帮助我。

我想要什么?

  

我想为我的某种绘图应用程序创建一个荧光笔。我希望这类似于您在Windows Snipping Tool上看到的荧光笔。

我的问题是什么?

  

问题是虽然我可以使用代码gfx.FillRectangle(New SolidBrush(Color.FromArgb(100, Colors.GreenYellow)), x, y, width, height)绘制半透明或不透明的矩形,但如果我绘制另一个矩形重叠任何先前的矩形,颜色会变暗并降低矩形的透明度它们重叠的地方。

代码:

Public Class Form1
    Dim drag As Boolean
    Dim mouseX, mouseY As Integer
    Dim prev As Point
    Dim initi As Point
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Dim grx As Graphics = Panel1.CreateGraphics
        grx.DrawString("+", New Font("Arial", 144, FontStyle.Regular), New SolidBrush(Color.FromArgb(100, Color.GreenYellow)), New Point(200, 200))
    End Sub

    Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel1.MouseDown
        drag = True
        mouseX = MousePosition.X - Me.Left - 8
        mouseY = MousePosition.Y - Me.Top - 34
        initi = New Point(mouseX, mouseY)
    End Sub

    Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel1.MouseMove
        If drag Then
            mouseX = MousePosition.X - Me.Left - 8
            mouseY = MousePosition.Y - Me.Top - 34
        End If
    End Sub

    Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
        drag = False
        prev = New Point(0, 0)
        Dim grx As Graphics = Panel1.CreateGraphics
        grx.FillRectangle(New SolidBrush(Color.FromArgb(100, Color.GreenYellow)), initi.X, initi.Y, (mouseX - initi.X), (mouseY - initi.Y))

    End Sub
End Class

应用程序的屏幕截图(显示问题

  • 左" +"是我想画的那个。
  • 对" +"是我画画时得到的那个。

enter image description here

2 个答案:

答案 0 :(得分:1)

我试过这个并且它有效:没有边框,你甚至无法区分不同的盒子: 我更改了以下内容:

New SolidBrush(Color.FromArgb(100, Color.GreenYellow)), New Point(200, 200))

进入这个:

New SolidBrush(Color.GreenYellow), New Point(200, 200))

两次:对于声明和Panel1_MouseDown 这是因为这通过defualt将Alpha设置为1也是不可变的,因此重叠不会改变任何颜色,图层或可见性。

当您要控制颜色的alpha时,您应该只使用FromArgb,但在这种情况下,您让计算机为您执行此操作

答案 1 :(得分:0)

好。我从来没有真正使用过图形,但我唯一想到的就是创建一个要绘制的高光列表,然后每次mouse_up事件触发时,创建一个新的位图,逐个像素地绘制每个矩形,半透明像素到位图,然后使用面板的绘制事件处理程序将生成的位图绘制到面板,该处理程序在刷新面板时触发。这似乎是.net自动执行的内置alpha混合。

将此添加到表单的变量声明

Dim highlightsList As New List(Of Rectangle)
Dim bmp1 As Bitmap

将您的form_shown事件更改为

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    bmp1 = New Bitmap(Panel1.Width, Panel1.Height)
End Sub 

添加此子句,处理向列表添加矩形和创建位图

Private Sub addRectangle(gr As Graphics, x As Integer, y As Integer, v1 As Integer, v2 As Integer)
    Dim newRectangle As New Rectangle(x, y, v1, v2)
    highlightsList.Add(newRectangle)
    Using G As Graphics = Graphics.FromImage(bmp1)
        G.Clear(Color.White)
    End Using
    bmp1.MakeTransparent(Color.White)
    For Each rect As Rectangle In highlightsList
        For i As Integer = rect.X To rect.X + rect.Width - 1
            For j As Integer = rect.Y To rect.Y + rect.Height - 1
                bmp1.SetPixel(i, j, Color.FromArgb(100, Color.GreenYellow))
            Next
        Next
    Next
    Panel1.Refresh()
End Sub

为面板的paint事件添加一个处理程序,以便在刷新面板时将位图绘制到其上

Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    e.Graphics.DrawImage(bmp1, 0, 0)
End Sub

并更改mouse_up事件以使用上面的子进行绘图

Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
    drag = False
    prev = New Point(0, 0)
    Dim grx As Graphics = Panel1.CreateGraphics
    'grx.Clear(Panel1.BackColor)
    addRectangle(grx, initi.X, initi.Y, (mouseX - initi.X), (mouseY - initi.Y))
End Sub

这似乎有效,但是如果您将该面板用于显示突出显示的其他任何内容,则可能无法按预期工作。