PictureBoxHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Drawing.Imaging;
  9. using System.Drawing.Drawing2D;
  10. namespace Core.Mes.Client.Comm.Control
  11. {
  12. /// <summary>
  13. /// PictureBox图片处理类
  14. /// </summary>
  15. public class PictureBoxHelper
  16. {
  17. /// <summary>
  18. /// PictureBox显示缩略图
  19. /// </summary>
  20. /// <param name="imagebytes">图片二进制数组</param>
  21. /// <param name="pic">显示图片的PictureBox</param>
  22. public static void LoadThumbnailImage(byte[] imagebytes, PictureBox pic)
  23. {
  24. LoadThumbnailImage(imagebytes, pic, pic.Width, pic.Height);
  25. }
  26. /// <summary>
  27. /// PictureBox显示缩略图
  28. /// </summary>
  29. /// <param name="imagebytes">图片二进制数组</param>
  30. /// <param name="pic">显示图片的PictureBox</param>
  31. /// <param name="Width">显示宽度</param>
  32. /// <param name="Height">显示高度</param>
  33. public static void LoadThumbnailImage(byte[] imagebytes, PictureBox pic, int Width, int Height)
  34. {
  35. if (imagebytes != null && imagebytes.Length > 0 && pic != null)
  36. {
  37. MemoryStream stream = new MemoryStream(imagebytes, true); // 创建一个内存流,支持写入,用于存放图片二进制数据
  38. stream.Write(imagebytes, 0, imagebytes.Length);
  39. Bitmap bitmap = new Bitmap(stream);
  40. stream.Close();
  41. Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
  42. int originalWidth = bitmap.Width;//图片实际宽度
  43. int originalHeight = bitmap.Height;//图片实际高度
  44. //图片显示宽度 按宽高比计算
  45. int showWidth = ((double)Width / originalWidth > (double)Height / originalHeight) ? Convert.ToInt32(Math.Round((double)originalWidth * Height / originalHeight, 0)) : Width;
  46. //图片显示高度
  47. int showHeight = ((double)Width / originalWidth > (double)Height / originalHeight) ? Height : Convert.ToInt32(Math.Round((double)originalHeight * Width / originalWidth, 0));
  48. Image image = bitmap.GetThumbnailImage(showWidth, showHeight, myCallback, IntPtr.Zero);
  49. //return myImage;
  50. pic.SizeMode = PictureBoxSizeMode.CenterImage;
  51. pic.Image = image;
  52. }
  53. }
  54. /// <summary>
  55. /// PictureBox显示缩略图(如果图片小于PictureBox的大小,则不进行缩放。)
  56. /// </summary>
  57. /// <param name="imagebytes">图片二进制数组</param>
  58. /// <param name="pic">显示图片的PictureBox</param>
  59. /// <param name="Width">显示宽度</param>
  60. /// <param name="Height">显示高度</param>
  61. public static void LoadThumbnailImage(Bitmap srcImg, PictureBox pic)
  62. {
  63. try
  64. {
  65. if (srcImg == null)
  66. {
  67. if (pic.Image != null)
  68. {
  69. pic.Image.Dispose();
  70. }
  71. return;
  72. }
  73. //Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
  74. //int Width = pic.Width;
  75. //int Height = pic.Height;
  76. //int originalWidth = bitmap.Width;//图片实际宽度
  77. //int originalHeight = bitmap.Height;//图片实际高度
  78. //if (Width >= originalWidth && Height >= originalHeight) return;
  79. ////图片显示宽度 按宽高比计算
  80. //int showWidth = ((double)Width / originalWidth > (double)Height / originalHeight) ? Convert.ToInt32(Math.Round((double)originalWidth * Height / originalHeight, 0)) : Width;
  81. ////图片显示高度
  82. //int showHeight = ((double)Width / originalWidth > (double)Height / originalHeight) ? Height : Convert.ToInt32(Math.Round((double)originalHeight * Width / originalWidth, 0));
  83. //Image image = bitmap.GetThumbnailImage(showWidth, showHeight, myCallback, IntPtr.Zero);
  84. ////return myImage;
  85. //pic.SizeMode = PictureBoxSizeMode.CenterImage;
  86. //pic.Image = image;
  87. int destHeight = pic.Height;
  88. int destWidth = pic.Width;
  89. System.Drawing.Image imgSource = srcImg;
  90. System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
  91. int sW = 0, sH = 0;
  92. // 按比例缩放
  93. int sWidth = imgSource.Width;
  94. int sHeight = imgSource.Height;
  95. if (sHeight > destHeight || sWidth > destWidth)
  96. {
  97. if ((sWidth * destHeight) > (sHeight * destWidth))
  98. {
  99. sW = destWidth;
  100. sH = (destWidth * sHeight) / sWidth;
  101. }
  102. else
  103. {
  104. sH = destHeight;
  105. sW = (sWidth * destHeight) / sHeight;
  106. }
  107. }
  108. else
  109. {
  110. sW = sWidth;
  111. sH = sHeight;
  112. }
  113. Bitmap outBmp = new Bitmap(destWidth, destHeight);
  114. Graphics g = Graphics.FromImage(outBmp);
  115. g.Clear(Color.Transparent);
  116. // 设置画布的描绘质量
  117. g.CompositingQuality = CompositingQuality.HighQuality;
  118. g.SmoothingMode = SmoothingMode.HighQuality;
  119. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  120. g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
  121. g.Dispose();
  122. // 以下代码为保存图片时,设置压缩质量
  123. EncoderParameters encoderParams = new EncoderParameters();
  124. long[] quality = new long[1];
  125. quality[0] = 100;
  126. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
  127. encoderParams.Param[0] = encoderParam;
  128. //if (pic.Image == outBmp)
  129. //{
  130. // pic.Image.Dispose();
  131. //}
  132. if (pic.Image != null)
  133. {
  134. pic.Image.Dispose();
  135. }
  136. pic.Image = outBmp;
  137. }
  138. catch (Exception ex)
  139. {
  140. }
  141. }
  142. public static bool ThumbnailCallback()
  143. {
  144. return false;
  145. }
  146. /// <summary>
  147. /// PictureBox显示缩略图(如果图片小于PictureBox的大小,则不进行缩放。)
  148. /// </summary>
  149. /// <param name="imagebytes">图片二进制数组</param>
  150. /// <param name="pic">显示图片的PictureBox</param>
  151. /// <param name="Width">显示宽度</param>
  152. /// <param name="Height">显示高度</param>
  153. public static void LoadThumbnailImage(PictureBox pic)
  154. {
  155. if (pic.Image == null) return;
  156. Bitmap bitmap = (Bitmap)pic.Image;
  157. Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
  158. int Width = pic.Width;
  159. int Height = pic.Height;
  160. int originalWidth = bitmap.Width;//图片实际宽度
  161. int originalHeight = bitmap.Height;//图片实际高度
  162. if (Width >= originalWidth && Height >= originalHeight) return;
  163. //图片显示宽度 按宽高比计算
  164. int showWidth = ((double)Width / originalWidth > (double)Height / originalHeight) ? Convert.ToInt32(Math.Round((double)originalWidth * Height / originalHeight, 0)) : Width;
  165. //图片显示高度
  166. int showHeight = ((double)Width / originalWidth > (double)Height / originalHeight) ? Height : Convert.ToInt32(Math.Round((double)originalHeight * Width / originalWidth, 0));
  167. Image image = bitmap.GetThumbnailImage(showWidth, showHeight, myCallback, IntPtr.Zero);
  168. //return myImage;
  169. pic.SizeMode = PictureBoxSizeMode.CenterImage;
  170. pic.Image = image;
  171. }
  172. }
  173. }