C# 图片处理
目录
1.按比例调整图片
2.合成图片
3.切割图片
1.按比例调整图片尺寸
1 #region 按比例调整图片尺寸 2 public static Bitmap GetThumbnail(Image bmp, int width, int height) 3 { 4 if (width == 0) 5 { 6 width = height * bmp.Width / bmp.Height; 7 } 8 if (height == 0) 9 { 10 height = width * bmp.Height / bmp.Width; 11 } 12 Image imgSource = bmp; 13 Bitmap outBmp = new Bitmap(width, height); 14 Graphics g = Graphics.FromImage(outBmp); 15 g.Clear(Color.Transparent); 16 // 设置画布的描绘质量 17 g.CompositingQuality = CompositingQuality.HighQuality; 18 g.SmoothingMode = SmoothingMode.HighQuality; 19 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 20 21 g.DrawImage(imgSource, new Rectangle(0, 0, width, height + 1), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); 22 23 g.Dispose(); 24 imgSource.Dispose(); 25 bmp.Dispose(); 26 return outBmp; 27 } 28 #endregion
2.合成图片
1 /// <summary> 2 /// 两种图片合并,类似相册,有个背景图,中间贴自己的目标图片 3 /// <param name="sourceImg">粘贴的源图片</param> 4 /// <param name="destImg">粘贴的目标图片</param> 5 /// <param name="name">合成图片存储路径</param> 6 /// </summary> 7 public void CombinImage(string sourceImg, string destImg, string name) 8 { 9 System.Drawing.Image imgBack = System.Drawing.Image.FromFile(sourceImg); //相框图片 10 System.Drawing.Image img = System.Drawing.Image.FromFile(destImg); //照片图片 11 12 Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height); 13 14 Graphics g = Graphics.FromImage(bmp); 15 g.Clear(Color.White); 16 g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); 17 //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框 18 19 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); 20 g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + 0, imgBack.Height / 2 - img.Height / 2 + 0, img.Width, img.Height); 21 GC.Collect(); 22 23 //保存指定格式图片 24 bmp.Save(name, ImageFormat.Png); 25 }
3.切割图片
/// <summary> /// 对URL链接图片进行切割操作 /// </summary> /// <param name="ImgUrl">URL链接图片</param> /// <param name="Height">需要图片的高</param> /// <returns></returns> public async Task CardCutImg(string ImgUrl,int Height) { //这段步骤可根据实际情况跳过 //传进来的地址可能会进行url编码操作,在这里需要对url进行解码 string url = HttpUtility.UrlDecode(ImgUrl); //这一步是对文件名称进行URL编码(根据实际情况而定,我这里查看的是阿里云上的图) var arr = url.Split("/"); string enCodeFileName = HttpUtility.UrlEncode(arr[arr.Length - 1]); string resultURL = arr.Where(x => !x.Contains(".png")).ToList().Join("/") + "/" + enCodeFileName; //将URL转成文件流来进行处理 Stream stream = await resultURL.GetAsStreamAsync(); //如下方式也可 //var wc = new System.Net.WebClient(); //byte[] imageData = wc.DownloadData(item.CardOssUrl); //Stream stream = new MemoryStream(imageData); //转成Image类型,获取图片的高宽属性 Image img = Image.FromStream(stream); Bitmap bitmap = new Bitmap(stream); var bt = new Bitmap(img.Width, Height); var grahics = Graphics.FromImage(bt); Rectangle resultRectangle = new Rectangle(0, 0, img.Width, Height);//指定在什么位置,绘制多大的图像 Rectangle sourceRectangle = new Rectangle(0, 0, img.Width, Height);//指定源图像的位置和大小 grahics.DrawImage(bitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel); //grahics.DrawString("这是水印", new Font("黑体", 20), new SolidBrush(Color.FromArgb(70, Color.Black)), 60, height / 2);//加水印 bt.Save("D:\\.jpg"); } //调用 await CardCutImg("https://keys-resource.oss-cn-shanghai.aliyuncs.com/WrongQuestionBook/13%2B1.png",118);