热门IT资讯网

C#使用GDI中的API函数

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,我们知道在.NET 平台下主要是用GDI+来进行图形图像处理,在效率要求不高的情况下使用GDI+已经足够实现各种功能了,但一旦要求效率的情况下,我们可以考虑使用GDI来代替GDI+,网上有人士做过相关

我们知道在.NET 平台下主要是用GDI+来进行图形图像处理,在效率要求不高的情况下使用GDI+已经足够实现各种功能了,但一旦要求效率的情况下,我们可以考虑使用GDI来代替GDI+,网上有人士做过相关测试(本人也测试过),GDI在图形、图像绘制方面效率较GDI+有很大提高。下面将自己开发过程中整理到的NativeGdi32Api类贴出来:

  1. public static class NativeGdi32Api
  2. {
  3. [ DllImport("gdi32.dll" )]
  4. public static extern int SetDIBits (IntPtr hdc, IntPtr hBitmap, int nStartScan , int nNumScans, IntPtr lpBits, IntPtr lpBI , int wUsage);
  5. [ DllImport("gdi32.dll" )]
  6. public static extern int SetDIBitsToDevice (IntPtr hdc, int x, int y , int dx, int dy, int SrcX, int SrcY, int Scan , int NumScans, IntPtr Bits, IntPtr BitsInfo , int wUsage);
  7. [ DllImport("gdi32.dll" )]
  8. public static extern IntPtr CreateDIBSection (IntPtr hdc, IntPtr pBitmapInfo, int un , IntPtr lplpVoid , IntPtr handle, int dw);
  9. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto )]
  10. public static extern int SetPixel (IntPtr hdc, int x, int y , int crColor);
  11. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto )]
  12. public static extern int GetPixel (IntPtr hdc, int x, int y );
  13. [ DllImport("gdi32.dll" , CharSet= CharSet.Auto )]
  14. public static extern int CombineRgn (IntPtr dest, IntPtr src1, IntPtr src2 , int flags);
  15. [ DllImport("gdi32.dll" , CharSet= CharSet.Auto )]
  16. public static extern IntPtr CreateBrushIndirect (ref LOGBRUSH brush );
  17. [ DllImport("gdi32.dll" , CharSet= CharSet.Auto )]
  18. public static extern IntPtr CreateRectRgnIndirect (ref RECTAPI rect );
  19. [ DllImport("gdi32.dll" , CharSet= CharSet.Auto )]
  20. public static extern int GetClipBox (IntPtr hDC, ref RECTAPI rectBox);
  21. [ DllImport("gdi32.dll" , CharSet= CharSet.Auto )]
  22. public static extern bool PatBlt (IntPtr hDC, int x, int y , int width, int height, uint flags);
  23. [ DllImport("gdi32.dll" , CharSet= CharSet.Auto )]
  24. public static extern int SelectClipRgn (IntPtr hDC, IntPtr hRgn);
  25. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  26. public static extern IntPtr MoveToEx (IntPtr hDC, int x, int y , ref POINTAPI lpPoint );
  27. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  28. public static extern IntPtr LineTo (IntPtr hDC, int x, int y );
  29. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  30. public static extern IntPtr CreatePen (int nPenStyle, int nWidth, int crColor );
  31. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  32. public static extern int SetBrushOrgEx (IntPtr hDC, int x, int y , ref POINTAPI p );
  33. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  34. public static extern IntPtr CreatePatternBrush (IntPtr hBMP);
  35. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  36. public static extern int GetTextFace (IntPtr hDC, int nCount, string lpFacename );
  37. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  38. public static extern int GetTextMetrics (IntPtr hDC, ref GDITextMetric TextMetric);
  39. [ DllImport("gdi32.dll" , CharSet = CharSet.Ansi , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  40. public static extern IntPtr CreateFontIndirect ([MarshalAs( UnmanagedType.LPStruct )]LogFont LogFont);
  41. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  42. public static extern int BitBlt (IntPtr hDestDC, int x, int y , int nWidth, int nHeight, IntPtr hSrcDC , int xSrc, int ySrc, int dwRop );
  43. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  44. public static extern IntPtr CreateSolidBrush (int crColor);
  45. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  46. public static extern int Rectangle (IntPtr hDC, int left, int top , int right, int bottom);
  47. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  48. public static extern IntPtr CreateHatchBrush (int Style, int crColor);
  49. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  50. public static extern IntPtr CreateCompatibleBitmap (IntPtr hDC, int nWidth, int nHeight );
  51. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  52. public static extern IntPtr CreateCompatibleDC (IntPtr hDC);
  53. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  54. public static extern IntPtr SelectObject (IntPtr hDC, IntPtr hObject);
  55. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  56. public static extern IntPtr DeleteObject (IntPtr hObject);
  57. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  58. public static extern int GetTextColor (IntPtr hDC);
  59. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  60. public static extern int SetTextColor (IntPtr hDC, int crColor);
  61. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  62. public static extern int GetBkColor (IntPtr hDC);
  63. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  64. public static extern int GetBkMode (IntPtr hDC);
  65. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  66. public static extern IntPtr DeleteDC (IntPtr hDC);
  67. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  68. public static extern int SetBkColor (IntPtr hDC, int crColor);
  69. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention .StdCall)]
  70. public static extern int SetBkMode (IntPtr hDC, int Mode);
  71. [ DllImport("gdi32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention .StdCall, SetLastError=true )]
  72. public static extern int GdiFlush ();
  73. [ DllImport("gdi32.dll" , SetLastError = true, CharSet = CharSet. Ansi, CallingConvention = CallingConvention .StdCall)]
  74. public static extern int EnumFontFamiliesEx (IntPtr hDC, [MarshalAs(UnmanagedType .LPStruct)] LogFont lf , FONTENUMPROC proc, Int64 LParam, Int64 DW );
  75. [ DllImport("gdi32.dll" , EntryPoint = "GdiAlphaBlend" )]
  76. public static extern bool AlphaBlend (
  77. IntPtr hdcDest , // handle to destination DC
  78. int nXOriginDest , // x-coord of upper-left corner
  79. int nYOriginDest , // y-coord of upper-left corner
  80. int nWidthDest , // destination width
  81. int nHeightDest , // destination height
  82. IntPtr hdcSrc , // handle to source DC
  83. int nXOriginSrc , // x-coord of upper-left corner
  84. int nYOriginSrc , // y-coord of upper-left corner
  85. int nWidthSrc , // source width
  86. int nHeightSrc , // source height
  87. BLENDFUNCTION blendFunction // alpha-blending function
  88. );
  89. }

另外,如果真的是对效率要求很高的应用,还是推荐大家使用OpenGL、DirectX。.NET平台下有与之对应的Tao.OpenGL和Managed DirectX。

0