1. 程式人生 > >C# 生成海報,文本區域指定和換行,圖片合成

C# 生成海報,文本區域指定和換行,圖片合成

response money .com file rec map tin textarea args

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                string path = Server.MapPath(@"/Content/images/bg/index_01.jpg");
                AddToImg(path, "圖片測試pictureBox在圖片上繪制文本_百度知道pictureBox在圖片上繪制文本_百度知道", Guid.NewGuid().ToString("N"));
            }
        }

using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

繪制圖片和文字

        /// <summary>
        /// 指定圖片添加指定文字
        /// </summary>
        /// <param name="fileName">指定文件路徑</param>
        /// <param name="text">添加的文字</param>
        /// <param name="picname">
生成文件名</param> private void AddToImg(string file, string text, string picname) { //判斷指定圖片是否存在 //if (!File.Exists(MapPath(fileName))) //{ // throw new FileNotFoundException("The file don‘t exist!"); //} if
(text == string.Empty) { return; } System.Drawing.Image image = System.Drawing.Image.FromFile(file); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); Graphics g = Graphics.FromImage(bitmap); float fontSize = 40.0f; //字體大小 float textWidth = text.Length * fontSize; //文本的長度 //下面定義一個矩形區域,以後在這個矩形裏畫上白底黑字 float rectX = 120; float rectY = 200; float rectWidth = 200; // text.Length * (fontSize + 40); float rectHeight = fontSize + 40; //聲明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, 270, 270); Font font = new Font("華文隸書", fontSize, FontStyle.Bold); //定義字體 Font font2 = new Font("楷體", fontSize, FontStyle.Bold); //定義字體 //font.Bold = true; Brush whiteBrush = new SolidBrush(Color.DodgerBlue); //白筆刷,畫文字用 //Brush blackBrush = new SolidBrush(Color.Black); //黑筆刷,畫背景用 //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight); g.DrawString(text, font, whiteBrush, textArea, StringFormat.GenericDefault); g.DrawString(text, font, whiteBrush, new RectangleF(rectX, image.Height/2, 270, 270)); g.DrawString("168元", font2, new SolidBrush(Color.Firebrick), new RectangleF(rectX, image.Height - 150, rectWidth, rectHeight)); ////------------------- 繪制高質量 ------------------------------------------- //設置 System.Drawing.Graphics對象的SmoothingMode屬性為HighQuality g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面這個也設成高質量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面這個設成High g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //畫專屬推廣二維碼 System.Drawing.Image qrCodeImage = System.Drawing.Image.FromFile(Server.MapPath(@"/Content/images/money-cards.png")); g.DrawImage(qrCodeImage, new Rectangle(image.Width - qrCodeImage.Width - 200, image.Height - qrCodeImage.Height - 200, qrCodeImage.Width, qrCodeImage.Height), 0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel); //畫微信昵稱 g.DrawString("小馬快跑", font, new SolidBrush(Color.Red), new Rectangle(image.Width - qrCodeImage.Width - 200, image.Height - qrCodeImage.Height - 300, qrCodeImage.Width + 100, 50)); MemoryStream ms = new MemoryStream(); //輸出方法一:將文件生成並保存到C盤 string path = "D:\\test\\" + picname + ".png"; bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); //輸出方法二,顯示在網頁中,保存為Jpg類型 //bitmap.Save(ms, ImageFormat.Jpeg); //Response.Clear(); //Response.ContentType = "image/jpeg"; //Response.BinaryWrite(ms.ToArray()); g.Dispose(); bitmap.Dispose(); image.Dispose(); }

C# 生成海報,文本區域指定和換行,圖片合成