下面是我专门抽出来的代码,可惜很死板,固定在80,120,这样不好,我的意思80,120就好比画布,图片正比缩小后容纳与此,保证有一边正好一致,例如宽或者高Bitmap myBmp = new Bitmap(@"c:\inetpub\wwwroot\upload\upload\25.jpg");
System.Drawing.Image pThumbnail = myBmp.GetThumbnailImage(80, 120, null, IntPtr.Zero);
pThumbnail.Save(@"c:\inetpub\wwwroot\upload\upload\25_s.jpg",ImageFormat.Jpeg);
上面的代码缩小后的图像清晰度不是很一流,谁有缩小后更清晰的写法
System.Drawing.Image pThumbnail = myBmp.GetThumbnailImage(80, 120, null, IntPtr.Zero);
pThumbnail.Save(@"c:\inetpub\wwwroot\upload\upload\25_s.jpg",ImageFormat.Jpeg);
上面的代码缩小后的图像清晰度不是很一流,谁有缩小后更清晰的写法
宽和高不要同时指定,可以判断一下,原图的宽高之比private const int DEF_WIDTH = 80;
private const int DEF_HEIGHT = 120;
if(pThumbnail.Height* DEF_WIDTH > DEF_HEIGHT * pThumbnail.Width )
newImg = oriImg.GetThumbnailImage(DEF_HEIGHT * oriImg.Width /oriImg.Height,DEF_HEIGHT ,null,IntPtr.Zero );
else
g.DrawImage(pThumbnail,DEF_WIDTH, DEF_WIDTH * pThumbnail.Height/pThumbnail.Width, pThumbnail.Width, pThumbnail.Height);
我写的上传代码,
/// <summary>
/// upload the Source File ,And Create a Small Image (Need .jpg or .gif or .bmp or .ico)
/// </summary>
/// <param name="PostFile">包含由要上传的文件的文件上传控件</param>
/// <param name="id">要保存的文件名</param>
/// <param name="DestFileName">输出目标文件名</param>
/// <param name="TargetPath">要上传到的目录</param>
/// <param name="nWidth">缩略图的宽度(同比缩小)</param>
/// <param name="FileExt">缩略图保存的类型</param>
/// <returns></returns>
public static bool UploadFile(System.Web.HttpPostedFile PostFile, string id,out string sDestFileName,string sTargetPath,int nWidth,string sFileExt)
{
bool bRe = true;
string fileExt,ImgExt;
System.Drawing.Image oriImg, newImg; fileExt = (System.IO.Path.GetExtension(PostFile.FileName)).ToString().ToLower();
//5-test.jpg
// string imgNameOnly = System.IO.Path.GetFileName(PostFile.FileName); try
{
if ((id == null) || (id == ""))
id = Guid.NewGuid().ToString(); if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".ico")
{
oriImg = System.Drawing.Image.FromStream(PostFile.InputStream);
newImg = oriImg.GetThumbnailImage(nWidth, nWidth * oriImg.Height/oriImg.Width,null,IntPtr.Zero );
if((sFileExt != null) && (sFileExt != ""))
ImgExt = sFileExt;
else
ImgExt = fileExt;
}
else
{
throw new FileTypeException();
} sDestFileName = System.IO.Path.Combine(sTargetPath , id + fileExt);
string sSmallFileName = GetSmallFileName(sDestFileName);
if(System.IO.File.Exists(sDestFileName))
System.IO.File.Delete(sDestFileName); if(System.IO.File.Exists(sSmallFileName))
System.IO.File.Delete(sSmallFileName); oriImg.Save(sDestFileName,GetFormat(fileExt));
newImg.Save(sSmallFileName,GetFormat(ImgExt)); } catch(Exception ex)
{
bRe = false;
sDestFileName = "";
m_sErrorMessage = ex.Message;
} return bRe;
}此处修改
private const int DEF_WIDTH = 80;
private const int DEF_HEIGHT = 120;
oriImg = System.Drawing.Image.FromStream(PostFile.InputStream);
if(oriImg.Height* DEF_WIDTH > DEF_HEIGHT * oriImg.Width )
newImg = oriImg.GetThumbnailImage(DEF_HEIGHT * oriImg.Width /oriImg.Height,DEF_HEIGHT ,null,IntPtr.Zero );
else
newImg = oriImg.GetThumbnailImage(DEF_WIDTH, DEF_WIDTH * oriImg.Height/oriImg.Width,null,IntPtr.Zero );
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(80,120);
System.Drawing.Graphics gs = System.Drawing.Graphics.FromImage(bitmap);
float scale = System.Math.Min(80f / image.Width,120f / image.Height);
System.Drawing.RectangleF rectF = new System.Drawing.RectangleF(0,0,image.Width * scale,image.Height * scale);
rectF.X = (80 - rectF.Width) / 2;
rectF.Y = (120 - rectF.Height) / 2;
gs.DrawImage(image,rectF);
image.Dispose();
gs.Dispose();
bitmap.Save(Application.StartupPath + "\\80x120.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();