按比例调整图片尺寸
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
合成图片
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 }
合成前:
合成后: