C# GDI+ 常用API

封装一个方法,用来设置绘制参数(抗锯齿,平滑等)

 private static void SetQuality(Graphics g)
 {
       g.SmoothingMode = SmoothingMode.AntiAlias;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 }

1.画直线

 private void Panel1_Paint(object sender, PaintEventArgs e)
 {
        //创建图形(画板)
        Graphics g = e.Graphics;

        //设置绘制参数(抗锯齿,平滑等)
        SetQuality(g);

        //开始绘制
        Pen pen = new Pen(Color.Red, 20F);
        Point[] points = new Point[]
        {
                new Point(50,50),
                new Point(150,50),
                new Point(60,160),
        };
        g.DrawLines(pen, points);
 }

2. 画弧

 private void Panel2_Paint(object sender, PaintEventArgs e)
 {
        Graphics g = e.Graphics;
        SetQuality(g);
        
        //画笔
        Pen p1 = new Pen(Color.Blue, 10F);
        Pen p2 = new Pen(Color.Red, 10F);

        Rectangle rect = new Rectangle(20, 20, 100, 100);

        //画椭圆       
        /* g.DrawArc(p1,rect,0,90);
        g.DrawArc(p2,rect, 90, 90);
        g.DrawArc(p1,rect, 180, 90);
        g.DrawArc(p2,rect, 270, 90);*/


        //画扇形
        g.DrawArc(p1, rect, 180, 180);
        Point[] points = new Point[]
        {
                new Point(120,65),
                new Point(70,120),
                new Point(20,65),
        };
        g.DrawLines(p1, points);
 }

3. 画图片

 private void Panel3_Paint(object sender, PaintEventArgs e)
 {
        Graphics g = e.Graphics;
        SetQuality(g);

        string path = Path.Combine(Environment.CurrentDirectory, "../../image/2.png");
        Image img = Image.FromFile(path);
        Rectangle destRect = new Rectangle(0, 0, this.panel3.Width, this.panel3.Height);
        Rectangle srcRect = new Rectangle(0, 0, img.Width, img.Height);

        //参数1:绘制的原始图片
        //参数2:目标矩形大小,如果图片过大,会自动缩放到此矩形中
        //参数3:原始图片所占矩的形大小
        //参数4:参数3所用的单位
        g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
 }

4. 画矩形

private void Panel4_Paint(object sender, PaintEventArgs e)
{
        Graphics g = e.Graphics;
        SetQuality(g);
        Pen pen = new Pen(Color.Red, 20F);

        Rectangle rect = new Rectangle(50, 20, 100, 50);
        g.DrawRectangle(pen, rect);

        //矩形填充颜色渐变
        Rectangle rect1 = new Rectangle(50, 100, 100, 100);

        Point p1 = new Point(50, 100);
        Point p2 = new Point(150, 200);
        Brush brush = new LinearGradientBrush(p1, p2, Color.Red, Color.Blue);

        //使用方向角度
        //Brush brush1 = new LinearGradientBrush(rect, Color.Red, Color.Blue, 135F);

        g.FillRectangle(brush, rect1);
}

5.画饼状图

private void Panel5_Paint(object sender, PaintEventArgs e)
{
        Graphics g = e.Graphics;
        SetQuality(g);

        Brush b1 = new SolidBrush(Color.Red);
        Brush b2 = new SolidBrush(Color.Blue);
        Brush b3 = new SolidBrush(Color.Gray);
        Brush b4 = new SolidBrush(Color.Green);

        Rectangle rect = new Rectangle(20, 20, 200, 150);
        g.FillPie(b1, rect, 0, 90);
        g.FillPie(b2, rect, 90, 90);
        g.FillPie(b3, rect, 180, 90);
        g.FillPie(b4, rect, 270, 90);

}

6. 写字

private void Panel6_Paint(object sender, PaintEventArgs e)
{
        Graphics g = e.Graphics;
        SetQuality(g);

        Pen p = new Pen(new SolidBrush(Color.Blue), 2F);

        //画字体
        g.DrawString("中国", new Font("宋体", 18F), new SolidBrush(Color.Red), 50, 50);

        //画虚线 点状
        p.DashStyle = DashStyle.Dot;
        g.DrawLine(p, 50, 80, 100, 80);

        string path = Path.Combine(Environment.CurrentDirectory, "../../image/2.ico");
        g.DrawIcon(new Icon(path), new Rectangle(100, 100, 64, 64));
}

7.画路径

 private void Panel7_Paint(object sender, PaintEventArgs e)
 {
        Graphics g = e.Graphics;
        SetQuality(g);

        GraphicsPath path = new GraphicsPath();
        path.StartFigure();//路径开始
        path.AddLine(30, 30, 100, 30);
        path.AddLine(100, 30, 100, 130);
        path.CloseAllFigures();//路径结束
        //g.DrawPath(new Pen(Color.Red,6),path);
        g.FillPath(Brushes.Red, path);

        GraphicsPath path2 = new GraphicsPath();
        path2.StartFigure();//路径开始
        path2.AddRectangle(new Rectangle(110, 30, 100, 100));
        StringFormat format = new StringFormat();
        path2.AddString("中国", new FontFamily("方正舒体"), 2, 25F, new Point(130, 50), format);
        path.CloseAllFigures();//路径结束
        g.FillPath(Brushes.Red, path2);
 }

8.画曲线

 private void Panel8_Paint(object sender, PaintEventArgs e)
 {
        Graphics g = e.Graphics;
        SetQuality(g);

        Pen pen = new Pen(Color.Red, 10F);
        Point[] points = new Point[]
        {
                new Point(0,220),
                new Point(60,50),
                new Point(120,150),
                new Point(180,50),
                new Point(230,220),
        };
        g.DrawCurve(pen, points);
 }

最终效果图

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值