C#中GDI+绘图应用(柱形图、折线图和饼形图)

2023-12-01 22:44

本文主要是介绍C#中GDI+绘图应用(柱形图、折线图和饼形图),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、柱形图

1.示例源码

2.生成效果

二、折线图

1.示例源码

2.生成效果

三、饼形图 

1.示例源码

2.生成效果


        GDI+绘制的一些常用的图形,其中包括柱形图、折线图和饼形图。

一、柱形图

        柱形图也称为条形图,是程序开发中比较常用的一种图表技术。柱形图是通过Graphics 类中的FillRectangle()方法实现的。
public void FillRectangle (Brush brush,int x,int y,int width,int height)
参    数说    明
brush确定填充特性的Brush
X要填充矩形左上角的x坐标
y要填充矩形左上角的y坐标
width要填充矩形的宽度
height要填充矩形的高度

1.示例源码

         ,NET 8.0的项目中需要使用NuGet程序包:system.data.sqlclient.4.8.5.nupkg。否则不能使用SqlClient的方法。

//Form1.cs
//投票窗口
using System.Data.SqlClient;namespace _12
{public partial class Form1 : Form{SqlConnection? conn;public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){ClientSize = new Size(292, 192);        //设置Form1工作区域StartPosition = FormStartPosition.CenterScreen;radioButton1.Checked = false;radioButton1.Text = "支付宝";radioButton2.Text = "微信支付";radioButton3.Text = "京东白条";radioButton4.Text = "小度钱包";button1.Text = "投一票吧";button2.Text = "投票结果";groupBox1.Text = "投票窗口";radioButton1.Location = new Point(26, 37);radioButton2.Location = new Point(26, 68);radioButton3.Location = new Point(26, 101);radioButton4.Location = new Point(26, 132);radioButton1.Size = new Size(60, 16);radioButton2.Size = new Size(60, 16);radioButton3.Size = new Size(60, 16);radioButton4.Size = new Size(60, 16);button1.Location = new Point(195, 94);button2.Location = new Point(195, 132);button1.Size = new Size(75, 23);button2.Size = new Size(75, 23);}private void Button1_Click(object sender, EventArgs e){try{conn = new SqlConnection(@"server=DESKTOP-S11C97H\SQLEXPRESS;database=db_CSharp;integrated security = True");string sqlstr = "";if (radioButton1.Checked){sqlstr = "update tb_vote set 票数=票数+1 where 选项='" + radioButton1.Text + "'";}if (radioButton2.Checked){sqlstr = "update tb_vote set 票数=票数+1 where 选项='" + radioButton2.Text + "'";}if (radioButton3.Checked){sqlstr = "update tb_vote set 票数=票数+1 where 选项='" + radioButton3.Text + "'";}if (radioButton4.Checked){sqlstr = "update tb_vote set 票数=票数+1 where 选项='" + radioButton4.Text + "'";}conn.Open();SqlCommand cmd = new(sqlstr, conn);int i = cmd.ExecuteNonQuery();conn.Close();if (i > 0){MessageBox.Show("投票成功");}}catch (Exception ex){MessageBox.Show(ex.Message);}}private void Button2_Click(object sender, EventArgs e){Form2 frm2 = new();frm2.Show();}}
}

       

//Form2.cs
//根据选票数据库,绘制直方图
using System.Data;
using System.Data.SqlClient;namespace _12
{public partial class Form2 : Form{public Form2(){InitializeComponent();}private int Sum;SqlConnection? conn;private void CreateImage(){conn = new SqlConnection(@"server=DESKTOP-S11C97H\SQLEXPRESS;database=db_CSharp;integrated security = True");conn.Open();SqlCommand cmd = new("select sum(票数) from tb_vote", conn);Sum = (int)cmd.ExecuteScalar();SqlDataAdapter sda = new("select * from tb_vote", conn);DataSet ds = new();sda.Fill(ds);int TP1 = Convert.ToInt32(ds.Tables[0].Rows[0][2].ToString());//第一个选项的票数int TP2 = Convert.ToInt32(ds.Tables[0].Rows[1][2].ToString());//第二个选项的票数int TP3 = Convert.ToInt32(ds.Tables[0].Rows[2][2].ToString());//第三个选项的票数int TP4 = Convert.ToInt32(ds.Tables[0].Rows[3][2].ToString());//第四个选项的票数float tp1 = Convert.ToSingle(Convert.ToSingle(TP1) * 100 / Convert.ToSingle(Sum));float tp2 = Convert.ToSingle(Convert.ToSingle(TP2) * 100 / Convert.ToSingle(Sum));float tp3 = Convert.ToSingle(Convert.ToSingle(TP3) * 100 / Convert.ToSingle(Sum));float tp4 = Convert.ToSingle(Convert.ToSingle(TP4) * 100 / Convert.ToSingle(Sum));int width = 300, height = 300;Bitmap bitmap = new(width, height);Graphics g = Graphics.FromImage(bitmap);try{g.Clear(Color.White);Brush brush1 = new SolidBrush(Color.LightGray);Brush brush2 = new SolidBrush(Color.Black);Brush brush3 = new SolidBrush(Color.Red);Brush brush4 = new SolidBrush(Color.Green);Brush brush5 = new SolidBrush(Color.Orange);Brush brush6 = new SolidBrush(Color.DarkBlue);Font font1 = new("Courier New", 16, FontStyle.Bold);Font font2 = new("Courier New", 8);g.FillRectangle(brush1, 0, 0, width, height);    //绘制背景浅灰色g.DrawString("投票结果", font1, brush2, new Point(90, 20));Point p1 = new(70, 50);Point p2 = new(230, 50);g.DrawLine(new Pen(Color.Black), p1, p2);//绘制文字g.DrawString("支付宝:", font2, brush2, new Point(45, 80));g.DrawString("微信支付:", font2, brush2, new Point(32, 110));g.DrawString("京东白条:", font2, brush2, new Point(32, 140));g.DrawString("小度钱包:", font2, brush2, new Point(32, 170));//绘制柱形图g.FillRectangle(brush3, 95, 80, tp1, 17);g.FillRectangle(brush4, 95, 110, tp2, 17);g.FillRectangle(brush5, 95, 140, tp3, 17);g.FillRectangle(brush6, 95, 170, tp4, 17);//绘制所有选项的票数显示g.DrawRectangle(new Pen(Color.Green), 10, 210, 280, 80);  //绘制范围框g.DrawString("支付宝:" + TP1.ToString() + "票", font2, brush2, new Point(15, 220));g.DrawString("微信支付:" + TP2.ToString() + "票", font2, brush2, new Point(150, 220));g.DrawString("京东白条:" + TP3.ToString() + "票", font2, brush2, new Point(15, 260));g.DrawString("小度钱包:" + TP4.ToString() + "票", font2, brush2, new Point(150, 260));pictureBox1.Image = bitmap;}catch (Exception ex){MessageBox.Show(ex.Message);}}private void Form2_Paint(object sender, PaintEventArgs e){CreateImage();}/// <summary>/// 设置窗体工作区大小,设置绘图板工作区,默认边距=8mm/// </summary>private void Form2_Load(object sender, EventArgs e){ClientSize = new Size(324, 324);               //Form2工作区域大小StartPosition = FormStartPosition.CenterScreen;pictureBox1.Size = new Size(300, 300);      //pictureBox1尺寸,边距8mm}}
}

2.生成效果

 

二、折线图

        折线图可以很直观地反映出相关数据的变化趋势,折线图主要是通过绘制点和折线实现的。绘制点是通过Graphics类中的FillEllipse()方法实现的。绘制折线是通过Graphics类中的DrawLine()方法实现的。

        用DrawString()方法绘制文本时,文本的长度必须在所绘制的矩形区域内,如果超出区域,必须用format参数指定截断方式,否则将在最近的单词处截断。

1.示例源码

//折线图、趋势图
namespace _11
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Paint(object sender, PaintEventArgs e){string[] month = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];float[] d = [20.5F, 60, 10.8F, 15.6F, 30, 70.9F, 50.3F, 30.7F, 70, 50.4F, 30.8F, 20];//画图初始化Bitmap bMap = new(500, 500);Graphics gph = Graphics.FromImage(bMap);gph.Clear(Color.White);PointF cPt = new(40, 420);                                                                                                                       //坐标原点PointF[] xPt = [new PointF(cPt.Y + 15, cPt.Y), new PointF(cPt.Y, cPt.Y - 4), new PointF(cPt.Y, cPt.Y + 4)];     //X轴箭头点PointF[] yPt = [new PointF(cPt.X, cPt.X - 15), new PointF(cPt.X - 4, cPt.X), new PointF(cPt.X + 4, cPt.X)];     //Y轴箭头点gph.DrawString("产品月产量趋势图", new Font("宋体", 14), Brushes.Blue, new PointF(cPt.X + 60, cPt.X));    //图标题//画X轴gph.DrawLine(Pens.Green, cPt.X, cPt.Y, cPt.Y, cPt.Y);     //X轴的长度=420= cPt.Ygph.DrawPolygon(Pens.Black, xPt);                         //X轴箭头gph.FillPolygon(new SolidBrush(Color.Pink), xPt);gph.DrawString("月份", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y + 10, cPt.Y + 10));//画Y轴gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X);      //Y轴的长度=420-40=380gph.DrawPolygon(Pens.Black, yPt);                          //Y轴箭头gph.FillPolygon(new SolidBrush(Color.Black), yPt);gph.DrawString("单位(万)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));for (int i = 1; i <= 12; i++){//画Y轴刻度if (i < 11){gph.DrawString((i * 10).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6)); //间隔30单位打一个标记,工程值和坐标值间有个3倍的关系gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30);                                                               //间隔30单位刻一条标尺}//画X轴项目gph.DrawString(month[i - 1].AsSpan(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 5));    //间隔30单位打第一个字gph.DrawString(month[i - 1].AsSpan(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 20));   //间隔30单位打第二个字if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].AsSpan(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 35));  //间隔30单位打第三个字//画数据点gph.DrawEllipse(Pens.Black, cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);                                                                  //以下Y坐标都要乘以3倍gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);//画数据值gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30, cPt.Y - d[i - 1] * 3));//画折线if (i > 1) gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - d[i - 2] * 3, cPt.X + i * 30, cPt.Y - d[i - 1] * 3);}pictureBox1.Image = bMap;}   }
}

2.生成效果

 

三、饼形图 

        饼形图可以很直观地查看不同数据所占的比例情况,通过Graphics类中的FillPie()方法,可以方便地绘制出饼形图。

public void FillPie (Brush brush,int x,int y,int width,int height,int startAngle,int sweepAngle)

参    数

说    明

  brush

确定填充特性的Brush

   X

边框左上角的x坐标,该边框定义扇形区所属的椭圆

  y

边框左上角的y坐标,该边框定义扇形区所属的椭圆

  width

边框的宽度,该边框定义扇形区所属的椭圆

  heigh

边框的高度,该边框定义扇形区所属的椭圆

  startAngle

从x轴沿顺时针方向旋转到扇形区第一个边所测得的角度(以度为单位)

  sweepAngle

从startAngle参数沿顺时针方向旋转到扇形区第二个边所测得的角度(以度为单位)

1.示例源码

2.生成效果

这篇关于C#中GDI+绘图应用(柱形图、折线图和饼形图)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/442971

相关文章

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F