首页 > 编程 > .NET > 正文

Asp.net 文件上传基类(取得文件后缀名,保存文件,加入文字水印)

2024-07-10 13:05:52
字体:
来源:转载
供稿:网友

/############################################
版权声明:
文章内容为本站编辑,创作.你可以任意转载、发布、使用但请务必标明文章原始出处及本声明
http://www.opent.cn  作者:浪淘沙
############################################/

using system;
using system.data;
using system.configuration;
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;
using system.drawing;
using system.io;
using system.drawing.imaging;

namespace ec
{
    /// <summary>
    /// 上传类
    /// </summary>
    public class uploadobj
    {

        public uploadobj()
        {
            //
            // todo: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 允许文件上传的类型枚举
        /// </summary>
        public enum filetype
        {
           jpg,gif,bmp,png           
        }

        #region 取得文件后缀
        /// <summary>
        /// 取得文件后缀
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <returns></returns>
        public static string getfileextends(string filename)
        {
            string ext = null;
            if (filename.indexof('.') > 0)
            {
                string[] fs = filename.split('.');
                ext = fs[fs.length - 1];
            }
            return ext;
        }
        #endregion

        #region 检测文件是否合法
        /// <summary>
        /// 检测上传文件是否合法
        /// </summary>
        /// <param name="fileextends">文件后缀名</param>
        /// <returns></returns>
        public static bool checkfileextends(string fileextends)
        {
            bool status = false;
            fileextends = fileextends.tolower();
            string[] fe = enum.getnames(typeof(filetype));
            for (int i = 0; i < fe.length; i++)
            {
                if (fe[i].tolower() == fileextends)
                {
                    status = true;
                    break;
                }
            }
            return status;
        }
        #endregion

        #region 保存文件
       /// <summary>
       /// 保存文件
       /// </summary>
       /// <param name="fpath">全路径,server.mappath()</param>
       /// <param name="myfileupload">上传控件</param>
       /// <returns></returns>
        public static string photosave(string fpath,fileupload myfileupload)
        {  
            string s = "";
            string fileextends = "";
            string filename = myfileupload.filename;
            if (filename != "")
            {
                //取得文件后缀
                fileextends = ec.uploadobj.getfileextends(filename);
                if (!ec.uploadobj.checkfileextends(fileextends))
                {
                    ec.messageobject.showpre("上传文件类型不合法");                 
                }
                random rd = new random();
                s = ec.randomobject.daterndname(rd) + "." + fileextends;
                string file = fpath + "//" + s;
                try
                {
                    myfileupload.saveas(file);
                }
                catch (exception ee)
                {
                    throw new exception(ee.tostring());
                }
            }
            return s;
        }

        #endregion

        #region 加入文字水印

        /// <summary>
        /// 加入文字水印
        /// </summary>
        /// <param name="filename">文件名称路径(全路径)</param>
        /// <param name="text">文件</param>
        public void addtexttoimg(string filename, string text)
        {
            if (!file.exists(filename))
            {
                throw new filenotfoundexception("文件不存在");
            }
            if (text == string.empty)
            {
                return;
            }
            //判断文件类型是否为图像类型

            system.drawing.image image = system.drawing.image.fromfile(filename);
            bitmap bitmap = new bitmap(image, image.width, image.height);
            graphics g = graphics.fromimage(bitmap);
            float fontsize = 12.0f;//字体大小
            float textwidth = text.length * fontsize;//文本的长度
            //下面定义一个矩形区域,以后在这个矩形里面画上白底黑字
            float rectx = 0;
            float recty = 0;
            float rectwidth = text.length * (fontsize + 8);
            float rectheight = fontsize + 8;
            //声明矩形域
            rectanglef textarea = new rectanglef(rectx, recty, rectwidth, rectheight);
            font font = new font("宋体", fontsize);//定义字体
            brush whitebrush = new solidbrush(color.white);//白笔刷,画文字用
            brush blackbrush = new solidbrush(color.black);//黑笔刷,画背景用
            g.fillrectangle(blackbrush, rectx, recty, rectwidth, rectheight);
            g.drawstring(text, font, whitebrush, textarea);
            memorystream ms = new memorystream();
            bitmap.save(ms, imageformat.jpeg);
            //输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
            //response.clear();
            //response.contenttype = "image/jpeg";
            //response.binarywrite(ms.toarray());
            g.dispose();
            bitmap.dispose();
            image.dispose();       
        }
        #endregion
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表