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
{
///
/// PictureBox图片处理类
///
public class PictureBoxHelper
{
///
/// PictureBox显示缩略图
///
/// 图片二进制数组
/// 显示图片的PictureBox
public static void LoadThumbnailImage(byte[] imagebytes, PictureBox pic)
{
LoadThumbnailImage(imagebytes, pic, pic.Width, pic.Height);
}
///
/// PictureBox显示缩略图
///
/// 图片二进制数组
/// 显示图片的PictureBox
/// 显示宽度
/// 显示高度
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;
}
}
///
/// PictureBox显示缩略图(如果图片小于PictureBox的大小,则不进行缩放。)
///
/// 图片二进制数组
/// 显示图片的PictureBox
/// 显示宽度
/// 显示高度
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;
}
///
/// PictureBox显示缩略图(如果图片小于PictureBox的大小,则不进行缩放。)
///
/// 图片二进制数组
/// 显示图片的PictureBox
/// 显示宽度
/// 显示高度
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;
}
}
}