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; } } }