none
求教“ e 作为 PaintEventArgs 传递”的“e”问题 RRS feed

  • 问题

  • 页面http://msdn.microsoft.com/zh-cn/library/system.drawing.imaging.bitmapdata.aspx#Y0

    "代码示例演示如何将 BitmapData 类与 LockBits 和 UnlockBits 方法一起使用。 此示例是针对使用 Windows 窗体而设计的。 若要运行此示例,请将其粘贴到一个窗体中,然后通过调用 LockUnlockBitsExample 方法处理该窗体的 Paint 事件,并将 作为 PaintEventArgs 传递。 "

    代码中最后一句:

        ' Draw the modified image.
        e.Graphics.DrawImage(bmp, 0, 150)

     

    这里"e 作为 PaintEventArgs 传递",但是我不知怎么实现这段示例代码 ,也没搞清楚这e是怎么回事

    (初学,不好意思)

    ,在此求高手指点!
    2012年1月14日 14:07

答案

  • 这里的e就是类型为PaintEventArgs的变量

    Form会有Paint事件,该事件会传递PaintEventArgs参数

     

    所以您只要这样使用,把Form的PaintEventArgs的参数e传递給LockUnlockBitsExample函数即可

        Private Sub LockUnlockBitsExample(ByVal e As PaintEventArgs)
    
            ' Create a new bitmap.
            Dim bmp As New Bitmap("D:\test.bmp") '这里是本机某个路径上的图片
    
            ' Lock the bitmap's bits.  
            Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
            Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
                Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
    
            ' Get the address of the first line.
            Dim ptr As IntPtr = bmpData.Scan0
    
            ' Declare an array to hold the bytes of the bitmap.
            ' This code is specific to a bitmap with 24 bits per pixels.
            Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
            Dim rgbValues(bytes - 1) As Byte
    
            ' Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
    
            ' Set every third value to 255. A 24bpp image will look red.
            For counter As Integer = 2 To rgbValues.Length - 1 Step 3
                rgbValues(counter) = 255
            Next
    
            ' Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
    
            ' Unlock the bits.
            bmp.UnlockBits(bmpData)
    
            ' Draw the modified image.
            e.Graphics.DrawImage(bmp, 0, 150)
    
        End Sub
    
        Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Me.LockUnlockBitsExample(e)
        End Sub
    

     

     


    2012年1月14日 17:47

全部回复

  • 这里的e就是类型为PaintEventArgs的变量

    Form会有Paint事件,该事件会传递PaintEventArgs参数

     

    所以您只要这样使用,把Form的PaintEventArgs的参数e传递給LockUnlockBitsExample函数即可

        Private Sub LockUnlockBitsExample(ByVal e As PaintEventArgs)
    
            ' Create a new bitmap.
            Dim bmp As New Bitmap("D:\test.bmp") '这里是本机某个路径上的图片
    
            ' Lock the bitmap's bits.  
            Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
            Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
                Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
    
            ' Get the address of the first line.
            Dim ptr As IntPtr = bmpData.Scan0
    
            ' Declare an array to hold the bytes of the bitmap.
            ' This code is specific to a bitmap with 24 bits per pixels.
            Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
            Dim rgbValues(bytes - 1) As Byte
    
            ' Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
    
            ' Set every third value to 255. A 24bpp image will look red.
            For counter As Integer = 2 To rgbValues.Length - 1 Step 3
                rgbValues(counter) = 255
            Next
    
            ' Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
    
            ' Unlock the bits.
            bmp.UnlockBits(bmpData)
    
            ' Draw the modified image.
            e.Graphics.DrawImage(bmp, 0, 150)
    
        End Sub
    
        Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Me.LockUnlockBitsExample(e)
        End Sub
    

     

     


    2012年1月14日 17:47
  • 谢谢!但这个e还是不太明白——如果在picturebox中使用这个例程,又该如何呢?
    2012年1月15日 15:58
  • 將 Form1_Paint event 改為 picturebox1_Paint event
    歡迎參觀我的Blog.NET菜鳥自救會
    2012年1月16日 1:06
  • 非常感谢!

    2012年1月16日 9:18