GraphicsEx.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Drawing;
  3. using Microsoft.Win32;
  4. namespace Microsoft.Drawing
  5. {
  6. /// <summary>
  7. /// Graphics辅助类
  8. /// </summary>
  9. public static class GraphicsEx
  10. {
  11. /// <summary>
  12. /// 复制设备内容(不支持Alpha通道)
  13. /// </summary>
  14. /// <param name="gSrc">原设备</param>
  15. /// <param name="gDest">目标设备</param>
  16. /// <param name="ptDest">源起始坐标</param>
  17. /// <param name="ptSrc">目标起始坐标</param>
  18. /// <param name="szBlock">复制大小</param>
  19. public static void Render(Graphics gSrc, Graphics gDest, Point ptDest, Point ptSrc, Size szBlock)
  20. {
  21. if (gSrc == null || gDest == null)
  22. return;
  23. IntPtr hdcDest = gDest.GetHdc();
  24. IntPtr hdcSrc = gSrc.GetHdc();
  25. try
  26. {
  27. UnsafeNativeMethods.BitBlt(hdcDest, ptDest.X, ptDest.Y, szBlock.Width, szBlock.Height, hdcSrc, ptSrc.X, ptSrc.Y, NativeMethods.SRCCOPY);
  28. }
  29. finally
  30. {
  31. gSrc.ReleaseHdcInternal(hdcSrc);
  32. gDest.ReleaseHdcInternal(hdcDest);
  33. }
  34. }
  35. /// <summary>
  36. /// 混合设备内容(支持Alpha通道)
  37. /// </summary>
  38. /// <param name="gSrc">原设备</param>
  39. /// <param name="gDest">目标设备</param>
  40. /// <param name="ptDest">源起始坐标</param>
  41. /// <param name="ptSrc">目标起始坐标</param>
  42. /// <param name="szBlock">复制大小</param>
  43. public static void BlendRender(Graphics gSrc, Graphics gDest, Point ptDest, Point ptSrc, Size szBlock)
  44. {
  45. if (gSrc == null || gDest == null)
  46. return;
  47. IntPtr hdcDest = gDest.GetHdc();
  48. IntPtr hdcSrc = gSrc.GetHdc();
  49. try
  50. {
  51. UnsafeNativeMethods.GdiAlphaBlend(hdcDest, ptDest.X, ptDest.Y, szBlock.Width, szBlock.Height, hdcSrc, ptSrc.X, ptSrc.Y, szBlock.Width, szBlock.Height, NativeMethods.BLENDFUNCTION.Default);
  52. }
  53. finally
  54. {
  55. gSrc.ReleaseHdcInternal(hdcSrc);
  56. gDest.ReleaseHdcInternal(hdcDest);
  57. }
  58. }
  59. /// <summary>
  60. /// 混合设备内容(支持Alpha通道)
  61. /// </summary>
  62. /// <param name="gSrc">原设备</param>
  63. /// <param name="gDest">目标设备</param>
  64. /// <param name="ptDest">源起始坐标</param>
  65. /// <param name="ptSrc">目标起始坐标</param>
  66. /// <param name="szBlock">复制大小</param>
  67. /// <param name="alpha">透明度[0-255]</param>
  68. public static void BlendRender(Graphics gSrc, Graphics gDest, Point ptDest, Point ptSrc, Size szBlock, byte alpha)
  69. {
  70. if (gSrc == null || gDest == null)
  71. return;
  72. IntPtr hdcDest = gDest.GetHdc();
  73. IntPtr hdcSrc = gSrc.GetHdc();
  74. try
  75. {
  76. UnsafeNativeMethods.GdiAlphaBlend(hdcDest, ptDest.X, ptDest.Y, szBlock.Width, szBlock.Height, hdcSrc, ptSrc.X, ptSrc.Y, szBlock.Width, szBlock.Height, new NativeMethods.BLENDFUNCTION(NativeMethods.AC_SRC_OVER, 0, alpha, NativeMethods.AC_SRC_ALPHA));
  77. }
  78. finally
  79. {
  80. gSrc.ReleaseHdcInternal(hdcSrc);
  81. gDest.ReleaseHdcInternal(hdcDest);
  82. }
  83. }
  84. }
  85. }