ASP.NET之上传文件管理策略(2)

分类: asp.net技巧   出处:iocblog整理  更新时间:2008-12-04   添加到收藏  

    #region
    /// <summary>
    /// 计算文件大小函数,size为字节大小
    /// </summary>
    /// <param name="size">初始文件大小</param>
    /// <returns></returns>
    public string countsize(long size)
    {
        string m_strsize = "";
        long factsize = 0; factsize = size;
        if (factsize <= 1024)
            m_strsize = factsize.tostring() + "byte";
        if(factsize>=1024&&factsize<=1048576)
        m_strsize=(factsize/1024).tostring ()+"k";
        if (factsize >= 1048576 && factsize <= 10485760)
        m_strsize = (factsize / 1024 / 1024).tostring() + "m";
        return m_strsize;
    }
    #endregion

    #region    savesmallphoto
    /// <summary>
    /// 高清晰缩略图算法
    /// </summary>
    /// <param name="postfile">图片文件对象</param>
    /// <param name="saveimg">要保存为缩略图的源文件</param>
    /// <param name="width">宽度</param>
    /// <param name="height">高度</param>
    public void makesmallimg(system.web.httppostedfile postfile, string saveimg, system.double width, system.double height)
    {

        //sourcephotoname
        string m_originalfilename = postfile.filename;
        string m_strgoodfile = saveimg;

        //getphotoobject from sourcefile
        system.drawing.image m_image = system.drawing.image.fromstream(postfile.inputstream, true);

        system.double newwidth, newheight;
        if (m_image.width > m_image.height)
        {
            newwidth = width;
            newheight = m_image.height * (newwidth / m_image.width);
        }
        else
        {
            newheight = height;
            newwidth = (newheight / m_image.height) * m_image.width;
        }

        if (newwidth > width)
        {
            newwidth = width;
        }
        if (newheight > height)
        {
            newheight = height;
        }

        //getphotosize
        system.drawing.size size =new system.drawing.size((int)newwidth, (int)newheight);
        //the new of bimp photo
        system.drawing.image bitmap = new system.drawing.bitmap(size.width, size.height);
        // the new of palette
        system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);
        // set hightquality arithmetic for graphics
        g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
        //设置高质量,低速度呈现平滑程度
        g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
        //clearcanvas
        g.clear(system.drawing.color.white);
        //在指定位置画图
        g.drawimage(m_image, new system.drawing.rectangle(0, 0, bitmap.width, bitmap.height),
        new system.drawing.rectangle(0, 0, m_image.width, m_image.height),
        system.drawing.graphicsunit.pixel);

        //savephoto of hightfocus
        bitmap.save(m_strgoodfile, system.drawing.imaging.imageformat.jpeg);
        //disposeres
        g.dispose();
        m_image.dispose();
        bitmap.dispose();
    }

    #endregion
}

(www.iocblog.net 文章来源)

上一页 [1] [2]


Tag: 上传



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。