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

【转载】C#生成图片的缩略图

程序员文章站 2022-04-08 17:51:40
图片处理是C#程序开发中时常会涉及到的一个业务,除了图像的上传、保存以及下载等功能外,根据上传的图片生成一个缩略图也是常见业务,在C#语言中,可以通过Image类提供的相关方法对图片进行操作,如指定宽高对图片进行缩放, 指定高宽裁减裁剪图片、生成图片水印等。 定义一个图片处理工具类PicDeal,该 ......

图片处理是c#程序开发中时常会涉及到的一个业务,除了图像的上传、保存以及下载等功能外,根据上传的图片生成一个缩略图也是常见业务,在c#语言中,可以通过image类提供的相关方法对图片进行操作,如指定宽高对图片进行缩放, 指定高宽裁减裁剪图片、生成图片水印等。

定义一个图片处理工具类picdeal,该类实现了生成缩略图、在图片上生成图片水印等相应功能。具体的业务代码如下:

    /// <summary>
    /// 枚举,生成缩略图模式
    /// </summary>
    public enum thumbnailmod : byte
    {
        /// <summary>
        /// hw
        /// </summary>
        hw,
        /// <summary>
        /// w
        /// </summary>
        w,
        /// <summary>
        /// h
        /// </summary>
        h,
        /// <summary>
        /// cut
        /// </summary>
        cut
    };
    /// <summary>
    /// 操作图片类, 生成缩略图,添加水印
    /// </summary>
    public static class picdeal
    {
        private static hashtable htmimes = new hashtable();
        internal static readonly string allowext = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
        #region 生成缩略图
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalimagepath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static bool makethumbnail(string originalimagepath, int width, int height, thumbnailmod mode)
        {
            string thumbnailpath = originalimagepath.substring(0, originalimagepath.lastindexof('.')) + "s.jpg";
            image originalimage = image.fromfile(originalimagepath);
            int towidth = width;
            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = originalimage.width;
            int oh = originalimage.height;
            switch (mode)
            {
                case thumbnailmod.hw://指定高宽缩放(可能变形)                
                    break;
                case thumbnailmod.w://指定宽,高按比例                    
                    toheight = originalimage.height * width / originalimage.width;
                    break;
                case thumbnailmod.h://指定高,宽按比例
                    towidth = originalimage.width * height / originalimage.height;
                    break;
                case thumbnailmod.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;
                    }
                    break;
                default:
                    break;
            }
            //新建一个bmp图片
            image bitmap = new bitmap(towidth, toheight);
            //新建一个画板
            graphics g = graphics.fromimage(bitmap);
            //设置高质量插值法
            g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
            //设置高质量,低速度呈现平滑程度
            g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
            //清空画布并以透明背景色填充
            g.clear(system.drawing.color.transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.drawimage(originalimage, new rectangle(0, 0, towidth, toheight),
                new rectangle(x, y, ow, oh),
                graphicsunit.pixel);
            bool isok = false;
            try
            {
                //以jpg格式保存缩略图
                bitmap.save(thumbnailpath, imageformat.jpeg);
                isok = true;
            }
            catch (exception)
            {
                thumbnailpath = originalimagepath;
            }
            finally
            {
                originalimage.dispose();
                bitmap.dispose();
                g.dispose();
            }
            return isok;
        }
        #endregion
        #region 在图片上生成图片水印
        ///// <summary>
        ///// 在图片上生成图片水印
        ///// </summary>
        ///// <param name="path">原服务器图片路径</param>
        ///// <param name="path_syp">生成的带图片水印的图片路径</param>
        ///// <param name="path_sypf">水印图片路径</param>
        /// <summary>
        /// 在图片上生成图片水印
        /// </summary>
        /// <param name="path">原服务器图片路径</param>
        /// <param name="path_sypf">水印图片路径</param>
        public static void addwaterpic(string path, string path_sypf)
        {
            try
            {
                image image = image.fromfile(path);
                image copyimage = image.fromfile(path_sypf);
                graphics g = graphics.fromimage(image);
                g.drawimage(copyimage, new system.drawing.rectangle(image.width - copyimage.width, image.height - copyimage.height, copyimage.width, copyimage.height), 0, 0, copyimage.width, copyimage.height, system.drawing.graphicsunit.pixel);
                g.dispose();
                image.save(path + ".temp");
                image.dispose();
                system.io.file.delete(path);
                file.move(path + ".temp", path);
            }
            catch
            { }
        }
        #endregion
        /// <summary>
        /// 公共方法
        /// </summary>
        private static void getimgtype()
        {
            htmimes[".jpe"] = "image/jpeg";
            htmimes[".jpeg"] = "image/jpeg";
            htmimes[".jpg"] = "image/jpeg";
            htmimes[".png"] = "image/png";
            htmimes[".tif"] = "image/tiff";
            htmimes[".tiff"] = "image/tiff";
            htmimes[".bmp"] = "image/bmp";
        }

        #region 返回新图片尺寸
        /// <summary>
        /// 返回新图片尺寸
        /// </summary>
        /// <param name="width">原始宽</param>
        /// <param name="height">原始高</param>
        /// <param name="maxwidth">新图片最大宽</param>
        /// <param name="maxheight">新图片最大高</param>
        /// <returns></returns>
        public static size resizeimage(int width, int height, int maxwidth, int maxheight)
        {
            decimal max_width = (decimal)maxwidth;
            decimal max_height = (decimal)maxheight;
            decimal aspect_ratio = max_width / max_height;
            int newwidth, newheight;
            decimal originalwidth = (decimal)width;
            decimal originalheight = (decimal)height;
            if (originalwidth > max_width || originalheight > max_height)
            {
                decimal factor;
                // determine the largest factor 
                if (originalwidth / originalheight > aspect_ratio)
                {
                    factor = originalwidth / max_width;
                    newwidth = convert.toint32(originalwidth / factor);
                    newheight = convert.toint32(originalheight / factor);
                }
                else
                {
                    factor = originalheight / max_height;
                    newwidth = convert.toint32(originalwidth / factor);
                    newheight = convert.toint32(originalheight / factor);
                }
            }
            else
            {
                newwidth = width;
                newheight = height;
            }
            return new size(newwidth, newheight);
        }
        #endregion
    }

 

备注:原文转载自c#生成图片的缩略图_it技术小趣屋