Main方法:
static void Main(string[] args)
{
var dateNow = DateTime.Now;
string saveFile = Environment.CurrentDirectory+"\puctures\" + dateNow.Year.ToString() + (dateNow.Month).ToString() + dateNow.Day.ToString() + dateNow.Hour.ToString() + dateNow.Minute.ToString() + dateNow.Second.ToString()+".jpg";
string imgBackPath = @"D:LearningPracticeDemoPracticeDemoimagesis407-046.jpg";
string imgBackExtension = Path.GetExtension(imgBackPath);
string imgPath = @"D:LearningPracticeDemoPracticeDemoimagesis104-052.jpg";
string imgExtension = Path.GetExtension(imgPath);
if (File.Exists(imgBackPath) == false||File.Exists(imgPath)==false||
(
imgBackExtension!=".gif"&&
imgBackExtension!=".png"&&
imgBackExtension!=".jpg"
)||(
imgBackExtension != ".gif" &&
imgBackExtension != ".png" &&
imgBackExtension != ".jpg"
)
)
{
Console.WriteLine("文件不存在或不支持该扩展名!");
Console.WriteLine("End");
}
Image imgBack = Image.FromFile(imgBackPath);
Image img = Image.FromFile(imgPath);
Bitmap bmp = MergeImgHelp.CombinImage(imgBack, img);
MemoryStream ms = new MemoryStream();
bmp.Save(saveFile, ImageFormat.Jpeg);
Console.WriteLine("文件保存成功!");
Console.ReadKey();
}
public class MergeImgHelp
{
/// <summary>
/// 合并图片
/// </summary>
/// <param name="imgBack"></param>
/// <param name="img"></param>
/// <param name="xDeviation"></param>
/// <param name="yDeviation"></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);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height / 2 - img.Height / 2+yDeviation, img.Width, img.Height);
GC.Collect();
return bmp;
}
}