ASP.NET 上传图片同时生成缩略图,并加水印

uploadimage   Label ===========================--- using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Drawing.Imaging; using System.IO; namespace StudyCase.upload { /// /// Summary description for uploadimage. /// public class uploadimage : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Image ImgPreview; protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile; protected System.Web.UI.WebControls.Label lblErrInfo; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here // if (Request.QueryString["uploadId"] == null) // Response.Redirect("uploadimage.aspx?uploadId=" + Guid.NewGuid().ToString()); if(!Page.IsPostBack) { ImgPreview.Visible=false; } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion /// /// generate the thumb of image /// /// the width of thumb /// the height of thumb /// the stamp string on the thumb /// left point of postion of stamp /// right point of postion of stamp /// true for auto size/ false for fixed size void GetThumbnailImage(int width,int height,string strInfo,int left,int right,bool switcher) { 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)); if(switcher==false) { 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; } else { int h=oldimage.Height; int w=oldimage.Width; int k=12; w=w/k; h=h/k; System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(w, h,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; } private void Button1_Click(object sender, System.EventArgs e) { int width,height,left,right; string strAddInfo="for testing jacky"; bool switcher=true; width=400; height=300; left=0; right=0; //uploadfile is a ID of HTMLfileupload control if(!(uploadFile.PostedFile.ContentLength>0)) { lblErrInfo.Text="please select a file firstly!"; } else { if(uploadFile.PostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0) { lblErrInfo.Text="Only image is allowed"; return; } //limited the size of uploading file if(uploadFile.PostedFile.ContentLength<1024) { this.lblErrInfo.Text="file max size is 1024 "; } 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,switcher); } } } } }

转载于:https://www.cnblogs.com/bloodycool/archive/2006/09/11/501202.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值