| 返回任务 | ||||
|
using System;
using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Drawing.Imaging; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnUpLoad_Click(object sender, EventArgs e) { Random n = new Random(); string str="~/" +n.Next(100000).ToString()+ FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.IndexOf(".")); FileUpload1.SaveAs(Server.MapPath(str)); myGetThumbnailImage(Server.MapPath(str), @"D:\Project\ImageEdit\84283-1.jpg", 120,120, "white"); } /// <summary> /// 判断图片大小 Quality(upload) /// </summary> /// <param name="upLoad">控件</param> /// <returns></returns> public Boolean Quality(FileUpload upLoad) { if (upLoad.PostedFile.ContentLength > 50000) { return false; } else { return true; } } public Boolean Size(string path) { System.Drawing.Image myThumbnail = System.Drawing.Image.FromFile(path); if ((myThumbnail.Width > 550) && (myThumbnail.Height > 412.5)) { System.Drawing.Image copyImage = myThumbnail.GetThumbnailImage(550, 412, null, IntPtr.Zero); return false; } else { return true; } } /// </summary> /// <param name="SourceFile">文件在服务器上的物理地址</param> /// <param name="strSavePathFile">保存在服务器上的路径</param> /// <param name="ThumbWidth">宽度</param> /// <param name="ThumbHeight">高度</param> /// <param name="BgColor">背景</param> public static void myGetThumbnailImage(string SourceFile, string strSavePathFile, int ThumbWidth, int ThumbHeight, string BgColor) { System.Drawing.Image oImg = System.Drawing.Image.FromFile(SourceFile); //小图 int intwidth, intheight; if (oImg.Width > oImg.Height) { if (oImg.Width > ThumbWidth) { intwidth = ThumbWidth; intheight = (oImg.Height * ThumbWidth) / oImg.Width; } else { intwidth = oImg.Width; intheight = oImg.Height; } } else { if (oImg.Height > ThumbHeight) { intwidth = (oImg.Width * ThumbHeight) / oImg.Height; intheight = ThumbHeight; } else { intwidth = oImg.Width; intheight = oImg.Height; } } //构造一个指定宽高的Bitmap Bitmap bitmay = new Bitmap(intwidth, intheight); Graphics g = Graphics.FromImage(bitmay); Color myColor; if (BgColor == null) myColor = Color.FromName("white"); else myColor = Color.FromName(BgColor); //用指定的颜色填充Bitmap g.Clear(myColor); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //开始画图 g.DrawImage(oImg, new Rectangle(0, 0, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel); //bitmay.Save(strSavePathFile, System.Drawing.Imaging.ImageFormat.Jpeg); KiSaveAsJPEG(bitmay, @"D:\Project\ImageEdit\1.jpg", 50); g.Dispose(); bitmay.Dispose(); oImg.Dispose(); //删除源图 try { File.Delete(SourceFile); } catch { } } /**/ /// <summary> /// 保存JPG时用 /// </summary> /// <param name="mimeType"></param> /// <returns>得到指定mimeType的ImageCodecInfo</returns> private static ImageCodecInfo GetCodecInfo(string mimeType) { ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo ici in CodecInfo) { if (ici.MimeType == mimeType) return ici; } return null; } /**/ /// <summary> /// 保存为JPEG格式,支持压缩质量选项 /// </summary> /// <param name="bmp"></param> /// <param name="FileName"></param> /// <param name="Qty"></param> /// <returns></returns> public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty) { try { EncoderParameter p; EncoderParameters ps; ps = new EncoderParameters(1); p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty); ps.Param[0] = p; bmp.Save(FileName, GetCodecInfo("image/jpeg"), ps); return true; } catch { return false; } } } |
|||||||||||
| 还没有人评论 |