• C#.NET把文字动态生成到图片上。


    背景:我需要做一个动态名片。背景图固定,把名字或其他区域的字,动态换

    然后生成一张新图,

    这是后台.CS类

    有哪里不懂留言一起研究

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Net;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    
    public partial class aTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //画布的宽度和高度
                int initialWidth = 399, initialHeight = 283;
                Bitmap theBitmap = new Bitmap(initialWidth, initialHeight);
                Graphics theGraphics = Graphics.FromImage(theBitmap);
                //呈现质量
                theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                //背景色
                theGraphics.Clear(Color.FromArgb(255, 255, 255));
                //灰色边框
                theGraphics.DrawRectangle(new Pen(Color.FromArgb(227, 227, 227), 1), 1, 1, initialWidth - 2,
                    initialHeight - 2);
    
                int autoHeight = 0; //累计高度(动态的内容高度位置)
                int sjh = 0; //时间轴的高度
                float fwigth = 0;
                int fontHeight = 12; //文字行间距
                string FontType = "微软雅黑";
                string IconType = "iconfont";
                Font theFont = new Font(FontType, 12.0f, System.Drawing.FontStyle.Bold);
    
                //准备工作。定义画刷颜色
                Color col = Color.FromArgb(51, 51, 51);
                Brush newBrush = new SolidBrush(col);
                Brush bush = new SolidBrush(Color.FromArgb(204, 204, 204)); //填充的颜色
    
                Bitmap bmp = Get_img(@"http://localhost:2446/aadmin/images/zhengshu.jpg");//背景图片地址~/aadmin/images/zhengshu.jpg
                //画布的开始坐标和结束坐标
                theGraphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, 399, 283),
                new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                                System.Drawing.GraphicsUnit.Pixel);
                //文字居画布的位置坐标
                theGraphics.DrawString("动态文字", theFont, newBrush, 90, 50);
    
    //浏览器自动下载开始
                Response.ContentType = "application/x-msdownload";
                //Bitmap theBitmap = new Bitmap(initialWidth, initialHeight);//此为所画的图片
                string filename = "attachment; filename=001.jpg";
                Response.AddHeader("Content-Disposition", filename);
    //浏览器自动下载结束
    
    
                //Bitmap转byte[]
                MemoryStream ms = new MemoryStream();
                theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] bytes = ms.GetBuffer();
                ms.Close();
    
                Response.BinaryWrite(bytes);
    
                
                string imurl = "/aadmin/userImages/" + DateTime.Now.ToFileTime().ToString() + ".Png";
                MemoryStream stream = new MemoryStream(bytes);
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                //要存到相对路径要用HttpContext.Current.Server.MapPath。否则只能用绝对路径,E:/这种格式
                img.Save(HttpContext.Current.Server.MapPath(imurl), System.Drawing.Imaging.ImageFormat.Png);
    
    
    
    
    
    
    
    
            }
    
    
        }
    
        public Bitmap Get_img(string imgurl)
        {
            Bitmap img = null;
            HttpWebRequest req;
            HttpWebResponse res = null;
            try
            {
                System.Uri httpUrl = new System.Uri(imgurl);
                req = (HttpWebRequest)(WebRequest.Create(httpUrl));
                req.Timeout = 180000; //设置超时值10秒
                req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
                req.Accept = "text/html, application/xhtml+xml, */*";
                req.Method = "GET";
                res = (HttpWebResponse)(req.GetResponse());
                img = new Bitmap(res.GetResponseStream());//获取图片流    
                //string imurl = "~/aadmin/userImages/" + DateTime.Now.ToFileTime().ToString() + ".png";
                //img.Save(imurl );//随机名
            }
            catch (Exception e) {
            }
            return img;
        }
        
    
    
    
    }
  • 相关阅读:
    首页下拉广告代码
    索引 CREATE INDEX
    苏三起解 故事
    日期正则表达式
    除去非空字符串函数
    数据库什么是字段应该设置索引
    警务通
    动态生成缩略图
    超级实用且不花哨的js代码大全
    无线警务移动办公系统解决方案
  • 原文地址:https://www.cnblogs.com/wybshyy/p/13783685.html
Copyright © 2020-2023  润新知