C#Graphics图片上添加文字并按点旋转

Graphics图片上添加文字并按点旋转

  1. 设置文本布局信息StringFormat 类,其中属性StringFormat.Alignments设置文字水平对齐,StringFormat.LineAlignment设置垂直对其,StringAlignment中属性Center(字符串中心与位置中心对齐)、Far(字符串远端即文字结尾与位置中心对齐)、Near(字符串近端即文字开头与与位置中心对齐)
  2. 设置画布旋转 Graphics.Transform,旋转矩阵,Matrix.RotateAt (角度,旋转中心点位置,旋转顺序默认顺时针)
  3. 写字Graphics.DrawString(字符串,字体,笔刷,位置,对齐)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A003
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            img = Image.FromFile("C:\\Users\\ibit\\Downloads\\a.jpg");
        }
        Graphics g;
        Image img;

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics gBmp = Graphics.FromImage(img);

            //添加文字
            String str1 = "1ABCDE";
            String str2 = "2ABCDEABCDE";
            String str3 = "3ABCDEABCDEABCDE";
            Font font = new Font("微软雅黑", 16);
            SolidBrush sbrush1 = new SolidBrush(Color.Black);
            SolidBrush sbrush2 = new SolidBrush(Color.Red);

            //笔刷渐变效果
            //LinearGradientBrush sbrush3 = new LinearGradientBrush(new PointF(100, 100), new PointF(250,250), Color.Red ,Color.Black); 
            SolidBrush sbrush3 = new SolidBrush(Color.Blue);

            //绘制饼图
            //SolidBrush s1 = new SolidBrush(Color.Pink);
            //gBmp.FillPie(s1, 150, 150, 200, 150, 90, 200);
            //SolidBrush s2 = new SolidBrush(Color.Purple);
            //gBmp.FillPie(s2, 150, 150, 200, 150, 290, 70);
            //SolidBrush s3 = new SolidBrush(Color.Green);
            //gBmp.FillPie(s3, 150, 150, 200, 150, 0, 60);
            //SolidBrush s4 = new SolidBrush(Color.Brown);
            //gBmp.FillPie(s4, 150, 150, 200, 150, 60, 30);

            //文字位置中心点坐标
            Pen p1 = new Pen(Color.Green,3);
            gBmp.DrawLine(p1,0,100,200,100);
            gBmp.DrawLine(p1, 100, 0, 100, 200);
           // Rectangle rect = new Rectangle(200,200,100,70);
           // gBmp.DrawEllipse(p1,rect);


            StringFormat format1 = new StringFormat();
            //指定字符串的水平对齐方式
            format1.Alignment = StringAlignment.Far;
            //表示字符串的垂直对齐方式
            format1.LineAlignment = StringAlignment.Center;
            StringFormat format2 = new StringFormat();
            //指定字符串的水平对齐方式
            format2.Alignment = StringAlignment.Center;
            //表示字符串的垂直对齐方式
            format2.LineAlignment = StringAlignment.Center;
            StringFormat format3 = new StringFormat();
            //指定字符串的水平对齐方式
            format3.Alignment = StringAlignment.Near;
            //表示字符串的垂直对齐方式
            format3.LineAlignment = StringAlignment.Center;

            //旋转角度和平移
            Matrix mtxRotate = gBmp.Transform;
            mtxRotate.RotateAt(30, new PointF(100, 100));
            gBmp.Transform = mtxRotate;

            gBmp.DrawString(str1, font, sbrush1, new PointF(100, 100), format1);
            gBmp.DrawString(str2, font, sbrush2, new PointF(100, 100), format2);
           gBmp.DrawString(str3, font, sbrush3, new PointF(100, 100), format3);


            g = this.CreateGraphics();
            g.DrawImage(img, 0, 0);

            gBmp.Dispose();
        }
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_42731241/article/details/81113590