none
VB.Net 2008 如何以颜色填充 RRS feed

  • 问题

  • 已知PICTUREBOX内一个封闭区域内的一个点,坐标是(X,Y),如何以某一种颜色填充这个区域
    我用的是下面的方法:
    建立一个窗体,在其中加一个PICTUREBOX1,加如下代码

        Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Integer) As Integer
        Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer
        Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal crColor As Integer, ByVal wFillType As Integer) As Integer
        Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal X As Integer, ByVal Y As Integer) As Integer

     


      Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picturebox1.MouseDown
           
            Dim g As Graphics = Me.Picturebox1.CreateGraphics()
            Dim mBrush As Integer
            mBrush = CreateSolidBrush(QBColor(3))
            SelectObject(g.GetHdc, mBrush)
            g.ReleaseHdc()
            Dim a As Integer
            a = GetPixel(g.GetHdc, e.X, e.Y)
            g.ReleaseHdc()
            ExtFloodFill(g.GetHdc, e.X, e.Y, a, 1)
            g.ReleaseHdc()
        End Sub

    在PICTUREBOX1中放置一个图片,运行以上程序,不管QBColor(3)是如何变化,都是以白色填充,速度倒是不慢,请大家看看,有什么地方不对

    微软也没有说在net中不好用API呀,可这样用为何就得不到结果呢,这个老盖!!!
    如果不用API,那么有什么好的方法么,建议速度不能太慢了

    2008年6月5日 9:09

答案

  • Code Snippet

     

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            Dim g As Graphics = Me.PictureBox1.CreateGraphics()
            Dim mBrush As Integer

            Dim hdc As Integer

            hdc = g.GetHdc
            mBrush = CreateSolidBrush(QBColor(3))
            SelectObject(hdc, mBrush)

            Dim a As Integer

            a = GetPixel(hdc, e.X, e.Y)

            ExtFloodFill(hdc, e.X, e.Y, a, 1)
            g.ReleaseHdc()

        End Sub

     

     

    2008年6月10日 1:59
    版主

全部回复

  • 我建议你在  .NET  中还是用  .NET  里面的各种方法吧

    2008里面是原生支持 solidbrush  的。

     

    关于你的这个应用我建议你看看《 application=code + markup》的第三章(如果我没有记错的话。就是将brush的那一章)

    2008年6月7日 15:19
  • Code Snippet

     

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            Dim g As Graphics = Me.PictureBox1.CreateGraphics()
            Dim mBrush As Integer

            Dim hdc As Integer

            hdc = g.GetHdc
            mBrush = CreateSolidBrush(QBColor(3))
            SelectObject(hdc, mBrush)

            Dim a As Integer

            a = GetPixel(hdc, e.X, e.Y)

            ExtFloodFill(hdc, e.X, e.Y, a, 1)
            g.ReleaseHdc()

        End Sub

     

     

    2008年6月10日 1:59
    版主