zoukankan      html  css  js  c++  java
  • 上传图片加水印

    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.IO;

    namespace MikeCat
    {
        
    /// <summary>
        
    /// MikeCat_WaterMark 的摘要说明。
        
    /// *******************************
        
    /// 作者:迈克老猫
        
    /// 功能:上传图片加入水印
        
    /// EMAIL:mikecat#mikecat.net
        
    /// *******************************
        
    /// </summary>

        public class MikeCat_WaterMark : System.Web.UI.Page
        
    {
            
    protected System.Web.UI.WebControls.Button Button1;
            
    protected System.Web.UI.HtmlControls.HtmlInputFile File1;
            
    protected System.Web.UI.WebControls.Image Image1;
            
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
            
    protected System.Web.UI.WebControls.Label Label1;
            
    protected System.Web.UI.WebControls.Button Button2;
        
            
    private void Page_Load(object sender, System.EventArgs e)
            
    {
                
    // 在此处放置用户代码以初始化页面
                if(!Page.IsPostBack)
                
    {
                    Image1.ImageUrl
    ="mikepp.gif";
                }

            }


            
    Web 窗体设计器生成的代码

            
    private void Button1_Click(object sender, System.EventArgs e)
            
    {
                
    if(File1.PostedFile.FileName.Trim()!="")
                
    {
                    
    //上传文件
                    string extension = Path.GetExtension(File1.PostedFile.FileName).ToLower();
                    
    string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                    
    string path = Server.MapPath("."+ "/upload/" + fileName + extension;
                    File1.PostedFile.SaveAs(path);

                    
    //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
                    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                    Graphics g 
    = Graphics.FromImage(image);
                    g.DrawImage(image, 
    00, image.Width, image.Height);
                    Font f 
    = new Font("Verdana",16);
                    Brush b 
    = new SolidBrush(Color.Blue);
                    
    string addText = "老猫的理想http://www.mikecat.net";
                    g.DrawString(addText, f, b, 
    1010);
                    g.Dispose();

                    
    //保存加水印过后的图片,删除原始图片
                    string newPath = Server.MapPath("."+ "/upload/" + fileName + "_new" + extension;
                    image.Save(newPath);
                    image.Dispose();
                    
    if(File.Exists(path))
                    
    {
                        File.Delete(path);
                    }

            
                    Image1.ImageUrl
    =newPath;
    //                Response.Redirect(newPath);
                }


            }


            
    private void Button2_Click(object sender, System.EventArgs e)
            
    {
                
    //上传文件
                string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
                
    string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                
    string path = Server.MapPath("."+ "/upload/" + fileName + extension;
                File1.PostedFile.SaveAs(path);

                
                
    //加图片水印
                System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                System.Drawing.Image copyImage 
    = System.Drawing.Image.FromFile( Server.MapPath("."+ "/mikepp.gif");
                Graphics g 
    = Graphics.FromImage(image);
                g.DrawImage(copyImage, 
    new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 00, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                g.Dispose();

                
    //保存加水印过后的图片,删除原始图片
                string newPath = Server.MapPath("."+ "/upload/" + fileName + "_new" + extension;
                image.Save(newPath);
                image.Dispose();
                
    if(File.Exists(path))
                
    {
                    File.Delete(path);
                }


                Image1.ImageUrl
    =newPath;
            }

        }

    }

  • 相关阅读:
    Java Mockito 笔记
    J2EE 练习题
    CXF 教程 (二)
    CXF 教程(一)
    Java 动态代理
    常用 Git 命令汇总
    JXL 简单示例
    Visual Studio 技巧
    TreeView双击节点而不改变节点的折叠/展开状态
    称3次,找出坏鸡蛋
  • 原文地址:https://www.cnblogs.com/adam/p/959895.html
Copyright © 2011-2022 走看看