欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 生成缩略图

程序员文章站 2022-06-30 12:03:44
个人觉得比较好用的缩略图方法,用在每次从FTP服务端下载图片,之后引用该方法进行压缩图片; 转载自:http://www.wxzzz.com/1413.html ......

个人觉得比较好用的缩略图方法,用在每次从ftp服务端下载图片,之后引用该方法进行压缩图片;

/// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="localimagepath">图片地址</param>
        /// <param name="thumbnailimagepath">缩略图地址</param>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片高度</param>
        /// <param name="p"></param>
        public static void getthumbnail(string localimagepath, string thumbnailimagepath, int width, int height)
        {
            system.drawing.image serverimage = system.drawing.image.fromfile(localimagepath);
            //画板大小
            int towidth = width;
            int toheight = height;
            //缩略图矩形框的像素点
            int x = 0;
            int y = 0;
            int ow = serverimage.width;
            int oh = serverimage.height;
 
            if (ow > oh)
            {
                toheight = serverimage.height * width / serverimage.width;
            }
            else
            {
                towidth = serverimage.width * height / serverimage.height;
            }
            //新建一个bmp图片
            system.drawing.image bm = new system.drawing.bitmap(width, height);
            //新建一个画板
            system.drawing.graphics g = system.drawing.graphics.fromimage(bm);
            //设置高质量插值法
            g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
            //设置高质量,低速度呈现平滑程度
            g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
            //清空画布并以透明背景色填充
            g.clear(system.drawing.color.white);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.drawimage(serverimage, new system.drawing.rectangle((width - towidth) / 2, (height - toheight) / 2, towidth, toheight),
                0, 0, ow, oh,
                system.drawing.graphicsunit.pixel);
            try
            {
                //以jpg格式保存缩略图
                bm.save(thumbnailimagepath, system.drawing.imaging.imageformat.jpeg);
            }
            catch (system.exception e)
            {
                throw e;
            }
            finally
            {
                serverimage.dispose();
                bm.dispose();
                g.dispose();
            }
        }

  转载自:http://www.wxzzz.com/1413.html