using system;
using system.web;
using system.web.ui;
using system.io;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;
using system.text.regularexpressions;

namespace baseproject.classes.util
{
    public class upload
    {
        ///


        /// 指定路径 图片大小
        ///

        ///
        ///
        ///
        ///
        public string getsqlfile(string dir, int width, int height)
        {
            string sqlfile = "";
            string path = httpcontext.current.request.physicalapplicationpath.tostring() "upload/";
            path = dir;
            if (directory.exists(path))
            {

            }
            else
            {
                directory.createdirectory(path);
            }
            if (directory.exists(path "/small"))
            {
            }
            else
            {
                directory.createdirectory(path "/small");
            }
            httpfilecollection files = httpcontext.current.request.files;
            if (files[0].filename.tostring().length > 0)
            {
              
                string filename = files[0].filename.tostring();
                string datestr = new stringoperate().convertchinesecharactertospelling(regex.replace(filename, "[^\\u4e00-\\u9fa5]", "")) datetime.now.tostring("yyyymmddhmmfff") ;
                string ext = filename.substring(filename.lastindexof(".")).tolower();
                if (ext != ".bmp" && ext != ".jpg" && ext != ".gif" && ext != ".jpeg")
                {
                    httpcontext.current.response.write("");
                    return "";
                }
                files[0].saveas(path "/" datestr ext);
                #region 生成小图
                string originalfilename = path "/" datestr ext;
              
                //缩小的倍数
                int iscale = 1;
                //从文件取得图片对象
                image image = null;
                try
                {
                    image = image.fromfile(originalfilename);
                }
                catch
                {
                    //
                    try
                    {
                        file.delete(originalfilename);
                        image.dispose();

                    }
                    catch
                    {
                    }
                    httpcontext.current.response.write("");
                    return "";

                }
                int hi = 0;
                int wi = 0;
                wi = width;
                hi = height;
                size size = new size(wi, hi);
                //新建一个bmp图片
                image bitmap = new bitmap(size.width, size.height);
                //新建一个画板
                graphics g = graphics.fromimage(bitmap);
                //设置高质量插值法
                g.interpolationmode = interpolationmode.high;
                //设置高质量,低速度呈现平滑程度
                g.smoothingmode = smoothingmode.highquality;
                //清空一下画布
                g.clear(color.blue);
                //在指定位置画图
                g.drawimage(image, new rectangle(0, 0, bitmap.width, bitmap.height), new rectangle(0, 0, image.width, image.height), graphicsunit.pixel);
                if (ext == ".jpg" || ext == ".jpeg")
                    bitmap.save(path "/small/" datestr ext, imageformat.jpeg);
                if (ext == ".gif")
                    bitmap.save(path "/small/" datestr ext, imageformat.gif);
                if (ext == ".bmp")
                    bitmap.save(path "/small/" datestr ext, imageformat.bmp);
                image.dispose();
                bitmap.dispose();
                g.dispose();
                #endregion
                sqlfile = datestr ext;

                try
                {
                    image.dispose();
                    bitmap.dispose();
                    g.dispose();
                }
                catch (exception ex)
                {
                    string exc = ex.message.tostring();
                    httpcontext.current.response.write("");
                }
            }
            else
            {
                sqlfile = "";
            }
            return sqlfile;
        }
        ///


        /// 指定路径 图片大小
        ///

        ///
        ///
        ///
        ///
        public string getsqlfilesmallimage(string dir, int width, int height)
        {
            string sqlfile = "";
            string path = httpcontext.current.request.physicalapplicationpath.tostring() "upload/";
            path = dir;
            if (directory.exists(path))
            {

            }
            else
            {
                directory.createdirectory(path);
            }
            if (directory.exists(path "/small"))
            {
            }
            else
            {
                directory.createdirectory(path "/small");
            }
            httpfilecollection files = httpcontext.current.request.files;
            if (files[0].filename.tostring().length > 0)
            {
                string filename = files[0].filename.tostring();
                string tempfilename = filename.substring(filename.lastindexof('\\') 1, filename.lastindexof('.') - (filename.lastindexof('\\') 1));
                string datestr = new stringoperate().convertchinesecharactertospelling(regex.replace(filename, "[^\\u4e00-\\u9fa5]", "")) datetime.now.tostring("yyyymmddhmmfff");
                string ext = filename.substring(filename.lastindexof(".")).tolower();
                if (ext != ".bmp" && ext != ".jpg" && ext != ".gif" && ext != ".jpeg")
                {
                    httpcontext.current.response.write("");
                    return "";
                }
                files[0].saveas(path "/" datestr ext);
                #region 生成小图
                string originalfilename = path "/" datestr ext;
              
                //缩小的倍数
                //int iscale = 1;
                //从文件取得图片对象
                image image = null;
                try
                {
                    image = image.fromfile(originalfilename);
                }
                catch
                {
                    //
                    try
                    {
                        file.delete(originalfilename);
                        image.dispose();

                    }
                    catch
                    {
                    }
                    httpcontext.current.response.write("");
                    return "";

                }
                int hi = 0;
                int wi = 0;
                wi = width;
                hi = height;
                size size = new size(wi, hi);
                //新建一个bmp图片
                image bitmap = new bitmap(size.width, size.height);
                //新建一个画板
                graphics g = graphics.fromimage(bitmap);
                //设置高质量插值法
                g.interpolationmode = interpolationmode.high;
                //设置高质量,低速度呈现平滑程度
                g.smoothingmode = smoothingmode.highquality;
                //清空一下画布
                g.clear(color.blue);
                //在指定位置画图
                g.drawimage(image, new rectangle(0, 0, bitmap.width, bitmap.height), new rectangle(0, 0, image.width, image.height), graphicsunit.pixel);
                if (ext == ".jpg" || ext == ".jpeg")
                    bitmap.save(path "/small/" datestr ext, imageformat.jpeg);
                if (ext == ".gif")
                    bitmap.save(path "/small/" datestr ext, imageformat.gif);
                if (ext == ".bmp")
                    bitmap.save(path "/small/" datestr ext, imageformat.bmp);
                image.dispose();
                bitmap.dispose();
                g.dispose();
                #endregion
                sqlfile = datestr ext;

                try
                {
                    image.dispose();
                    bitmap.dispose();
                    g.dispose();
                    system.io.file.delete(path "/" datestr ext);
                }
                catch (exception ex)
                {
                    string exc = ex.message.tostring();
                    httpcontext.current.response.write("");
                }
            }
            else
            {
                sqlfile = "";
            }
            return sqlfile;
        }

    }
}

posted on 2009-04-02 18:36 sanmao 阅读(695) 评论(1)    

feedback:
# re: 上传图片类代码
2011-03-10 17:33 |
这是java代码?  回复  
  

只有注册用户后才能发表评论。


网站导航:
              
 

常用链接

留言簿(5)

上传图片类代码 -凯发k8网页登录

搜索

  •  

最新评论

阅读排行榜

评论排行榜

网站地图