Graphics zu Bitmap

    • VB.NET

      Graphics zu Bitmap

      Hi
      Das .NET Framework bietet ja keine Möglichkeit, Images aus Graphics zu erstellen. Hier mal eine Möglichkeit (recht unschön aber wirkunsvoll):

      VB.NET-Quellcode

      1. Imports System.Runtime.InteropServices
      2. '...
      3. <DllImport("gdi32.DLL", EntryPoint:="BitBlt", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      4. Private Shared Function BitBlt(ByVal Intptr1 As IntPtr, ByVal Left As Integer, ByVal Top As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal Intptr2 As IntPtr, _
      5. ByVal int1 As Integer, ByVal int2 As Integer, ByVal int3 As Integer) As Boolean
      6. End Function
      7. Private Function CreateImageFromGraphics(ByVal Graphics As Graphics, ByVal Bounds As Rectangle) As Image
      8. Dim graph As Graphics = Nothing
      9. Dim dc1 As IntPtr = IntPtr.Zero
      10. Dim dc2 As IntPtr = IntPtr.Zero
      11. Dim img As Bitmap
      12. img = New Bitmap(Bounds.Width, Bounds.Height, Graphics)
      13. graph = Graphics.FromImage(img)
      14. dc1 = Graphics.GetHdc()
      15. dc2 = graph.GetHdc()
      16. BitBlt(dc2, 0, 0, Bounds.Width, Bounds.Height, dc1, 0, 0, 13369376)
      17. Graphics.ReleaseHdc(dc1)
      18. graph.ReleaseHdc(dc2)
      19. Return img
      20. End Function

      Edit: Fehler behoben. Sry, ist mir erst jetzt aufgefallen...

      Gruß
      ~blaze~

      Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „~blaze~“ ()