public static string ReSizeImge(string filePath,string text,int width,int height,int level)
![]()
![]()
{
string fileName = System.IO.Path.GetFileName(filePath).ToLower();
string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();
string document = System.IO.Path.GetDirectoryName(filePath)+"\\";
bool check = false;
switch(fileExtension)
![]()
{
case ".jpg":check =true;
break;
case ".gif":check =false;
break;
default: check =false;
break;
}
![]()
//图片按比例缩放算法
if(check)
![]()
{
string saveName = Guid.NewGuid().ToString().Replace("-","")+fileExtension;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(filePath);
![]()
double h=Convert.ToDouble(oldimage.Height.ToString());
double w=Convert.ToDouble(oldimage.Width.ToString());
double f=h/w;
if(width==0&&height==0)
![]()
{
![]()
if(w>700)
{w=700;h=700*f;}
}
else if(width==0&&height!=0)
![]()
{
h = height;
w = height*(1/f);
}
else if(width!=0&&height==0)
![]()
{
w = width;
h = width*f;
}
else
![]()
{
w = width;
h = height;
}
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Convert.ToInt32(w),Convert.ToInt32(h),null,new System.IntPtr());
Bitmap output=new Bitmap(thumbnailImage);
![]()
//处理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);
![]()
//Graphics g = Graphics.FromImage(output);
//g.DrawImage(thumbnailImage, 0, 0, thumbnailImage.Width, thumbnailImage.Height);
//Font f11 = new Font("Verdana", 9);
//Brush b = new SolidBrush(Color.White);
//g.DrawString(text, f11, b, 10, 10);
//g.Dispose();
![]()
![]()
string Path = document+saveName;
output.Save(Path,ici,ep);
![]()
//释放所有使用对象
ep.Dispose();
output.Dispose();
oldimage.Dispose();
thumbnailImage.Dispose();
![]()
//删除源图片
UnitLink.Component.Common.doIO.DeletePath(filePath);
return saveName;
}
else
![]()
{
return fileName;
}
}
![]()