在最近开发项目的时候有时候需要进行图片的合并,即将两张图片合并成功一张图片
合并图片的代码:
1 #region 两张图片的合并 2 3 /// <summary > 4 /// 将Image对象转化成二进制流 5 /// </summary > 6 /// <param name="image" > </param > 7 /// <returns > </returns > 8 public byte[] ImageToByteArray(Image image) 9 { 10 //实例化流 11 MemoryStream imageStream = new MemoryStream(); 12 //将图片的实例保存到流中 13 image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); 14 //保存流的二进制数组 15 byte[] imageContent = new Byte[imageStream.Length]; 16 imageStream.Position = 0; 17 //将流泻如数组中 18 imageStream.Read(imageContent, 0, (int)imageStream.Length); 19 return imageStream.ToArray(); 20 } 21 22 /// <summary> 23 /// 两张图片的合并 24 /// </summary> 25 /// <param name="mainImgePath">主图的路径</param> 26 /// <param name="secondaryImgePath">被合并的图片的图片的路径(次图)</param> 27 /// <param name="x">将次图 合并的主图的坐标的 X轴</param> 28 /// <param name="y">将次图 合并的主图的坐标的 Y轴</param> 29 /// <param name="width">合并后次图的宽度</param> 30 /// <param name="height">合并后次图的高度</param> 31 /// <returns></returns> 32 public Image BuildImage(string mainImgePath, string secondaryImgePath, int x, int y, int width, int height) 33 { 34 Image img = Image.FromFile(mainImgePath); 35 Image qr = Image.FromFile(secondaryImgePath); 36 Graphics g = Graphics.FromImage(img); 37 g.DrawImage(qr, x, y, width, height); 38 return img; 39 } 40 #endregion