改进版本v1.1。解决了生成较大缩略图时质量下降问题。现在的版本正确的叫法应改是缩放图了。哈哈
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Onest.Cyclone.Business.util
{
/// <summary>
/// 缩略图
/// </summary>
public class Miniature
{
private Miniature()
{
}
public static void Convert(System.IO.Stream src,string newImagePath,int Width,int Height,string Copyright)
{
System.Drawing.Image oldimage = Image.FromStream(src,true,true);
int width ;//缩略图的宽度
int height ;// 缩略图的高度
if(oldimage.Width > oldimage.Height ) //横向,颠倒一下尺寸设置
{
width = Height;//缩略图的宽度
height =Width ;// 缩略图的高度
}
else //纵向
{
//oldImagePath -原图地址 newImagePath 生成缩略图地址
width =Width ;//缩略图的宽度
height =Height ;// 缩略图的高度
}
int level = 100; //缩略图的质量 1-100的范围
//System.Drawing.Image oldimage = System.Drawing.Image.FromFile(oldImagePath);
//System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
//Bitmap bm=new Bitmap(thumbnailImage);
// new method
Bitmap bm = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bm);
g.InterpolationMode= System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear ;
g.DrawImage(oldimage, new Rectangle(0, 0, bm.Width, bm.Height),0, 0,
oldimage.Width, oldimage.Height, GraphicsUnit.Pixel) ;
Graphics.FromImage(oldimage).DrawImage(bm,0,0,width,height);
//处理JPG质量的函数
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
#region 制作水印
Graphics grPhoto = Graphics.FromImage(bm);
//用指定Sizes绘制图片时,测量用指定的字串宽度
//取可能的最大宽度
int[] sizes = new int[]{64,48,32,16,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("Verdana", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont);
if((ushort)crSize.Width < (ushort)width)
break;
}
//指定做图点
int yPixlesFromBottom = (int)(height *.05);
float yPosFromBottom = ((height -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (width/2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//绘制copyright
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(100, 0, 0,0));
grPhoto.DrawString(Copyright,crFont,semiTransBrush2,new PointF(xCenterOfImg+1,yPosFromBottom+1),StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));
grPhoto.DrawString(Copyright,crFont,semiTransBrush, new PointF(xCenterOfImg,yPosFromBottom),StrFormat);
#endregion
bm.Save(newImagePath,ici,ep);
try
{
//释放资源
ep.Dispose();
bm.Dispose();
oldimage.Dispose();
g.Dispose();
codecs=null;
StrFormat.Dispose();
semiTransBrush2.Dispose();
grPhoto.Dispose();
semiTransBrush.Dispose();
sizes=null;
}
catch{}
}
public static void Convert(string oldImagePath,string newImagePath,int Width,int Height,string Copyright)
{
System.IO.StreamReader sr = new System.IO.StreamReader(oldImagePath);
Convert(sr.BaseStream ,newImagePath,Width,Height,Copyright);
}
private static bool ThumbnailCallback(){return true;}
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Onest.Cyclone.Business.util
{
/// <summary>
/// 缩略图
/// </summary>
public class Miniature
{
private Miniature()
{
}
public static void Convert(System.IO.Stream src,string newImagePath,int Width,int Height,string Copyright)
{
System.Drawing.Image oldimage = Image.FromStream(src,true,true);
int width ;//缩略图的宽度
int height ;// 缩略图的高度
if(oldimage.Width > oldimage.Height ) //横向,颠倒一下尺寸设置
{
width = Height;//缩略图的宽度
height =Width ;// 缩略图的高度
}
else //纵向
{
//oldImagePath -原图地址 newImagePath 生成缩略图地址
width =Width ;//缩略图的宽度
height =Height ;// 缩略图的高度
}
int level = 100; //缩略图的质量 1-100的范围
//System.Drawing.Image oldimage = System.Drawing.Image.FromFile(oldImagePath);
//System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
//Bitmap bm=new Bitmap(thumbnailImage);
// new method
Bitmap bm = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bm);
g.InterpolationMode= System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear ;
g.DrawImage(oldimage, new Rectangle(0, 0, bm.Width, bm.Height),0, 0,
oldimage.Width, oldimage.Height, GraphicsUnit.Pixel) ;
Graphics.FromImage(oldimage).DrawImage(bm,0,0,width,height);
//处理JPG质量的函数
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
#region 制作水印
Graphics grPhoto = Graphics.FromImage(bm);
//用指定Sizes绘制图片时,测量用指定的字串宽度
//取可能的最大宽度
int[] sizes = new int[]{64,48,32,16,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("Verdana", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont);
if((ushort)crSize.Width < (ushort)width)
break;
}
//指定做图点
int yPixlesFromBottom = (int)(height *.05);
float yPosFromBottom = ((height -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (width/2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//绘制copyright
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(100, 0, 0,0));
grPhoto.DrawString(Copyright,crFont,semiTransBrush2,new PointF(xCenterOfImg+1,yPosFromBottom+1),StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));
grPhoto.DrawString(Copyright,crFont,semiTransBrush, new PointF(xCenterOfImg,yPosFromBottom),StrFormat);
#endregion
bm.Save(newImagePath,ici,ep);
try
{
//释放资源
ep.Dispose();
bm.Dispose();
oldimage.Dispose();
g.Dispose();
codecs=null;
StrFormat.Dispose();
semiTransBrush2.Dispose();
grPhoto.Dispose();
semiTransBrush.Dispose();
sizes=null;
}
catch{}
}
public static void Convert(string oldImagePath,string newImagePath,int Width,int Height,string Copyright)
{
System.IO.StreamReader sr = new System.IO.StreamReader(oldImagePath);
Convert(sr.BaseStream ,newImagePath,Width,Height,Copyright);
}
private static bool ThumbnailCallback(){return true;}
}
}
来源:https://www.cnblogs.com/Ankh/archive/2005/09/20/240295.html