方法一:直接读取图片文件
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="localPath">原图路径</param>
/// <param name="toWidth">缩略后的宽</param>
/// <param name="toHeight">高</param>
/// <returns></returns>
private Image MakeThumbnail(string localPath, int toWidth, int toHeight)
{
if (!File.Exists(localPath))
return null; // 可以返回一个特定的图片
Image originalImage = Image.FromFile(localPath);
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
//指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)toWidth / (double)toHeight)
{
oh = originalImage.Height;
ow = originalImage.Height * toWidth / toHeight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * toHeight / toWidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
//新建一个bmp图片
Image bitmap = new Bitmap(toWidth, toHeight);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
originalImage.Dispose();
g.Dispose();
return bitmap;
}
方法二:读取文件到内存中
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="toWidth">缩略后的宽</param>
/// <param name="toHeight">高</param>
/// <param name="originalImage">原始图片</param>
/// <returns></returns>
private Image MakeThumbnail(string localPath, int toWidth, int toHeight)
{
Image originalImage = ReadPictureStream(localPath);
if (originalImage == null)
return null;// 可以返回一个特定的图片
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
//指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)toWidth / (double)toHeight)
{
oh = originalImage.Height;
ow = originalImage.Height * toWidth / toHeight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * toHeight / toWidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
//新建一个bmp图片
Image bitmap = new Bitmap(toWidth, toHeight);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
originalImage.Dispose();
g.Dispose();
return bitmap;
}
// 读取图片存放内存中并返回一个图片对象
private Image ReadPictureStream(string localPath)
{
if (!File.Exists(localPath))
return null;
Image image = null;
FileInfo fileinfo = new FileInfo(localPath);
using (FileStream imgdatastream = System.IO.File.Open(localPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
{
int imgdatalen = int.Parse(fileinfo.Length.ToString()); //定义每次读取字节的长度
byte[] m_ImgData = new byte[imgdatalen];
//获取图片的字节数
imgdatastream.Read(m_ImgData, 0, imgdatalen);
//压缩指定路径下的图片并预览
using (MemoryStream MemStream = new System.IO.MemoryStream(m_ImgData))
{
image = Image.FromStream(MemStream);
}
}
return image;
}