Zeichen auf einer Winform ohne Bitmap

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Bluespide.

    Zeichen auf einer Winform ohne Bitmap

    Hallo,

    ich bin gerade dabei mich zu fragen, ob es möglich ist in die Winform zu zeichnen ohne die Bitmap und ohne die methode Graphic.Draw.... ein bild in der Winform darzustellen
    also Byte Array nehmen, rüberschrieben und Updaten :D

    Weis jemand wie das geht zufällig, denn ich bin auf noch nichts brauchbares gestoßen, denn meistens wird ein Bild geladen und das dann zeichnet :/ ?

    Edit also sowas hier ohne Bitmap ;D docs.microsoft.com/en-us/cpp/d…ions-cpp-cli?view=vs-2017
    @Facebamm Ohne eine Bitmap-Instanz geht das nicht, denn Du musst ja sagen, wo das bunte Pixel hin soll.
    Mit Bitmap.LockBit() und Co kannst Du allerdings sehr viel schneller die Pixel verändern als mit Bitmap.SetPixel().
    Was ist denn Dein Plan?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Wenn du eine Byte-Array mit RGB Daten direkt zeichnen möchtest, dann kannst du das mit StretchDIBits machen. Beispielsweise so, du musst nur die statischen Zahlen anpassen:

    C#-Quellcode

    1. ​[DllImport("user32.dll")]
    2. public static extern IntPtr GetDC(IntPtr hWnd);
    3. [DllImport("user32.dll")]
    4. public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
    5. [DllImport("gdi32.dll")]
    6. public static extern int StretchDIBits(IntPtr hdc, int xDest, int yDest, int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, byte[] lpBits, ref BitmapInfo lpBitsInfo, DibColorMode iUsage, TernaryRasterOperations dwRop);
    7. byte[] myPixelData = new byte[3600];
    8. [StructLayout(LayoutKind.Sequential, Pack = 1)]
    9. public struct BitmapInfo {
    10. public uint Size;
    11. public int Width;
    12. public int Height;
    13. public ushort Planes;
    14. public ushort BitCount;
    15. public BitmapInfoCompression Compression;
    16. public uint SizeImage;
    17. public int XPelsPerMeter;
    18. public int YPelsPerMeter;
    19. public int ClrUsed;
    20. public int ClrImportant;
    21. public byte RgbBlue;
    22. public byte RgbGreen;
    23. public byte RgbRed;
    24. public byte RgbReserved;
    25. }
    26. public enum BitmapInfoCompression : uint {
    27. Rgb = 0x00000000,
    28. Rle8 = 0x00000001,
    29. Rle4 = 0x00000002,
    30. BitFields = 0x00000003,
    31. Jpeg = 0x00000004,
    32. Png = 0x00000005
    33. }
    34. public enum DibColorMode : uint {
    35. RGBColors = 0x00000000, /* color table in RGBs */
    36. PALColors = 0x00000001 /* color table in palette indices */
    37. }
    38. public enum TernaryRasterOperations : uint {
    39. SrcCopy = 0x00CC0020, /* dest = source */
    40. SrcPaint = 0x00EE0086, /* dest = source OR dest */
    41. SrcAnd = 0x008800C6, /* dest = source AND dest */
    42. SrcInvert = 0x00660046, /* dest = source XOR dest */
    43. SrcErase = 0x00440328, /* dest = source AND (NOT dest) */
    44. NotSrcCopy = 0x00330008, /* dest = (NOT source) */
    45. NotSrcErase = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
    46. MergeCopy = 0x00C000CA, /* dest = (source AND pattern) */
    47. MergePaint = 0x00BB0226, /* dest = (NOT source) OR dest */
    48. PatCopy = 0x00F00021, /* dest = pattern */
    49. PatPaint = 0x00FB0A09, /* dest = DPSnoo */
    50. PatInvert = 0x005A0049, /* dest = pattern XOR dest */
    51. DstInvert = 0x00550009, /* dest = (NOT dest) */
    52. Blackness = 0x00000042, /* dest = BLACK */
    53. Whiteness = 0x00FF0062, /* dest = WHITE */
    54. CaptureBlt = 0x40000000 /* Capture window as seen on screen. This includes layered windows such as WPF windows with AllowsTransparency="true" */
    55. }
    56. private void Draw() {
    57. IntPtr myHwnd = Process.GetCurrentProcess().MainWindowHandle;
    58. IntPtr myDC = GetDC(myHwnd);
    59. //Layout der PixelDaten im Byte-Array
    60. BitmapInfo bmi = new BitmapInfo() { Size = 40, Width = 60 , Height = -60 , Planes = 1, BitCount = 24, Compression = BitmapInfoCompression.Rgb };
    61. StretchDIBits(myDC, 0, 0, 60, 60, 0, 0, 60, 60, this.myPixelData, ref bmi, DibColorMode.RGBColors, TernaryRasterOperations.SrcCopy);
    62. ReleaseDC(myHwnd, myDC);
    63. }