none
图片清除问题 RRS feed

  • 问题

  • 我现在有一个bitmap,是透明的bitmap。现在我在上面画了几个图,想实现擦出(橡皮擦)功能,不过透明的不知道怎么实现。clear方法用透明的话,会留下以前画的图。根本实现不了擦出。。。。。我想擦出以后把picturebox backgrandimage也显示出来。。。这个怎么实现
    muradil
    2011年9月10日 2:45

答案

  • 试试这个代码:

    Public Class ErasePic
        Private Sub ErasePic_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.PictureBox1.Image = Image.FromFile("D:\file\red.jpg")
            Me.PictureBox1.BackgroundImage = Image.FromFile("D:\file\2WQRP0hLHtZDNGVFKPaNQA.jpg")
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Using g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
                    g.DrawImage(Me.PictureBox1.BackgroundImage, New Rectangle(e.X, e.Y, 10, 10), New Rectangle(e.X, e.Y, 10, 10), GraphicsUnit.Pixel)
                    Me.PictureBox1.Invalidate()
                End Using
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.PictureBox1.Image.Save("D:\file\red1.jpg")
        End Sub
    End Class
    

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年9月13日 5:51
    版主

全部回复

  • Hi

    看看这个帖子, 其中有代码, 你应该可以从中获取一些思路的

    http://www.devdiv.com/thread-32158-1-1.html

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年9月12日 5:53
    版主
  • Public Class Form1
        Public Declare Function CreatePen Lib "gdi32" Alias "CreatePen" (ByVal nPenStyle As Integer, ByVal nWidth As Integer, ByVal crColor As Integer) As Integer
        Public Declare Function CreatePatternBrush Lib "gdi32" Alias "CreatePatternBrush" (ByVal hBitmap As Integer) As Integer
        Public Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer
        Public Declare Function DeleteObject Lib "gdi32" Alias "DeleteObject" (ByVal hObject As Integer) As Integer
        Public Declare Function Ellipse Lib "gdi32" Alias "Ellipse" (ByVal hdc As Integer, ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) As Integer
    
        Private Const NULL_PEN As Integer = 5
        Private ScreenGraphics As Graphics
        Private BufferGraphics As Graphics
        Private imgOffscreen As Bitmap
        Dim IsDown As Boolean = False
        Dim bmp As Bitmap
        Private g_hBrush As IntPtr
        Private g_hPen As IntPtr
        Dim g_hdc As IntPtr
    
        Dim g_oldp As IntPtr
        Dim g_oldb As IntPtr
    
        Public Sub New()
    
            ' 此调用是设计器所必需的。
            InitializeComponent()
    
            ' 在 InitializeComponent() 调用之后添加任何初始化。
            bmp = New Bitmap("G:/毕业照/毕业照/PivotClearAll.bmp")
            Dim ibmp As Integer = bmp.GetHbitmap().ToInt32
            g_hBrush = CreatePatternBrush(ibmp)
            ScreenGraphics = Me.CreateGraphics
            imgOffscreen = New Bitmap(240, 320)
            BufferGraphics = Graphics.FromImage(imgOffscreen)
            BufferGraphics.FillRectangle(New SolidBrush(Color.White), New Rectangle(0, 0, 240, 320))
            g_hdc = BufferGraphics.GetHdc
            g_hPen = CreatePen(NULL_PEN, 1, 0)
            Dim oldp As IntPtr = SelectObject(g_hdc, g_hPen)
            Dim oldb As IntPtr = SelectObject(g_hdc, g_hBrush)
    
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            ScreenGraphics.DrawImage(imgOffscreen, 0, 0)
    
        End Sub
    
        Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
            IsDown = True
        End Sub
    
       
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            If IsDown = True Then
                Ellipse(g_hdc, e.X - 10, e.Y - 10, e.X + 10, e.Y + 10)
                '  Me.Invalidate()
            End If
    
        End Sub
    
        Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
            IsDown = False
        End Sub
    End Class
    

    麻烦你看一下,没反应啊。。。谢谢。。。
    muradil
    2011年9月12日 15:19
  • Hi ,

    用这个代码试试:

    Public Class ErasePic
        Private Sub ErasePic_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.PictureBox1.Image = Image.FromFile("D:\file\red.jpg")
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Using g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
                    g.FillEllipse(Brushes.White, e.X, e.Y, 10, 10)
                    Me.PictureBox1.Invalidate()
                End Using
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.PictureBox1.Image.Save("D:\file\red1.jpg")
        End Sub
    End Class
    
    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年9月13日 4:02
    版主
  • 这个不行。。。如果用画笔的话picturebox backgrandimage被遮住。。。我要的是透明的。。。。
    muradil
    2011年9月13日 4:40
  • 试试这个代码:

    Public Class ErasePic
        Private Sub ErasePic_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.PictureBox1.Image = Image.FromFile("D:\file\red.jpg")
            Me.PictureBox1.BackgroundImage = Image.FromFile("D:\file\2WQRP0hLHtZDNGVFKPaNQA.jpg")
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Using g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
                    g.DrawImage(Me.PictureBox1.BackgroundImage, New Rectangle(e.X, e.Y, 10, 10), New Rectangle(e.X, e.Y, 10, 10), GraphicsUnit.Pixel)
                    Me.PictureBox1.Invalidate()
                End Using
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.PictureBox1.Image.Save("D:\file\red1.jpg")
        End Sub
    End Class
    

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年9月13日 5:51
    版主