| 关于网上流行《上传图片并生成可带版权信息的缩略图》问题讨论 | 下面代码是网上流行的一些代码,可是发现生成的图片非常模糊,因为他是调用.net类库里面GetThumbnailImage方法。 ========================================================= ASP.NET上传图片并生成可带版权信息的缩略图
<%@ Page Language="C#" ResponseEncoding="gb2312" %> <%@ import Namespace="System" %> <%@ import Namespace="System.IO" %> <%@ import Namespace="System.Drawing" %> <%@ import Namespace="System.Drawing.Imaging" %> <script runat="server">
void Page_Load(Object sender, EventArgs e) { if(!Page.IsPostBack) { ImgPreview.Visible=false; } } void GetThumbnailImage(int width,int height,string strInfo,int left,int right) { string file="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//')+1); string newfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//')+1)+".jpg"; string strAdd=strInfo; System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file)); System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); Response.Clear(); Bitmap output=new Bitmap(thumbnailImage); Graphics g=Graphics.FromImage(output); g.DrawString(strAdd,new Font("Courier New", 14),new SolidBrush(Color.Red),left,right); output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg); Response.ContentType = "image/gif"; ImgPreview.Visible=true; ImgPreview.ImageUrl=newfile; } bool ThumbnailCallback() { return true; } | | | 2005-04-06 10:16:28 |
|
`漆黑 [9151021] | | | | 发帖数量:7 |
| Re:关于网上流行《上传图片并生成可带版权信息的缩略图》问题讨论 | void Button_Click(object sender, EventArgs e) { int width,height,left,right; string strAddInfo=txtAddInfo.Text; width=Int32.Parse(txtWidth.Text); height=Int32.Parse(txtHeight.Text); left=Int32.Parse(txtLeft.Text); right=Int32.Parse(txtRight.Text); if(!(uploadFile.PostedFile.ContentLength>0)) { lblErrInfo.Text="没有选择文件"; } else {
string path = Server.MapPath("./Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//')+1)); if(File.Exists(path)) { lblErrInfo.Text="已经有同名文件"; } else { uploadFile.PostedFile.SaveAs(path); GetThumbnailImage(width,height,strAddInfo,left,right); } } } </script> <html> <head> </head> <body> <form method="post" enctype="multipart/form-data" runat="server"> <p> <input id="uploadFile" type="file" runat="server" /> <asp:Label id="lblErrInfo" runat="server" forecolor="Red"></asp:Label> </p> <p> width:<asp:TextBox id="txtWidth" runat="server" Width="40px">100</asp:TextBox> height:<asp:TextBox id="txtHeight" runat="server" Width="40px">150</asp:TextBox>
</p> <p> 添加信息:<asp:TextBox id="txtAddInfo" runat="server"> AspxBoy.Com</asp:TextBox> </p> <p> 信息位置:left:<asp:TextBox id="txtLeft" runat="server" Width="40px">10</asp:TextBox> right:<asp:TextBox id="txtRight" runat="server" Width="40px">135</asp:TextBox> </p> <p>
<input id="button" type="button" value="上传生成所略图" onServerClick="Button_Click" runat="server" /> </p> <p><asp:Image id="ImgPreview" runat="server"></asp:Image> </p> <!-- Insert content here --> </form> </body> </html>
========================================================== | | | 2005-04-06 10:16:49 |
|
`漆黑 [9151021] | | | | 发帖数量:7 |
| Re:关于网上流行《上传图片并生成可带版权信息的缩略图》问题讨论 | 后来我自己写了个简单的类,以前写的,这个类写得很不规范,不过我也懒得改。 ============================================================== using System; using System.Web; using System.IO; using System.Drawing;
namespace pp.FileIO { /// <summary> /// 上传图片支持所略图 /// 2004/1/13 创建 林碧致 /// </summary> public class UpImg { private string imgFile; public string ImgFile { get { return imgFile; }
set { imgFile = value; } } private double newWH; /// <summary> /// 获取或设置所生成所略图的高宽 /// </summary> public double NewWH { get { return newWH; } set { newWH = value; } } private int maxSize; /// <summary> /// 获取或设置上传图片允许的最大字节数 /// </summary> public int MaxSize { get { return maxSize; } set { maxSize = value; } } /// <summary> /// 获取反馈消息 /// </summary> private string message; public string Message { get { return message; } } /// <summary> /// 保存上载图片并创建所略图 /// </summary> /// <param name="PF">提供PostedFile</param> /// <param name="pImgfile">保存图片到地址</param> /// <param name="pnewWH">所略图的高宽(单位象素)</param> /// <param name="pMaxSize">所上传图片最大的K数</param> public UpImg(HttpPostedFile PF,string pImgfile,int pnewWH,int pMaxSize) { this.ImgFile = pImgfile; this.NewWH = pnewWH; this.MaxSize = pMaxSize; this.UpFile(PF); } /// <summary> /// 创建所略图 /// </summary> /// <param name="pImgfile">指定被所略的图片路径</param> /// <param name="newWH">所略图的高宽</param> public UpImg(string pImgfile,int pnewWH , string houzui) { this.GetSLImage(pImgfile,pnewWH,houzui); }
/// <summary> /// 创建对象 /// </summary> public UpImg() {
} | | | 2005-04-06 10:17:25 |
|
`漆黑 [9151021] | | | | 发帖数量:7 |
| Re:关于网上流行《上传图片并生成可带版权信息的缩略图》问题讨论 | PF.SaveAs(this.ImgFile); this.OmitImage(".jpg"); this.message = "OK."; } } } } #endregion
#region 生成所略图 private void OmitImage(string houzui) { Bitmap oldBitmap = new Bitmap(imgFile); double oldW,oldH; int newW=0,newH=0; oldW = oldBitmap.Width; oldH = oldBitmap.Height; //如果宽大于高 if(oldW>oldH) { newW=(int)NewWH; newH=(int)(oldH/(oldW/NewWH)); } //如果高大于宽 if(oldH>oldW) { newH=(int)NewWH; newW=(int)(oldW/(oldH/NewWH)); } //如果高宽相等 if(oldH==oldW) { newW=(int)NewWH; newH=(int)NewWH; } Size size = new Size(newW,newH); Bitmap newBitmap = new Bitmap(oldBitmap,size); newBitmap.Save(@imgFile+houzui); newBitmap.Dispose(); oldBitmap.Dispose(); } #endregion | | | 2005-04-06 10:17:58 |
|
`漆黑 [9151021] | | | | 发帖数量:7 |
| Re:关于网上流行《上传图片并生成可带版权信息的缩略图》问题讨论 | /// <summary> /// 生成指定大小的所略图 /// </summary> /// <param name="pImgfile"></param> /// <param name="pnewWH"></param> public void GetSLImage(string pImgfile,int pnewWH, string houzui) { this.imgFile = pImgfile; this.NewWH = pnewWH; this.OmitImage(houzui); message = "所略图创建成功."; }
/// <summary> /// 删除图片及所略图 /// </summary> public void DeleteImg(string filePath) { //删除大图 File.Delete(filePath);
//删除中等所略图 File.Delete(filePath+".gif");
//删除所略图 File.Delete(filePath+".jpg");
}
#region 上传图片 private void UpFile(HttpPostedFile PF) { if(PF.ContentLength<=0) { this.message="请选择所要上传的图片."; } else { if(PF.ContentType!="image/pjpeg" & PF.ContentType!="image/gif") { this.message = "只允许上传GIF或JPG类型文件."; } else { if(PF.ContentLength>(this.MaxSize*1024)) { this.message = "图片文件不得超过"+this.MaxSize+"KB."; } else {
#region 图片写信息 /// <summary> /// 图片写信息 /// </summary> /// <param name="path">图片路径</param> /// <param name="str">文字信息</param> public void WriteImgStr(string path, string str) { string temp = path+"_temp.jpg"; Image newImg = Image.FromFile(path); if(newImg.PixelFormat.ToString()=="Format8bppIndexed") { newImg.Save(temp,System.Drawing.Imaging.ImageFormat.Jpeg); newImg.Dispose(); File.Delete(path); File.Move(temp,path); File.Delete(temp); WriteImgStr(path,str); } else { Graphics g = Graphics.FromImage(newImg); int fontsize = 18; int x=15; int y=15; g.DrawString(str,new Font("Arial", fontsize),new SolidBrush(Color.LightCoral),x,y); g.DrawString(str,new Font("Arial", fontsize),new SolidBrush(Color.White),x+1,y+1); g.DrawString(str,new Font("Arial", fontsize),new SolidBrush(Color.Green),x+2,y+2); newImg.Save(temp); //清除资源 g.Dispose(); newImg.Dispose(); File.Delete(path); File.Move(temp,path); File.Delete(temp); } } #endregion } } | | | 2005-04-06 10:18:09 |
|
`漆黑 [9151021] | | | | 发帖数量:7 |
| 两个所略图的区别 | 这是.net的方法 缩略图像是图像的小版本。可通过调用 Image 对象的 GetThumbnailImage 方法创建缩略图像。 [C#] Image image = new Bitmap("Compass.bmp"); Image pThumbnail = image.GetThumbnailImage(100, 100, null, new IntPtr()); e.Graphics.DrawImage( pThumbnail, 10, 10, pThumbnail.Width, pThumbnail.Height);
================================================= 下面这个是我写的方法 #region 生成所略图 private void OmitImage(string houzui) { Bitmap oldBitmap = new Bitmap(imgFile); double oldW,oldH; int newW=0,newH=0; oldW = oldBitmap.Width; oldH = oldBitmap.Height; //如果宽大于高 if(oldW>oldH) { newW=(int)NewWH; newH=(int)(oldH/(oldW/NewWH)); } //如果高大于宽 if(oldH>oldW) { newH=(int)NewWH; newW=(int)(oldW/(oldH/NewWH)); } //如果高宽相等 if(oldH==oldW) { newW=(int)NewWH; newH=(int)NewWH; } Size size = new Size(newW,newH); Bitmap newBitmap = new Bitmap(oldBitmap,size); newBitmap.Save(@imgFile+houzui); newBitmap.Dispose(); oldBitmap.Dispose(); } #endregion
|
|