当前位置:编程学习 > C#/ASP.NET >>

C#压缩图片,可以指定图片模板高宽

今天要在web程序处理图片,指定图片的高宽大小。google了一把资料。觉得此方法挺不错的,大家可以借鉴一下,如果小弟写的有不对的地方请大家指点一下:以下代码在winform写的,在web下测试可以使用。

\代码
//生成缩略图函数
        //顺序参数:源图文件流、缩略图存放地址、模版宽、模版高
        //注:缩略图大小控制在模版区域内
        public static void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight)
        {
            //从文件取得图片对象,并使用流中嵌入的颜色管理信息
            System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
            //缩略图宽、高
            System.Double newWidth = myImage.Width, newHeight = myImage.Height;
            //宽大于模版的横图
            if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
            {
                if (myImage.Width > templateWidth)
                {
                    //宽按模版,高按比例缩放
                    newWidth = templateWidth;
                    newHeight = myImage.Height * (newWidth / myImage.Width);
                }
            }
            //高大于模版的竖图
            else
            {
                if (myImage.Height > templateHeight)
                {
                    //高按模版,宽按比例缩放
                    newHeight = templateHeight;
                    newWidth = myImage.Width *&
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,