在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享,(效果能达到一些绘图软件的效果)
代码如下:
-
-
-
-
-
-
-
-
- public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
- {
- string sThumbFile = "";
- string sFilename = "";
- if (upImage.PostedFile != null)
- {
- HttpPostedFile myFile = upImage.PostedFile;
- int nFileLen = myFile.ContentLength;
- if (nFileLen == 0)
- return "没有选择上传图片";
-
- string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
-
- if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
- return "图片格式不正确";
-
- byte[] myData = new Byte[nFileLen];
- myFile.InputStream.Read(myData, 0, nFileLen);
- sFilename = System.IO.Path.GetFileName(myFile.FileName);
- int file_append = 0;
-
- while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
- {
- file_append++;
- sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
- + file_append.ToString() + extendName;
- }
- System.IO.FileStream newFile
- = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
- System.IO.FileMode.Create, System.IO.FileAccess.Write);
- newFile.Write(myData, 0, myData.Length);
- newFile.Close();
-
- try
- {
-
- using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
- {
-
- int width = sourceImage.Width;
- int height = sourceImage.Height;
- int smallWidth;
- int smallHeight;
-
- if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
- {
- smallWidth = intThumbWidth;
- smallHeight = intThumbWidth * height / width;
- }
- else
- {
- smallWidth = intThumbHeight * width / height;
- smallHeight = intThumbHeight;
- }
-
- file_append = 0;
- sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
- while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
- {
- file_append++;
- sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
- file_append.ToString() + extendName;
- }
-
- string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
-
- using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
- {
-
- using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
- {
-
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.Clear(Color.Black);
- g.DrawImage(
- sourceImage,
- new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
- new System.Drawing.Rectangle(0, 0, width, height),
- System.Drawing.GraphicsUnit.Pixel
- );
- }
-
- using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
- {
-
- using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
- {
-
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.Clear(Color.Black);
- int lwidth = (smallWidth - intThumbWidth) / 2;
- int bheight = (smallHeight - intThumbHeight) / 2;
- g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
- g.Dispose();
- bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- }
- }
- }
- }
- catch
- {
-
- System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
- return "图片格式不正确";
- }
-
- return sThumbFile;
- }
- return "没有选择图片";
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input id="File1" runat="server" type="file" /></div><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- </form>
- </body>
- </html>
- protected void Button1_Click(object sender, EventArgs e)
- {
- string a = this.UpLoadImage(this.File1, "UpLoad/", "thumb_", 118, 118);
- }
|