在最近开发项目的时候有时候需要进行图片的合并,即将两张图片合并成功一张图片
合并图片的代码:
#region 两张图片的合并 /// <summary >
/// 将Image对象转化成二进制流
/// </summary >
/// <param name="image" > </param >
/// <returns > </returns >
public byte[] ImageToByteArray(Image image)
{
//实例化流
MemoryStream imageStream = new MemoryStream();
//将图片的实例保存到流中
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//保存流的二进制数组
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = ;
//将流泻如数组中
imageStream.Read(imageContent, , (int)imageStream.Length);
return imageStream.ToArray();
} /// <summary>
/// 两张图片的合并
/// </summary>
/// <param name="mainImgePath">主图的路径</param>
/// <param name="secondaryImgePath">被合并的图片的图片的路径(次图)</param>
/// <param name="x">将次图 合并的主图的坐标的 X轴</param>
/// <param name="y">将次图 合并的主图的坐标的 Y轴</param>
/// <param name="width">合并后次图的宽度</param>
/// <param name="height">合并后次图的高度</param>
/// <returns></returns>
public Image BuildImage(string mainImgePath, string secondaryImgePath, int x, int y, int width, int height)
{
Image img = Image.FromFile(mainImgePath);
Image qr = Image.FromFile(secondaryImgePath);
Graphics g = Graphics.FromImage(img);
g.DrawImage(qr, x, y, width, height);
return img;
}
#endregion