使用橡皮带矩形突出显示部分图片框图像 [英] Highlight part of a picturebox image using a rubberband rectangle

查看:81
本文介绍了使用橡皮带矩形突出显示部分图片框图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧

我不确定这是不是最好的方式,但是到目前为止我已经完成了。我创建了一个橡皮筋矩形并使用鼠标我想要突出显示我的桌面图像的图片框区域。突出显示应该是透明的,这样您仍然可以看到它背后的图像。

它有点有效。如果我快速拖动鼠标,则突出显示更加透明。如果我慢慢走,那不是。我看不到它背后的图像。我认为它在任何鼠标速度下都是均匀的。



继承我鼠标动作的代码。

HI everyone
I'm not sure if this is the best way to go, but heres what I've done so far. I created a rubberband rectangle and using a mouse I want to highlight an area of a picturebox which is displaying my desktop image. The highlight should be transparent so you can still see the image behind it.
It kinda works. If I drag my mouse quickly the highlight is more transparent. If I go slowly it isnt. I cant see the image behind it. I thought it would be uniform at any mouse speed.

Heres the code of my mouse movements.

Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown

        '*** This is for working on main desktop image
        ' Save the point's coordinates.
        m_X1 = e.X
        m_X2 = e.X
        m_Y1 = e.Y
        m_Y2 = e.Y

        If varDrawOnOff = "DrawOn" Then
            _Previous = e.Location
            '  pictureBox1_MouseMove(sender, e)
        End If

        If varEraserOnOff = "EraserOn" Then
            _Previous = e.Location
            ' pictureBox1_MouseMove(sender, e)
        End If

        If varHighlightOnOff = "HighlightOn" Then
            
            _Previous = e.Location

        End If


    End Sub

    Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove

        '*** This is for working on main desktop image
        Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 5) 'for drawing

        Dim yellowPen As New Pen(Color.FromArgb(255, 255, 102, 0), 5) 'for highlight

        Dim whitePen As New Pen(Color.FromArgb(255, 255, 255, 255), 10) 'for eraser

        If _Previous IsNot Nothing Then
            If PictureBox1.Image Is Nothing Then
                Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
                Using g As Graphics = Graphics.FromImage(bmp)
                    g.Clear(Color.White)
                End Using
                PictureBox1.Image = bmp
            End If

            'determine which button was selected either draw or highlight
            If varDrawOnOff = "DrawOn" Then
                Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
                    ' g.DrawLine(Pens.Red, _Previous.Value, e.Location)
                    g.DrawLine(greenPen, _Previous.Value, e.Location)

                End Using
                PictureBox1.Invalidate()
                _Previous = e.Location
            End If

            If varEraserOnOff = "EraserOn" Then
                Using g As Graphics = Graphics.FromImage(PictureBox1.Image)

                    g.DrawLine(whitePen, _Previous.Value, e.Location)

                End Using
                PictureBox1.Invalidate()
                _Previous = e.Location
            End If

            'needs work
            If varHighlightOnOff = "HighlightOn" Then

                Dim semiTransBrush As New SolidBrush(Color.FromArgb(3, 255, 255, 0))


                Using g As Graphics = Graphics.FromImage(PictureBox1.Image)


                    '*******************************
                    '1st point is transparency, 0 is most, 255 is least
                    '2nd is red, 3rd is green, 4th is blue
                    '255, 255, 0 is yellow

                    m_X2 = e.X
                    m_Y2 = e.Y


                    'Draw the rubberband rectangle.
                    Dim rect As New Rectangle( _
                        Min(m_X1, m_X2), Min(m_Y1, m_Y2), _
                        Abs(m_X1 - m_X2), Abs(m_Y1 - m_Y2))

                   'if i go slow colour is too deep, not transparent
                    g.FillRectangle(semiTransBrush, rect)

                End Using
                PictureBox1.Invalidate()
                _Previous = e.Location

            End If


        End If
    End Sub

    Private Sub pictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp



        'this stops the drawing
        _Previous = Nothing

        

End Sub





只要忽略对绘图和橡皮擦的引用,它们就可以正常工作。还是有更好的方法吗?



谢谢

H



我尝试了什么:



我已经尝试了几个网站,我正在经历的行为但到目前为止没有运气



Just ignore the references to draw and eraser, they work fine. Or is there a better way?

Thank you
H

What I have tried:

I've tried several websites as to the behaviour I'm experiencing but no luck so far

推荐答案

使用 PictireBox 是一个坏主意。这是一个控制渲染图像,而不是图像;它纯粹是多余的(你可以做同样的事情)并且过于冗余而不能用于除静态图像之外的任何东西。对于任何更复杂的事情,例如,交互式(您的案例),动画等,它不提供任何帮助,只使用额外的资源并有助于浪费您的开发时间。滥用此控件是一个典型的错误。请看我过去的答案:

如何从旧图纸中清除面板 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



在这些答案中,我解释了你要做的事情:创建一个图形模型,在输入时更新它事件,并在其上面使用rubberband元素渲染图像(在自定义控件中使用透明整体的半透明背景或更高级的东西),使用无效为控件提供更改通知的方法。



有关图形渲染的更多信息,请参阅我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加快我的vb.net应用程序? [ ^ ],

另见如何避免DatagridView C#中的红十字[ [ ^ ]。



-SA
Using PictireBox is a bad idea. This is a control rendering image, not image; it is purely redundant (you can do the same thing) and is too redundant to use it for anything but static images. For anything more complicated, say, interactive (your case), animated, etc., it does not provide any help, only uses extra resources and helps to waste your development time. Misusing this control is a typical mistake. Please see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

In those answers, I explained what you have to do: create a graphic model, update it on your input events, and render image with rubberband element on top of it (it's good to use semi-transparent background with a transparent whole, or something more advanced) in a custom control, use Invalidate method for giving the control your notifications on the change.

For more on graphics rendering, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
see also How to avoid Red Cross in DatagridView C#[^].

—SA


这篇关于使用橡皮带矩形突出显示部分图片框图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆