⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 img.aspx.cs

📁 这个是个未完成品 演示地址:http://www.yyzyq.com 仅以此例给正在学习编程爱好.net的朋友 希望能给大家带来帮助
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Editor_include_img : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        Response.Expires = 0;
        Response.Cache.SetNoStore();
        Response.AppendHeader("Pragma", "no-cache");
    }

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.btnUpLoad.Click += new System.EventHandler(this.btnUpLoad_Click);
        this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnUpLoad_Click(object sender, System.EventArgs e)
    {        
        upfile();
    }

    #region 上传文件
    /// <summary>
    /// 文件上传
    /// </summary>
    /// <returns>文件上传到服务器的路径</returns>
    void upfile()
    {
        string strWidth = "";
        Random myrd = new Random();
        string path, filename;	//上传文件路径,上传文件名称
        path = Server.MapPath("../../upfile/old/");
        filename = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + myrd.Next(1000).ToString();		//上传文件名称

        //取得文件的扩展名
        string fileExtension = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToLower();
        path = path + filename + fileExtension;
        if (fileExtension != ".jpg" && fileExtension != ".gif" && fileExtension != ".jpeg")
        {
            JScript.Alert("错误!!文件类型必须为jpg或者gif!");            
        }
        else
        {
            File1.PostedFile.SaveAs(path);
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            int w = image.Width;
            strWidth = (w >= 600) ? " width=\"600\" " : "";
            int h = image.Height;
            g.DrawImage(image, 0, 0, w, h);
            w = GetFontSize(w);
            System.Drawing.Font f = new System.Drawing.Font("verdana", w);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.White);
            string addtext = "图片来自:http://www.yyzyq.com(永远在一起)";
            h = h - GetFontHeight(h);
            g.DrawString(addtext, f, b, 0, h);
            g.Dispose();

            //保存加水印过后的图片
            string newpath = Server.MapPath("../../upfile/users/") + filename + fileExtension;
            image.Save(newpath);
            image.Dispose();
            string strFileName = "upfile/users/" + filename + fileExtension;
            string js = @"<script language=javascript>";
            js += " window.returnValue =  '<img src= \"" + strFileName + "\" " + strWidth + "/>';";
            js += "window.close();";
            js += "</script>";
            Response.Write(js);        
        }         
    }

    public bool ThumbnailCallback()
    {
        return false;
    }    

    private int GetFontHeight(int intHeight)
    {
        if (intHeight <= 100)
            return 10;
        if (intHeight > 100 && intHeight <= 200)
            return 15;
        else if (intHeight > 200 && intHeight <= 300)
            return 20;
        else if (intHeight > 300 && intHeight <= 400)
            return 25;
        else if (intHeight > 400 && intHeight <= 500)
            return 30;
        else if (intHeight > 500 && intHeight <= 600)
            return 35;
        else if (intHeight > 600 && intHeight <= 800)
            return 40;
        else
            return 50;
    }

    private int GetFontSize(int intWidth)
    {
        if (intWidth <= 100)
            return 4;
        else if (intWidth > 100 && intWidth <= 200)
            return 6;
        else if (intWidth > 200 && intWidth <= 300)
            return 8;
        else if (intWidth > 300 && intWidth <= 400)
            return 10;
        else if (intWidth > 400 && intWidth <= 500)
            return 12;
        else if (intWidth > 500 && intWidth <= 600)
            return 14;
        else if (intWidth > 600 && intWidth <= 800)
            return 16;
        else
            return 20;
    }

    #endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -