2014-01-20 26 views
2

正如我所研究的。 Graphics.DrawImage()可以替換PaintPicture()從VB6升級到VB.Net時。 但是,DrawImage()缺少像操作碼參數,它定義了按位操作。 請幫我找到替換的正確解決方案。 謝謝。VB.NET中的VB6 PaintPicture()方法的等價值?

+0

我們不能幫你找到正確的解決方案,如果你不告訴我們你用什麼碼。在Graphics.CompositingMode尚未完成的情況下,您將不得不鎖住BitBlt()。這在20年前停止了。 –

+0

我需要從PaintPicture()中使用所有可用的操作碼...謝謝 – Nick

回答

0

這似乎你必須自己動手。
如果有人知道管理方式,請發表其他答案。

創建一個單獨的模塊:

Module PaintPicture 

    <System.Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=False)> _ 
    Private Function StretchBlt(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, ByVal xSrc As Long, ByVal ySrc As Integer, ByVal nSrcWidth As Integer, ByVal nSrcHeight As Integer, ByVal dwRop As Integer) As Integer 
    End Function 

    <System.Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=False)> _ 
    Private Function SelectObject(ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr 
    End Function 

    <System.Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=False)> _ 
    Private Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr 
    End Function 

    <System.Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=False)> _ 
    Private Function DeleteDC(ByVal hdc As IntPtr) As Integer 
    End Function 

    <System.Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=False)> _ 
    Private Function DeleteObject(ByVal hObject As IntPtr) As Integer 
    End Function 


    Private Class DCHandle 
     Inherits Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid 

     Public Sub New(ByVal hDC As IntPtr) 
      MyBase.New(True) 

      Me.SetHandle(hDC) 
     End Sub 

     Protected Overrides Function ReleaseHandle() As Boolean 
      Return DeleteDC(Me.handle) <> 0 
     End Function 
    End Class 

    Private Class BitmapHandle 
     Inherits Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid 

     Public Sub New(ByVal hBitmap As IntPtr) 
      MyBase.New(True) 

      Me.SetHandle(hBitmap) 
     End Sub 

     Protected Overrides Function ReleaseHandle() As Boolean 
      Return DeleteObject(Me.handle) <> 0 
     End Function 
    End Class 


    Public Enum Opcodes As Integer 
     vbDstInvert = &H550009 
     vbMergeCopy = &HC000CA 
     vbMergePaint = &HBB0226 
     vbNotSrcCopy = &H330008 
     vbNotSrcErase = &H1100A6 
     vbPatCopy = &HF00021 
     vbPatInvert = &H5A0049 
     vbPatPaint = &HFB0A09 
     vbSrcAnd = &H8800C6 
     vbSrcCopy = &HCC0020 
     vbSrcErase = &H440328 
     vbSrcInvert = &H660046 
     vbSrcPaint = &HEE0086 
    End Enum 


    <System.Runtime.CompilerServices.Extension()> _ 
    Public Sub PaintPicture(ByVal g As Graphics, ByVal Picture As System.Drawing.Bitmap, ByVal X1 As Integer, ByVal Y1 As Integer, Optional ByVal Width1 As Integer = 0, Optional ByVal Height1 As Integer = 0, Optional ByVal X2 As Integer = 0, Optional ByVal Y2 As Integer = 0, Optional ByVal Width2 As Integer = 0, Optional ByVal Height2 As Integer = 0, Optional ByVal Opcode As Opcodes = Opcodes.vbSrcCopy) 

     Dim hDCTarget As IntPtr = g.GetHdc() 

     Try 
      Using hDCSource = New DCHandle(CreateCompatibleDC(hDCTarget)) 
       If hDCSource.IsInvalid Then Throw New Exception("Failed to create compatible DC") 

       Using hBitmap = New BitmapHandle(Picture.GetHbitmap()) 
        Dim prev As IntPtr = SelectObject(hDCSource.DangerousGetHandle(), hBitmap.DangerousGetHandle()) 

        Try 
         If prev = IntPtr.Zero Then Throw New Exception("Failed to select bitmap into the compatible DC") 

         If StretchBlt(hDCTarget, X1, Y1, If(Width1 = 0, Picture.Width, Width1), If(Height1 = 0, Picture.Height, Height1), hDCSource.DangerousGetHandle(), X2, Y2, If(Width2 = 0, Picture.Width, Width2), If(Height2 = 0, Picture.Height, Height2), Opcode) = 0 Then 
          Throw New Exception("Failed to draw picture") 
         End If 
        Finally 
         SelectObject(hDCSource.DangerousGetHandle(), prev) 
        End Try 

       End Using 
      End Using 
     Finally 
      g.ReleaseHdc(hDCTarget) 
     End Try 

    End Sub 

End Module 

然後使用它:

Using p = New Drawing.Bitmap("picture.jpg") 
    Using g = Me.CreateGraphics() 

     g.PaintPicture(p, 10, 10, Opcode:=Opcodes.vbSrcInvert) 

    End Using 
End Using