博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成缩率图项目实例
阅读量:5302 次
发布时间:2019-06-14

本文共 4123 字,大约阅读时间需要 13 分钟。

 

生成缩率图项目实例

///         /// 生成缩略图        ///         /// 源图路径(绝对路径)        /// 缩略图路径(绝对路径)        /// 缩略图宽度        /// 缩略图高度        /// "HW":指定高宽缩放(不变形)            public static void MakeThumbnailImage(string fileName, string newFileName, int width, int height, string mode)        {            Image originalImage = Image.FromFile(fileName);            int Imgwidth = width;            int Imgheight = height;            int towidth = width;            int toheight = height;            int tox = 0;            int toy = 0;            int x = 0;            int y = 0;            int ow = originalImage.Width;            int oh = originalImage.Height;            switch (mode)            {                case "HW"://等高宽缩放(不变形)                    if ((double)ow / (double)oh > (double)towidth / (double)toheight)                    {                        towidth = width;                        toheight = Convert.ToInt32((double)oh * (double)towidth / (double)ow);                        toy = (height - toheight) / 2;                        Imgwidth = width;                        Imgheight = height;                    }                    else                     {                        toheight = height;                        towidth = Convert.ToInt32((double)ow * (double)toheight / (double)oh);                        tox = (width - towidth) / 2;                                            }                    break;                case "W"://指定宽,高按比例                               towidth = originalImage.Width > width ? width : originalImage.Width;                    toheight = originalImage.Height * towidth / originalImage.Width;                    Imgwidth = towidth;                    Imgheight = toheight;                    break;                case "H"://指定高,宽按比例                    toheight = originalImage.Height > height ? height : originalImage.Height;                    towidth = originalImage.Width * toheight / originalImage.Height;                    Imgwidth = towidth;                    Imgheight = toheight;                    break;                case "Cut"://指定高宽裁减(不变形)                                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)                    {                        oh = originalImage.Height;                        ow = originalImage.Height * towidth / toheight;                        y = 0;                        x = (originalImage.Width - ow) / 2;                    }                    else                    {                        ow = originalImage.Width;                        oh = originalImage.Width * height / towidth;                        x = 0;                        y = (originalImage.Height - oh) / 2;                    }                    Imgwidth = width;                    Imgheight = height;                    break;                default:                    break;            }            //新建一个bmp图片            Bitmap b = new Bitmap(Imgwidth, Imgheight);            try            {                //新建一个画板                Graphics g = Graphics.FromImage(b);                //设置高质量插值法                g.InterpolationMode = InterpolationMode.HighQualityBicubic;                //设置高质量,低速度呈现平滑程度                g.SmoothingMode = SmoothingMode.HighQuality;                g.PixelOffsetMode = PixelOffsetMode.HighQuality;                g.CompositingQuality = CompositingQuality.HighQuality;                //清空画布并以透明背景色填充                g.Clear(Color.Transparent);                //在指定位置并且按指定大小绘制原图片的指定部分                g.DrawImage(originalImage, new Rectangle(tox, toy, towidth, toheight), new Rectangle(0, 0, ow, oh), GraphicsUnit.Pixel);                SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));                g.Dispose();            }            catch (System.Exception e)            {                throw e;            }            finally            {                originalImage.Dispose();                b.Dispose();            }        }
View Code

 

转载于:https://www.cnblogs.com/xiaoshi657/p/5334344.html

你可能感兴趣的文章
shell脚本解析10(练习4)------监视文件
查看>>
ubuntu重装mysql
查看>>
JS 学习笔记
查看>>
English trip -- VC(情景课)1 C What's your name?(review)
查看>>
redirect的错误用法asp.net怎么使用自定义错误
查看>>
在MyEclipse下统计工程的代码(package、行数、类个数)
查看>>
Erlcron分析学习
查看>>
idea 快捷键
查看>>
SimpleDateFormate的使用
查看>>
菜鸟运维笔记:Windows上用Xshell管理你的云主机
查看>>
JavaScript中的this
查看>>
Activity生命周期
查看>>
jsp
查看>>
OpenNI / NITE的Stable版更新
查看>>
03 基本数据结构 - 栈
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
微信小程序之随笔
查看>>
每秒处理10万高并发订单的乐视集团支付系统架构分享
查看>>
Lua_02
查看>>
ios蓝牙详解
查看>>