面板下的位图图像 [英] Bit Map Images Under A Panel

查看:86
本文介绍了面板下的位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个图片框中绘制随机图案,该图片框的表面上有一个黑色面板,隐藏了下面的图形.但是,当我使面板消失时(panel.visible = false),面板下方图片框中的图形也会被删除.在下面的代码中(其中大部分是从VB10书籍中获取的),如果我在for private ... next循环中在Private Sub picCanvas_MouseMove中创建椭圆,则该过程将起作用.当单击隐藏面板"按钮时,随机椭圆图案将持续存在.但是,如果我在Private Sub MakeGraphics_Click(单击表单上的按钮)中创建椭圆图案,则当panel.visible = false时,面板下的椭圆图案将被擦除.
在我的程序中,不需要按下鼠标等事件来绘制波浪线,只需要在单击表单按钮时创建图形即可.
我可能缺少代表存储图像的bmp的某些信息,但是我不确定是什么.

感谢您的帮助!!!
加里五世

 公共  Class  Form1
     Dim  PenColor  As   New  Pen(颜色.红色, 2 )
     X  As   '  x,y,w,h设置椭圆的随机位置
     Dim  As  
     W  As  
     H  As  
    ' 我们将绘制的位图和图形对象.
    私有 m_Bitmap  As 位图
    私有 m_Graphics  As 图形
    ' 用于涂鸦.
    私有 m_Drawing  As  布尔值
    私有 m_X  As  整数
    私有 m_Y  As  整数

    ' 制作初始空白图片.
    私有  Form1_Load()句柄  MyBase .加载
        致电 MakeNewBitmap()
    结束 

    ' 制作新的空白图片.
    私有  mnuFileClear_Click()句柄 mnuFileClear.点击
        致电 MakeNewBitmap()
    结束 

    ' 制作新的位图以适合画布.
    私有  MakeNewBitmap()
        ' 获取工程图表面的大小.
         Dim  wid  As  整数 = picCanvas.ClientSize .宽度
         Dim  hgt  As  整数 = picCanvas.ClientSize .高度

        ' 制作适合的位图和图形.
        m_Bitmap = 位图(wid,hgt)
        m_Graphics = Graphics.FromImage(m_Bitmap)

        ' 清除绘图区域.
        m_Graphics.Clear( .BackColor)

        ' 显示结果.
        picCanvas.Image = m_Bitmap
    结束 

    ' 开始涂鸦.
    私有  picCanvas_MouseDown( ByVal 发​​件人目标 对象 ByVal  e  As  System.Windows.Forms.MouseEventArgs)
        m_X = e.X
        m_Y = e.Y
      
    结束 

    ' 继续进行涂鸦.
    私有  picCanvas_MouseMove(> ByVal 发​​件人目标 对象 ByVal  e  As  System.Windows.Forms.MouseEventArgs)如果 不是 m_Drawing 然后 退出  Sub 

        ' 出于测试目的:在MouseMove屏幕上随机画出一个椭圆形

        对于 Temp =  1  收件人  8 
            Randomize()
            X =  5  +(Rnd()*(m_Bitmap.Width- 30 ))
            Y =  5  +(Rnd()*(m_Bitmap.Height- 30 ))
            H =  2  +(Rnd()*  20 )
            W =  2  +(Rnd()*  20 )
            m_Graphics.DrawEllipse(PenColor,X,Y,W,H)
        下一步

        m_Graphics.DrawLine(笔黑,m_X,m_Y,e.X,e.Y)
        m_X = e.X
        m_Y = e.Y
        ' 显示结果.
        picCanvas.Refresh()
    结束 

' 停止涂鸦.
    私有  picCanvas_MouseUp( ByVal 发​​件人目标 对象 ByVal  e  As  System.Windows.Forms.MouseEventArgs)错误
    结束 

    私有  MakeGraphics_Click( ByVal 发​​件人 As 系统.对象 ByVal  e  As  System.EventArgs) Handles  MakeGraphics.Click
        ' 单击按钮时,在屏幕上制作一些随机的椭圆形
        m_Graphics = picCanvas.CreateGraphics
        对于 Temp =  1    8 
            Randomize()
            X =  5  +(Rnd()*(picCanvas.Width- 30 ))
            Y =  5  +(Rnd()*(picCanvas.Height- 30 ))
            ' 最小长度,宽度= 20,最大= 220 
            H =  2  +(Rnd()*  20 )
            W =  2  +(Rnd()*  20 )
            m_Graphics.DrawEllipse(PenColor,X,Y,W,H)
        下一步
    结束 

    私有  HidePanel_Click( ByVal 发​​件人 As 系统.对象 ByVal  e  As  System.EventArgs) Handles  HidePanel.Click
        如果 Panel1.Visible = 真实 然后 Panel1 .Visible = 错误 其他 Panel1.Visible = 真跨度>
    结束 
结束  

解决方案

我能发现的唯一区别是,在按钮单击处理程序中,您未使用已初始化的m_Grphics Graphics上下文,而是使用m_Graphics = picCanvas.CreateGraphics创建一个新的上下文.由于您没有在MouseMove事件处理程序中执行此操作,因此您说它会产生所需的结果,所以我怀疑这是问题所在.

最好的问候,
Manfred


为了进一步说明,"creategraphics"是魔鬼的作品.它创建一个绘图表面,该表面将在下一次使表单无效时消失.您应该始终在绘画事件中进行绘画,除非您要像橡皮筋那样绘画,即突出显示所选内容或其他绘画,其中绘画的目的是不稳定并且可能会消失.

I am trying to draw random patterns in a picture box that has a black panel over the surface hiding the drawing below. But when I make the panel disappear (panel.visible = false), the graphics in the picture box UNDER the panel are also erased. In the code below (most of which was grabbed from a VB10 book) the process works if I put the for...next loop creating the ellipses in the Private Sub picCanvas_MouseMove. The random ellipse patterns persist when the Hide Panel Button is clicked. But if I create the ellipse patterns in the Private Sub MakeGraphics_Click (click a button on the form), the ellipse patterns under the panel are erased when the panel.visible = false.

In my program I do not need the mouse down, etc events to draw squiggly lines, I only need to create the graphics when a form button is clicked.
I am probably missing something with representing the bmp where the images are stored but I am not sure what.

Thanks for any help!!!
Gary V

Public Class Form1
    Dim PenColor As New Pen(Color.Red, 2)
    Dim X As Long  ' x,y,w,h set the random position of the ellipses
    Dim Y As Long
    Dim W As Long
    Dim H As Long
    ' The Bitmap and Graphics objects we will draw on.
    Private m_Bitmap As Bitmap
    Private m_Graphics As Graphics
    ' Used for scribbling.
    Private m_Drawing As Boolean
    Private m_X As Integer
    Private m_Y As Integer

    ' Make the initial blank image.
    Private Sub Form1_Load() Handles MyBase.Load
        Call MakeNewBitmap()
    End Sub

    ' Make a new blank image.
    Private Sub mnuFileClear_Click() Handles mnuFileClear.Click
        Call MakeNewBitmap()
    End Sub

    ' Make a new Bitmap to fit the canvas.
    Private Sub MakeNewBitmap()
        ' Get the drawing surface's size.
        Dim wid As Integer = picCanvas.ClientSize.Width
        Dim hgt As Integer = picCanvas.ClientSize.Height

        ' Make a Bitmap and Graphics to fit.
        m_Bitmap = New Bitmap(wid, hgt)
        m_Graphics = Graphics.FromImage(m_Bitmap)

        ' Clear the drawing area.
        m_Graphics.Clear(Me.BackColor)

        ' Display the result.
        picCanvas.Image = m_Bitmap
    End Sub

    ' Start scribbling.
    Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
        m_Drawing = True
        m_X = e.X
        m_Y = e.Y
      
    End Sub

    ' Continue scribbling.
    Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
        If Not m_Drawing Then Exit Sub

        'For testing Purposes: Make some random elipses on screen on MouseMove

        For Temp = 1 To 8
            Randomize()
            X = 5 + (Rnd() * (m_Bitmap.Width - 30))
            Y = 5 + (Rnd() * (m_Bitmap.Height - 30))
            H = 2 + (Rnd() * 20)
            W = 2 + (Rnd() * 20)
            m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
        Next

        m_Graphics.DrawLine(Pens.Black, m_X, m_Y, e.X, e.Y)
        m_X = e.X
        m_Y = e.Y
        ' Display the result.
        picCanvas.Refresh()
    End Sub

' Stop scribbling.
    Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
        m_Drawing = False
    End Sub

    Private Sub MakeGraphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeGraphics.Click
        'Make some random ellipses on screen when button is clicked
        m_Graphics = picCanvas.CreateGraphics
        For Temp = 1 To 8
            Randomize()
            X = 5 + (Rnd() * (picCanvas.Width - 30))
            Y = 5 + (Rnd() * (picCanvas.Height - 30))
            'Minimum length,width = 20, max = 220
            H = 2 + (Rnd() * 20)
            W = 2 + (Rnd() * 20)
            m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
        Next
    End Sub

    Private Sub HidePanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HidePanel.Click
        If Panel1.Visible = True Then Panel1.Visible = False Else Panel1.Visible = True
    End Sub
End Class

解决方案

The only difference I could spot is that in your button click handler you''re not using the already initialized m_Grphics Graphics context but are creating a new one with m_Graphics = picCanvas.CreateGraphics. Since you didn''t do that in your MouseMove event handler which you said yields the desired results I suspect that this is the issue.

Best Regards,
Manfred


To clarify further, ''creategraphics'' is the work of the devil. It creates a drawing surface which will disappear the next time the form is invalidated. You should always draw in your paint event, unless you''re drawing somethign like a rubber band, that is, highlighting a selection or somethign else where what you''re drawing is designed to be unstable and likely to disappear.


这篇关于面板下的位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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