C#—绘制图形

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class FirstForm : Form
    {
        public FirstForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            实例7_8绘制矩形  form8=new 实例7_8绘制矩形 ();
            form8.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            实例7_9绘制平滑曲线 form9 = new 实例7_9绘制平滑曲线();
            form9.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            实例7_10绘制贝塞尔曲线 form10 = new 实例7_10绘制贝塞尔曲线();
            form10.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            实例7_11绘制多边形 form11 = new 实例7_11绘制多边形();
            form11.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            实例7_12绘制椭圆 form12 = new 实例7_12绘制椭圆();
            form12.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            实例7_13绘制文字 form13 = new 实例7_13绘制文字();
            form13.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            实例7_14显示图像 form14 = new 实例7_14显示图像();
            form14.Show();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            实例7_15刷新图像 form15 = new 实例7_15刷新图像();
            form15.Show();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            绘制柱形图 formZX = new 绘制柱形图();
            formZX.Show();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            验证码 yzm = new 验证码();
            yzm.Show();
        }

        private void FirstForm_Load(object sender, EventArgs e)
        {

        } 
    }
}

/*
 * 使用DrawLine和DrawLines绘制直线。
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Black, 2.5f);
            g.DrawLine(p, new Point(50, 10), new Point(250, 10));
            Point[] points = { new Point(50,20),new Point(250,20),
                                new Point(250,70), new Point(50,20)};
            g.DrawLines(p, points  );
            g.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果:

/*
 * 使用DrawRectangle、FillRectangle方法绘制、填充矩形轮廓。
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_8绘制矩形
{
    public partial class 实例7_8绘制矩形 : Form
    {
        public 实例7_8绘制矩形()
        {
            InitializeComponent();
        }

        private void 实例7_8绘制矩形_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Black, 2.0f);
            Rectangle rct1 = new Rectangle(new Point(10, 10), new Size(80, 40));
            g.DrawRectangle(p, rct1);         //绘制矩形轮廓
            //充填矩形区域
            Rectangle rct2 = new Rectangle(new Point(100, 80), new Size(200, 50));
            SolidBrush brush = new SolidBrush(Color.Blue);
            g.FillRectangle(brush, rct2);
            g.Dispose();

        }

        private void 实例7_8绘制矩形_Load(object sender, EventArgs e)
        {
            this.Text = "实例7-8绘制矩形";
        }
    }
}
运行结果:

/*
 * 使用DrawCurve方法绘制平滑曲线。
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class 实例7_9绘制平滑曲线 : Form
    {
        public 实例7_9绘制平滑曲线()
        {
            InitializeComponent();
        }

        private void 实例7_9绘制平滑曲线_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //绘制平滑曲线
            Pen p = new Pen(Color.Black, 2.0f);
            Point[] points = {new Point(50,100), new Point(100, 20),
                             new Point(200, 100), new Point(250, 20), new Point(300, 75) };
            g.DrawCurve(p, points, 0.8f);
            g.Dispose();
        }

        private void 实例7_9绘制平滑曲线_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果;

//绘制贝塞尔曲线
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class 实例7_10绘制贝塞尔曲线 : Form
    {
        public 实例7_10绘制贝塞尔曲线()
        {
            InitializeComponent();
        }

        private void 实例7_10绘制贝塞尔曲线_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //绘制贝塞尔曲线
            Pen p = new Pen(Color.Black, 2.0f);
            Point p1 = new Point(50, 100);
            Point p2 = new Point(100, 20);
            Point p3 = new Point(200, 200);
            Point p4 = new Point(250, 80);
            g.DrawBezier(p, p1, p2, p3, p4);
            g.Dispose();

        }

        private void 实例7_10绘制贝塞尔曲线_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果:

//绘制、填充多边形
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class 实例7_11绘制多边形 : Form
    {
        public 实例7_11绘制多边形()
        {
            InitializeComponent();
        }

        private void 实例7_11绘制多边形_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //绘制多边形轮廓
            Pen p = new Pen(Color.Black, 2.0f);
            g.DrawPolygon(p, new Point[]{
                         new Point(20,20),
                         new Point(120,20),
                         new Point(70,70),
                         new Point(20,20)});
            //充填多边形区域
            SolidBrush brush = new SolidBrush(Color.Blue);
            Point[] points = {new Point(150,70), new Point(250,70),
                              new Point(200,20), new Point(150,70) };
            g.FillPolygon(brush, points );
            g.Dispose();
        }

        private void 实例7_11绘制多边形_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果:

//绘制椭圆
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class 实例7_12绘制椭圆 : Form
    {
        public 实例7_12绘制椭圆()
        {
            InitializeComponent();
        }

        private void 实例7_12绘制椭圆_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Black, 2.0f);
            g.DrawEllipse(p, new Rectangle(30, 30, 100, 60));     //绘制椭圆
            SolidBrush brush = new SolidBrush(Color.Blue);
            g.FillEllipse(brush, new Rectangle(180, 60, 100, 60));  //填充椭圆
            g.Dispose();

        }

        private void 实例7_12绘制椭圆_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果:

//绘制文字,该文字由图片填充。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D ;

namespace 实例7_7绘制图形
{
    public partial class 实例7_13绘制文字 : Form
    {
        public 实例7_13绘制文字()
        {
            InitializeComponent();
        }

        private void 实例7_13绘制文字_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Image image = Image.FromFile(Application.StartupPath + @"\p01.jpg");
            TextureBrush brush = new TextureBrush(image );
            //创建字体
            Font font = new Font("黑体", 60, FontStyle.Underline ^ FontStyle.Bold);
            g.DrawString("烟台大学", font, brush, new Point(10, 10));
            g.Dispose();
        }

        private void 实例7_13绘制文字_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果:

/*
 * 显示图像和保存图像。
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class 实例7_14显示图像 : Form
    {
        public 实例7_14显示图像()
        {
            InitializeComponent();
        }
        Graphics g;
        Bitmap bitmap ;
        private void button1_Click(object sender, EventArgs e)
        {
            g = pictureBox1.CreateGraphics();
            bitmap = new Bitmap(Application.StartupPath + @"\p01.jpg");
            g.DrawImage(bitmap, 0, 0);     //从PictureBox1控件的左上角开始绘制
            bitmap.Dispose();
            g.Dispose();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            bitmap  = new Bitmap(Application.StartupPath +  @"\p01.jpg ");
            Bitmap image = new Bitmap(pictureBox1.Width ,pictureBox1.Height );    //以图片框的高度和宽度保存图像
            g = Graphics.FromImage(image);
            g.DrawImage(bitmap, 0,0);
            image.Save(@"D:\p01.gif", System.Drawing.Imaging.ImageFormat.Gif);
            bitmap.Dispose();
            image.Dispose();
            g.Dispose();

        }

        private void 实例7_14显示图像_Load(object sender, EventArgs e)
        {

        }
    }
}
运行结果:

//刷新图像。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实例7_7绘制图形
{
    public partial class 实例7_15刷新图像 : Form
    {
        public 实例7_15刷新图像()
        {
            InitializeComponent();
        }
        Bitmap bitmap;
        Graphics g;
        private void button1_Click(object sender, EventArgs e)
        {
            bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.BackgroundImage = bitmap;
            g = Graphics.FromImage(bitmap);
            g.Clear(pictureBox1.BackColor);
            Pen  pen=new Pen (Color.Red ,3.0f );
            Point p1 = new Point(10, 10);
            Point p2 = new Point(150, 100);
            g.DrawLine(pen, p1, p2);
            g.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.BackgroundImage = bitmap;
            g = Graphics.FromImage(bitmap);
            g.Clear(pictureBox1.BackColor);           
            Pen pen = new Pen(Color.Red, 3.0f);
            Rectangle rec = new Rectangle(0, 0, 100, 100);
            g.DrawEllipse(pen, rec);
            g.Dispose();
        }

       

        private void 实例7_15刷新图像_Load(object sender, EventArgs e)
        {
          
        }
    }
}
运行结果:



  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中使用Excel绘制图形可以通过使用Excel的COM对象来实现。下面是一个简单的示例代码,演示了如何使用C#创建一个Excel应用程序,并在工作表中绘制一个简单的折线: ```csharp using Excel = Microsoft.Office.Interop.Excel; class Program { static void Main(string[] args) { // 创建Excel应用程序对象 Excel.Application excelApp = new Excel.Application(); // 添加一个工作簿 Excel.Workbook workbook = excelApp.Workbooks.Add(); // 获取第一个工作表 Excel.Worksheet worksheet = workbook.Sheets[1]; // 在工作表中添加一些数据 worksheet.Cells[1, 1] = "X"; worksheet.Cells[1, 2] = "Y"; worksheet.Cells[2, 1] = 1; worksheet.Cells[2, 2] = 10; worksheet.Cells[3, 1] = 2; worksheet.Cells[3, 2] = 20; worksheet.Cells[4, 1] = 3; worksheet.Cells[4, 2] = 15; // 添加表对象 Excel.ChartObjects chartObjects = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing); Excel.ChartObject chartObject = chartObjects.Add(100, 100, 300, 200); Excel.Chart chart = chartObject.Chart; // 设置表的数据源 Excel.Range chartRange = worksheet.Range["A1:B4"]; chart.SetSourceData(chartRange); // 设置表类型为折线 chart.ChartType = Excel.XlChartType.xlLine; // 保存Excel文件并关闭应用程序 workbook.SaveAs("ChartExample.xlsx"); excelApp.Quit(); } } ``` 上述代码使用了Excel的COM对象模型,因此要确保在项目中引用了Microsoft Excel的COM组件。注意,在运行代码之前,需要安装Excel应用程序。 这只是一个简单的示例,你可以根据自己的需求进行更复杂的图形绘制。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值