欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图片上绘制文字换行处理

程序员文章站 2022-05-27 22:51:02
...
 protected void Page_Load(object sender, EventArgs e)
        {
            CreatePicture("2010", "我是中国人们的儿子我深深的爱着我的祖国和人民", "我是中国人们的儿子我深深的爱着我的祖国和人民", @"C:\Users\wangyanfei\Pictures\1277702967234.jpg");
        }
        public void CreatePicture(string year, string chubpc, string kanm, string imgUrl)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(imgUrl);//获取图片路径
            Bitmap bmpImage = new Bitmap(image, 173, 228);
            Graphics graphic = Graphics.FromImage(bmpImage);
         
            Brush whiteBrush = new SolidBrush(Color.White);   //白笔刷,画文字用
            Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷,画文字用
          

            using (Font font1 = new Font("宋体", 12, FontStyle.Bold, GraphicsUnit.Point))
            {
                RectangleF rectF1 = new RectangleF(20, 80, 153, 50);
                graphic.DrawString(kanm, font1, blackBrush, rectF1);//建立一个矩形 让其在里面换行
                RectangleF rectF2 = new RectangleF(120, 200, 53, 50);
                graphic.DrawString(year, font1, blackBrush,  rectF2);
                RectangleF rectF3 = new RectangleF(120, 180, 53, 50);
                graphic.DrawString(chubpc, font1, blackBrush,  rectF3);
            }

          

            MemoryStream ms = new MemoryStream();
            bmpImage.Save("D:/1.jpg", ImageFormat.Jpeg);
            //保存为Jpg类型
            //return ms;
        }
 
string text1 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
    RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
    e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
    e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
}
 
string text2 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
    Rectangle rect2 = new Rectangle(30, 10, 100, 122);

    // Specify the text is wrapped.
    TextFormatFlags flags = TextFormatFlags.WordBreak;
    TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
    e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2));

}
 
相关标签: C C++ C#