RenderEngine.3.Image.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Drawing;
  2. using System.Drawing.Imaging;
  3. namespace Microsoft.Windows.Forms
  4. {
  5. public static partial class RenderEngine
  6. {
  7. /// <summary>
  8. /// 获取黑白图像(返回了新的图像,使用完后需要手动释放)
  9. /// </summary>
  10. /// <param name="originImage">原图</param>
  11. /// <returns>黑白图</returns>
  12. public static Bitmap GetGrayImage(Image originImage)
  13. {
  14. int width = originImage.Width;
  15. int height = originImage.Height;
  16. Bitmap newBitmap = new Bitmap(width, height);
  17. //绘制新图像
  18. using (Graphics g = Graphics.FromImage(newBitmap))
  19. {
  20. //绘图参数检查
  21. if (m_DisabledImageAttr == null)
  22. {
  23. //颜色变换矩阵,第一行到第五行分别表示RGBA虚拟,第一列到第五列分别表示RGBA虚拟.
  24. ColorMatrix colorMatrix = new ColorMatrix(new float[][]
  25. {
  26. new float[] {0.2125f, 0.2125f, 0.2125f, 000f, 000f},//新的R=旧的R*0.2125f+旧的G*0.2125f+旧的B*0.2125f
  27. new float[] {0.2577f, 0.2577f, 0.2577f, 000f, 000f},
  28. new float[] {0.0361f, 0.0361f, 0.0361f, 000f, 000f},
  29. new float[] {000000f, 000000f, 000000f, 001f, 000f},
  30. new float[] {0.3800f, 0.3800f, 0.3800f, 000f, 001f}
  31. });
  32. //创建绘图参数
  33. m_DisabledImageAttr = new ImageAttributes();
  34. m_DisabledImageAttr.SetColorMatrix(colorMatrix);
  35. }
  36. //绘图
  37. g.DrawImage(originImage, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, m_DisabledImageAttr);
  38. }
  39. //返回
  40. return newBitmap;
  41. }
  42. /// <summary>
  43. /// 获取透明图像(返回了新的图像,使用完后需要手动释放)
  44. /// </summary>
  45. /// <param name="originImage">原图</param>
  46. /// <param name="opacity">透明度[0-1]</param>
  47. /// <returns>透明图像</returns>
  48. public static Bitmap GetTransparentImage(Image originImage, float opacity)
  49. {
  50. int width = originImage.Width;
  51. int height = originImage.Height;
  52. Bitmap newBitmap = new Bitmap(width, height);
  53. //绘制新图像
  54. using (Graphics graphics = Graphics.FromImage(newBitmap))
  55. {
  56. //绘图参数
  57. using (ImageAttributes imgAttr = new ImageAttributes())
  58. {
  59. ColorMatrix clrMatrix = new ColorMatrix();
  60. clrMatrix.Matrix33 = opacity;
  61. imgAttr.SetColorMatrix(clrMatrix);
  62. //绘图
  63. graphics.DrawImage(originImage, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imgAttr);
  64. }
  65. }
  66. //返回
  67. return newBitmap;
  68. }
  69. /// <summary>
  70. /// 缩放图像(返回了新的图像,使用完后需要手动释放)
  71. /// </summary>
  72. /// <param name="originImage">要缩放的图像</param>
  73. /// <param name="size">要缩放为的大小</param>
  74. /// <returns>缩放后的图像</returns>
  75. public static Bitmap GetStretchImage(Image originImage, Size size)
  76. {
  77. if (originImage == null || size.Width <= 0 || size.Height <= 0)
  78. return null;
  79. Bitmap newBitmap = new Bitmap(size.Width, size.Height);
  80. using (Graphics g = Graphics.FromImage(newBitmap))
  81. {
  82. g.DrawImage(originImage, new Rectangle(0, 0, size.Width, size.Height));
  83. }
  84. return newBitmap;
  85. }
  86. }
  87. }