C# 图形处理-缩略图,图片合并,图片写文字,图片调整

1.根据源图片生成缩略图

/// 
        /// 根据源图片生成缩略图
        /// 
        /// 源图(大图)物理路径
        /// 缩略图物理路径(生成的缩略图将保存到该物理位置)
        /// 缩略图宽度
        /// 缩略图高度
        /// 缩略图缩放模式(取值"HW":指定高宽缩放,可能变形;取值"W":按指定宽度,高度按比例缩放;取值"H":按指定高度,宽度按比例缩放;取值"Cut":按指定高度和宽度裁剪,不变形);取值"DB":等比缩放,以值较大的作为标准进行等比缩放
        /// 即将生成缩略图的文件的扩展名(仅限:JPG、GIF、PNG、BMP)
        public static void MakeThumbnail(string imgPath_old, string imgPath_new, int width, int height, string mode, string type)
        {
            Image img = Image.FromFile(imgPath_old);
            int towidth = width; int toheight = height;
            int x = 0; int y = 0; int ow = img.Width;
            int oh = img.Height;
            switch (mode)
            {
                case "HW":  //指定高宽缩放(可能变形)                  
                    break;
                case "W":  //指定宽,高按比例
                    toheight = img.Height * width / img.Width;
                    break;
                case "H":  //指定高,宽按比例
                    towidth = img.Width * height / img.Height;
                    break;
                case "Cut": //指定高宽裁减(不变形)
                    if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
                    {
                        oh = img.Height;
                        ow = img.Height * towidth / toheight;
                        y = 0; x = (img.Width - ow) / 2;
                    }
                    else
                    {
                        ow = img.Width;
                        oh = img.Width * height / towidth;
                        x = 0; y = (img.Height - oh) / 2;
                    } break;

                case "DB":    // 按值较大的进行等比缩放(不变形)
                    if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
                    {
                        toheight = height;
                        towidth = img.Width * height / img.Height;
                    }
                    else
                    {
                        towidth = width;
                        toheight = img.Height * width / img.Width;
                    } break;
                default:
                    break;
            }
            //新建一个bmp图片
            Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.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(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel); try
            {
                //保存缩略图
                switch (type.ToUpper())
                {
                    case "JPG":
                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case "GIF":
                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    case "PNG":
                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    case "BMP":
                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Bmp);
                        break;
                    default:
                        break;
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                img.Dispose();
                bitmap.Dispose(); g.Dispose();
            }
        }

2.生成文字图片

/// 
        /// 生成文字图片
        /// 
        /// 
        /// 
        /// 
        public static Image CreateImage(string text, bool isBold, int fontSize)
        {
            int wid = 400;
            int high = 200;
            Font font;
            if (isBold)
            {
                font = new Font("Arial", fontSize, FontStyle.Bold);

            }
            else
            {
                font = new Font("Arial", fontSize, FontStyle.Regular);

            }
            //绘笔颜色
            SolidBrush brush = new SolidBrush(Color.Black);
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            Bitmap image = new Bitmap(wid, high);
            Graphics g = Graphics.FromImage(image);
            SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);//得到文本的宽高
            int width = (int)(sizef.Width + 1);
            int height = (int)(sizef.Height + 1);
            image.Dispose();
            image = new Bitmap(width, height);
            g = Graphics.FromImage(image);
            g.Clear(Color.White);//透明

            RectangleF rect = new RectangleF(0, 0, width, height);
            //绘制图片
            g.DrawString(text, font, brush, rect);
            //释放对象
            g.Dispose();
            return image;
        }

3.合并两个图片

///   
        /// 合并图片  
        ///   
        ///   
        ///   
        ///   
        public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
        {

            Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height + img.Height);

            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);     

            //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框    

            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);    

            g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height + yDeviation, img.Width, img.Height);
            GC.Collect();
            return bmp;
        }

4.图片大小调整

///   
        /// Resize图片  
        ///   
        /// 原始Bitmap  
        /// 新的宽度  
        /// 新的高度  
        /// 保留着,暂时未用  
        /// 处理以后的图片  
        public static Image ResizeImage(Image bmp, int newW, int newH, int mode)
        {
            try
            {
                Image b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                // 插值算法的质量    
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height),
                            GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }

5.如果文章对你有帮助,记得一键三连❤

你可能感兴趣的:(C#-图像处理,图像处理)