• 合成图片+合成文字+图片


    private void Form1_Load(object sender, EventArgs e)
            {
                Image backgroundImage = Image.FromFile(Environment.CurrentDirectory + @"Template.JPG");
    
                var iamgeCompanyName = CreateText("北京红某某科技有限公司", false, 102);
                Bitmap bmp = CombinImage(backgroundImage, iamgeCompanyName, 0, 1280);
    
                var imageBrandName = CreateText("某某世纪", false, 95);
                bmp = CombinImage(bmp, imageBrandName, 0, 1545);
    
                var imageCycle = CreateText("二0一九年十四月", false, 90);
                bmp = CombinImage(bmp, imageCycle, 0, 2025);
    
                var imageSerialNum = CreateText("编号:8885559998", false, 45);
                bmp = CombinImage(bmp, imageSerialNum, 608, 578);
    
                Image cctvLogo = Image.FromFile(Environment.CurrentDirectory + @"logol1.png");
                bmp = CombinImage(bmp, cctvLogo, 0, 2435);
    
    
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, ImageFormat.Png);
    
                string filePath = @"C:迅雷下载" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
    
                FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write);
                file.Write(ms.ToArray(), 0, ms.ToArray().Length);
                file.Close();
                ms.Close();
    
                System.Diagnostics.Process.Start(filePath);
            }
    
    
    
            public void SaveImage(MemoryStream ms, string filePath)
            {
                FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write);
                file.Write(ms.ToArray(), 0, ms.ToArray().Length);
                file.Close();
                ms.Close();
            }
    
            /// <summary>
            /// 生成文字图片
            /// </summary>
            /// <param name="text"></param>
            /// <param name="isBold"></param>
            /// <param name="fontSize"></param>
            public Image CreateText(string text, bool isBold, int fontSize)
            {
                int wid = 400;
                int high = 200;
    
                PrivateFontCollection fontCollection = new PrivateFontCollection();
                fontCollection.AddFontFile(Environment.CurrentDirectory + @"AdobeHeitiStd-Regular.otf");
    
                var r = fontCollection.Families[0].IsStyleAvailable(FontStyle.Regular);
                var b = fontCollection.Families[0].IsStyleAvailable(FontStyle.Bold);
                //定义成新的字体对象
                FontFamily myFontFamily = new FontFamily(fontCollection.Families[0].Name, fontCollection);
                Font font = new Font(myFontFamily, fontSize);
                if (isBold)
                {
                    font = new Font(myFontFamily, fontSize, FontStyle.Bold);
                }
                //绘笔颜色
                SolidBrush brush = new SolidBrush(Color.Black);
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                Bitmap image = new Bitmap(wid, high);
                Graphics g = Graphics.FromImage(image);
    
                SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);//得到文本的宽高
                int width = (int)(sizef.Width + 1);
                int height = (int)(sizef.Height + 1);
                image.Dispose();
                image = new Bitmap(width, height);
                g = Graphics.FromImage(image);
    
    
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    
                g.Clear(Color.Transparent);//透明
    
                RectangleF rect = new RectangleF(0, 0, width, height);
                //绘制图片
                g.DrawString(text, font, brush, rect);
                //释放对象
                g.Dispose();
                return image;
            }
    
            /// <summary>  
            /// 合并图片  
            /// </summary>  
            /// <param name="imgBack"></param>  
            /// <param name="img"></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(imgBack, 0, 0, 相框宽, 相框高);     
                g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, yDeviation, img.Width, img.Height);
                GC.Collect();
                return bmp;
            }
    
            /// <summary>  
            /// Resize图片  
            /// </summary>  
            /// <param name="bmp">原始Bitmap</param>  
            /// <param name="newW">新的宽度</param>  
            /// <param name="newH">新的高度</param>  
            /// <param name="mode">保留着,暂时未用</param>  
            /// <returns>处理以后的图片</returns>  
            public static Image ResizeImage(Image bmp, int newW, int newH, int mode)
            {
                try
                {
                    Image b = new Bitmap(newW, newH);
                    Graphics g = Graphics.FromImage(b);
    
                    // 插值算法的质量    
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height),
                                GraphicsUnit.Pixel);
                    g.Dispose();
                    return b;
                }
                catch
                {
                    return null;
                }
            }
        }
  • 相关阅读:
    BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 总结
    OpenStreetMap初探(一)——了解OpenStreetMap
    企业服务总线架构介绍
    【Stackoverflow好问题】java在,如何推断阵列Array是否包括指定的值
    C和指针 (pointers on C)——第一章:高速启动
    类别sort使用排序
    [Oracle]
    4点,从今天谈用户体验设计经验京东亚马逊购物
    从[java.lang.OutOfMemoryError: Java heap space]恢复
    C++学习笔记32 断言函数
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/12048626.html
Copyright © 2020-2023  润新知