• C#合并两个图片,并保存到本地


    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;
            }
        }
  • 相关阅读:
    day⑥:logging模块
    day⑥:shelve模块
    day⑥:xml模块
    day⑤:冒泡排序
    day⑤:模块
    day⑤:re深入
    day④:递归
    day④:迭代器
    day④:装饰器
    day③:函数式编程
  • 原文地址:https://www.cnblogs.com/taidou/p/7211440.html
Copyright © 2020-2023  润新知