1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C# 图形处理-缩略图 图片合并 图片写文字 图片调整

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

时间:2021-11-25 10:53:54

相关推荐

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

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

/// <summary>/// 根据源图片生成缩略图/// </summary>/// <param name="imgPath_old">源图(大图)物理路径</param>/// <param name="imgPath_new">缩略图物理路径(生成的缩略图将保存到该物理位置)</param>/// <param name="width">缩略图宽度</param>/// <param name="height">缩略图高度</param>/// <param name="mode">缩略图缩放模式(取值"HW":指定高宽缩放,可能变形;取值"W":按指定宽度,高度按比例缩放;取值"H":按指定高度,宽度按比例缩放;取值"Cut":按指定高度和宽度裁剪,不变形);取值"DB":等比缩放,以值较大的作为标准进行等比缩放</param>/// <param name="type">即将生成缩略图的文件的扩展名(仅限:JPG、GIF、PNG、BMP)</param>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.生成文字图片

/// <summary>/// 生成文字图片/// </summary>/// <param name="text"></param>/// <param name="isBold"></param>/// <param name="fontSize"></param>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.合并两个图片

/// <summary> /// 合并图片 /// </summary> /// <param name="imgBack"></param> /// <param name="img"></param> /// <returns></returns> 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.图片大小调整

/// <summary> /// Resize图片 /// </summary> /// <param name="bmp">原始Bitmap</param> /// <param name="newW">新的宽度</param> /// <param name="newH">新的高度</param> /// <param name="mode">保留着,暂时未用</param> /// <returns>处理以后的图片</returns> 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.如果文章对你有帮助,记得一键三连❤

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。