| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- namespace Core.Mes.Client.Comm.Control
- {
- /// <summary>
- /// PictureBox图片处理类
- /// </summary>
- public class PictureBoxHelper
- {
- /// <summary>
- /// PictureBox显示缩略图
- /// </summary>
- /// <param name="imagebytes">图片二进制数组</param>
- /// <param name="pic">显示图片的PictureBox</param>
- public static void LoadThumbnailImage(byte[] imagebytes, PictureBox pic)
- {
- LoadThumbnailImage(imagebytes, pic, pic.Width, pic.Height);
- }
- /// <summary>
- /// PictureBox显示缩略图
- /// </summary>
- /// <param name="imagebytes">图片二进制数组</param>
- /// <param name="pic">显示图片的PictureBox</param>
- /// <param name="Width">显示宽度</param>
- /// <param name="Height">显示高度</param>
- public static void LoadThumbnailImage(byte[] imagebytes, PictureBox pic, int Width, int Height)
- {
- if (imagebytes != null && imagebytes.Length > 0 && pic != null)
- {
- MemoryStream stream = new MemoryStream(imagebytes, true); // 创建一个内存流,支持写入,用于存放图片二进制数据
- stream.Write(imagebytes, 0, imagebytes.Length);
- Bitmap bitmap = new Bitmap(stream);
- stream.Close();
- Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
- int originalWidth = bitmap.Width;//图片实际宽度
- int originalHeight = bitmap.Height;//图片实际高度
- //图片显示宽度 按宽高比计算
- int showWidth = ((double)Width / originalWidth > (double)Height / originalHeight) ? Convert.ToInt32(Math.Round((double)originalWidth * Height / originalHeight, 0)) : Width;
- //图片显示高度
- int showHeight = ((double)Width / originalWidth > (double)Height / originalHeight) ? Height : Convert.ToInt32(Math.Round((double)originalHeight * Width / originalWidth, 0));
- Image image = bitmap.GetThumbnailImage(showWidth, showHeight, myCallback, IntPtr.Zero);
- //return myImage;
- pic.SizeMode = PictureBoxSizeMode.CenterImage;
- pic.Image = image;
- }
- }
- /// <summary>
- /// PictureBox显示缩略图(如果图片小于PictureBox的大小,则不进行缩放。)
- /// </summary>
- /// <param name="imagebytes">图片二进制数组</param>
- /// <param name="pic">显示图片的PictureBox</param>
- /// <param name="Width">显示宽度</param>
- /// <param name="Height">显示高度</param>
- public static void LoadThumbnailImage(Bitmap srcImg, PictureBox pic)
- {
- try
- {
- if (srcImg == null)
- {
- if (pic.Image != null)
- {
- pic.Image.Dispose();
- }
- return;
- }
- //Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
- //int Width = pic.Width;
- //int Height = pic.Height;
- //int originalWidth = bitmap.Width;//图片实际宽度
- //int originalHeight = bitmap.Height;//图片实际高度
- //if (Width >= originalWidth && Height >= originalHeight) return;
- ////图片显示宽度 按宽高比计算
- //int showWidth = ((double)Width / originalWidth > (double)Height / originalHeight) ? Convert.ToInt32(Math.Round((double)originalWidth * Height / originalHeight, 0)) : Width;
- ////图片显示高度
- //int showHeight = ((double)Width / originalWidth > (double)Height / originalHeight) ? Height : Convert.ToInt32(Math.Round((double)originalHeight * Width / originalWidth, 0));
- //Image image = bitmap.GetThumbnailImage(showWidth, showHeight, myCallback, IntPtr.Zero);
- ////return myImage;
- //pic.SizeMode = PictureBoxSizeMode.CenterImage;
- //pic.Image = image;
- int destHeight = pic.Height;
- int destWidth = pic.Width;
- System.Drawing.Image imgSource = srcImg;
- System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
- int sW = 0, sH = 0;
- // 按比例缩放
- int sWidth = imgSource.Width;
- int sHeight = imgSource.Height;
- if (sHeight > destHeight || sWidth > destWidth)
- {
- if ((sWidth * destHeight) > (sHeight * destWidth))
- {
- sW = destWidth;
- sH = (destWidth * sHeight) / sWidth;
- }
- else
- {
- sH = destHeight;
- sW = (sWidth * destHeight) / sHeight;
- }
- }
- else
- {
- sW = sWidth;
- sH = sHeight;
- }
- Bitmap outBmp = new Bitmap(destWidth, destHeight);
- Graphics g = Graphics.FromImage(outBmp);
- g.Clear(Color.Transparent);
- // 设置画布的描绘质量
- g.CompositingQuality = CompositingQuality.HighQuality;
- g.SmoothingMode = SmoothingMode.HighQuality;
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
- g.Dispose();
- // 以下代码为保存图片时,设置压缩质量
- EncoderParameters encoderParams = new EncoderParameters();
- long[] quality = new long[1];
- quality[0] = 100;
- EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
- encoderParams.Param[0] = encoderParam;
- //if (pic.Image == outBmp)
- //{
- // pic.Image.Dispose();
- //}
- if (pic.Image != null)
- {
- pic.Image.Dispose();
- }
- pic.Image = outBmp;
- }
- catch (Exception ex)
- {
- }
- }
- public static bool ThumbnailCallback()
- {
- return false;
- }
- /// <summary>
- /// PictureBox显示缩略图(如果图片小于PictureBox的大小,则不进行缩放。)
- /// </summary>
- /// <param name="imagebytes">图片二进制数组</param>
- /// <param name="pic">显示图片的PictureBox</param>
- /// <param name="Width">显示宽度</param>
- /// <param name="Height">显示高度</param>
- public static void LoadThumbnailImage(PictureBox pic)
- {
- if (pic.Image == null) return;
- Bitmap bitmap = (Bitmap)pic.Image;
- Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
- int Width = pic.Width;
- int Height = pic.Height;
- int originalWidth = bitmap.Width;//图片实际宽度
- int originalHeight = bitmap.Height;//图片实际高度
- if (Width >= originalWidth && Height >= originalHeight) return;
- //图片显示宽度 按宽高比计算
- int showWidth = ((double)Width / originalWidth > (double)Height / originalHeight) ? Convert.ToInt32(Math.Round((double)originalWidth * Height / originalHeight, 0)) : Width;
- //图片显示高度
- int showHeight = ((double)Width / originalWidth > (double)Height / originalHeight) ? Height : Convert.ToInt32(Math.Round((double)originalHeight * Width / originalWidth, 0));
- Image image = bitmap.GetThumbnailImage(showWidth, showHeight, myCallback, IntPtr.Zero);
- //return myImage;
- pic.SizeMode = PictureBoxSizeMode.CenterImage;
- pic.Image = image;
- }
- }
- }
|