zoukankan      html  css  js  c++  java
  • Asp.net(C#)给图片加上水印效果

     private void Btn_Upload_Click(object sender, System.EventArgs e)
            {
                if(UploadFile.PostedFile.FileName.Trim()!="")
                {
                    //上传文件
                    string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
                    string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
                    UploadFile.PostedFile.SaveAs(path);
    
                    //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
                    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(image, 0, 0, image.Width, image.Height);
                    Font f = new Font("Verdana", 32);
                    Brush b = new SolidBrush(Color.White);
                    string addText = AddText.Value.Trim();
                    g.DrawString(addText, f, b, 10, 10);
                    g.Dispose();
    
                    //加图片水印
                    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                    System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");
                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                    g.Dispose();
    
                    //保存加水印过后的图片,删除原始图片
                    string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
                    image.Save(newPath);
                    image.Dispose();
                    if(File.Exists(path))
                    {
                        File.Delete(path);
                    }
    
                    Response.Redirect(newPath);
                }
            }
  • 相关阅读:
    Windows 平台下的Mysql集群主从复制
    IOS 的loadView 及使用loadView中初始化View注意的问题。(死循环并不可怕)
    【2013625】K2+SAP集成应用解决方案在线研讨会
    to_char 和 to_date 经验分享
    Java向Access插入数据
    spring Bean的生命周期管理
    [置顶] 30分钟,让你成为一个更好的程序员
    Spring框架中Bean的生命周期
    Box2D的相关知识
    第八周项目二
  • 原文地址:https://www.cnblogs.com/yannis/p/2507347.html
Copyright © 2011-2022 走看看