C# Winform编程(8)GDI+绘图

news2024/9/20 14:55:54

GDI+绘图

  • 简介
  • System.Drawing命名空间
    • System.Drawing命名空间中的常用类:
    • System.Drawing命名空间中的常用结构:
  • Graphics 类
  • Pen类
  • Brush类
  • Font类
  • PictureBox图像控件
  • Bitmap类

简介

GDI(Graphics Device Interface)图像设备接口,属于绘图方面的API。System.Drawing命名空间提供了对GDI+基本图形功能的访问。

System.Drawing命名空间

System.Drawing命名空间中的常用类:

说明
Bitmap封装GDI+位图,此位图由图形图像及其属性的像素数据组成。
Brush画刷,定义用于填充图形形状(如:矩形、椭圆、饼图、多边形和封闭路径)的内部对象
Font字体,定义特定的文本格式,包括:字体、字号和字形属性。无法继承此类
Graphics封装一个GDI+绘图图面。无法继承此类
Pen画笔,定义用于绘制直线和曲线的对象。无法继承此类
Region指示由矩形和路径构成的图形形状的内部。无法继承此类

System.Drawing命名空间中的常用结构:

结构说明
Color表示RGB颜色
Point表示二维平面中定义的点、整数X和Y坐标的有序对
Rectangle存储一组整数,共4个,表示一个矩形的位置和大小
Size存储一个有序整数对,通常为矩形的宽度和高度

Graphics 类

Graphics类封装了一个GDI+绘图界面,提供将对象绘制到显示设备的方法。
Graphics类的常用方法:

名称说明
Dispose释放由Graphics使用的所有资源
DrawEllipse绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆
DrawArc绘制弧形
DrawLine绘制直线
DrawPolygon绘制由一组Point结构定义的多边形
DrawRectangle绘制由坐标对、宽度和高度指定的矩形
DrawPie绘制一个扇形,该形状由一个坐标对、宽度、高度以及两天射线所指定的椭圆定义
DrawCurse绘制曲线,由参数Point数组指定
FillEllips填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定
FillRegion填充Region的内部
ScaleTransform将指定的缩放操作应用与此Graphics
TanslateTransform平移坐标系原点

Pen类

Pen类可以设置画笔的颜色,线条的粗细和线条的样式(实现、虚线等)。画笔是绘图的工具,Graphics对象是绘画的场所。

Brush类

Brush类(画刷),用于填充图形。该类是一个抽象基类,不能直接实例化。
Brush类的派生类:

名称说明
ImageBrush图像绘制区域
LinearGradientBrush线性渐变绘制区域
RadialGradientBrush径向渐变绘制区域,焦点定义渐变的开始,椭圆定义渐变的重点
SolidColorBrush单色绘制区域
VideoBrush视频内容绘制区域

Font类

Font类,绘制文本时,可以设置字体的样式、大小、以及字体的种类。

using System.Drawing.Drawing2D;
using System.Numerics;
using System.Security.Cryptography.Xml;
using System.Threading;


namespace GDIDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Black);
            g.DrawLine(p, 0, this.Height / 2, this.Width, this.Height / 2);
            p.Dispose();
            g.Dispose();
        }


        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Red);
            g.DrawRectangle(p, 50, 50, 200, 200);
            p.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 绘制圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Blue);
            g.DrawEllipse(p, 50, 50, 100, 100);
            p.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 绘制圆柱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            int height = this.ClientSize.Height - 100;
            int width = this.ClientSize.Width - 50;
            int vHeight = 200;
            int vWidth = 100;

            Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            Pen pen = new Pen(Color.Gray);
            SolidBrush brush = new SolidBrush(Color.Gainsboro);
            for (int i = height / 2; i > 0; i--)
            {
                g.DrawEllipse(pen, width / 2, i, vHeight, vWidth);
            }
            g.FillEllipse(brush, width / 2, 0, vHeight, vWidth);
        }

        /// <summary>
        /// 绘制填充矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Blue);
            Brush brush = pen.Brush;
            Rectangle rect = new Rectangle(50, 50, 100, 100);
            g.FillRectangle(brush, rect);
            pen.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 绘制渐变圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rect = new Rectangle(150, 50, 200, 200);
            LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Purple, 90);
            g.FillEllipse(brush, rect);
            brush.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Blue);
            Font f = new Font("隶书", 24, FontStyle.Italic);
            g.DrawString("Windows 应用程序设计", f, pen.Brush, 50, 50);
            pen.Dispose();
            g.Dispose();
        }


        private void button9_Click(object sender, EventArgs e)
        {
            using (Graphics g = this.CreateGraphics())
            {
                g.Clear(Color.White);
            }
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
#if false
            Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            Pen myPen = new Pen(Color.Red, 3);
            g.DrawRectangle(myPen, 0, 0, 200, 100);
            g.DrawEllipse(myPen, 0, 0, 200, 100);
            g.Dispose();
            myPen.Dispose();
#endif
        }

        /// <summary>
        /// 坐标平移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {

            Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            Pen myPen = new Pen(Color.Red, 3);

            g.DrawRectangle(myPen, 0, 0, 200, 100);
            g.DrawEllipse(myPen, 0, 0, 200, 100);
            Thread.Sleep(1000);

            g.Clear(Color.White);
            g.TranslateTransform(30, 30);
            g.DrawRectangle(myPen, 0, 0, 200, 100);
            g.DrawEllipse(myPen, 0, 0, 200, 100);
            g.Dispose();
            myPen.Dispose();
        }

        /// <summary>
        /// 坐标缩放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            Pen myPen = new Pen(Color.Red, 3);
            g.DrawRectangle(myPen, 0, 0, 200, 100);
            g.DrawEllipse(myPen, 0, 0, 200, 100);
            Thread.Sleep(1000);

            g.Clear(Color.White);
            g.ScaleTransform(1.5f, 2.0f); // 横轴放到1.5倍,纵轴放大2倍
            g.DrawRectangle(myPen, 0, 0, 200, 100);
            g.DrawEllipse(myPen, 0, 0, 200, 100);
            g.Dispose();
            myPen.Dispose();


        }

        /// <summary>
        /// 绘制波形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button11_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            Pen pen = new Pen(Color.Blue, 3);
            Point p1 = new Point(30, this.ClientSize.Height - 200);
            Point p2 = new Point(this.ClientSize.Width - 100, this.ClientSize.Height - 200);
            Point p3 = new Point(30, 30);
            g.DrawLine(pen, p1, p2);
            g.DrawLine(pen, p1, p3);
            Font f = new Font("宋体", 12, FontStyle.Bold);
            g.DrawString("x", f, pen.Brush, p2);
            g.DrawString("y", f, pen.Brush, 10, 10);


            double x1, x2, y1, y2, a;
            x1 = x2 = y1 = y2 = 0;
            y2 = this.ClientSize.Height - 200;
            for (x2 = 0; x2 < this.ClientSize.Width; x2++)
            {
                a = 2 * Math.PI * x2 / this.ClientSize.Width;
                y2 = Math.Sin(a);
                y2 = (1 - y2) * (this.ClientSize.Height - 200) / 2;
                g.DrawLine(pen, (int)(x1 + 30), (int)y1, (int)(x2 + 30), (int)y2);
                x1 = x2;
                y1 = y2;
            }


        }

        /// <summary>
        /// 绘制饼状图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button12_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rect = new Rectangle(50, 50, 200, 100);
            Brush brush = new SolidBrush(Color.Blue);
            g.FillPie(brush, rect, 0, 60);
            g.FillPie(brush, rect, 60, 150);
            brush = new SolidBrush(Color.Yellow);
            g.FillPie(brush, rect, 210, 150);
            brush.Dispose();
            g.Dispose();
        }
    }
}

在这里插入图片描述

PictureBox图像控件

PictureBox控件,图片框是操作图形图像的基本控件,主要用于显示,保存图形图像信息,其主要属性方法:

  • 属性:
    • Image:设置或获取与该控件显示的图像
    • SizeMode:指示如何显示图像
  • 方法:
    • Load:显示图像

Bitmap类

Bitmap类,封装GDI+位图,此位图由图形图像及其属性的像素数据组成。Bitmap是用于处理由像素数据定义的图像的对象,其常用属性及方法:

名称说明
Size获取图像的以像素为单位的宽度和高度
Width获取图像的宽度
Height获取图像的高度
FromFile()从指定的文件创建图像
FromStream()从指定的数据流创建图像
GetPixel获取此Bitmap中指定像素的颜色
MakeTransparent使默认的透明颜色对此Bitmap透明
Save将此图像以指定的格式保存到指定的流中
RotateFlip旋转、镜像或同时旋转和镜像图像

        /// <summary>
        /// 创建位图并保存为图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bm);

            Font f = new Font("隶书", 24, FontStyle.Italic);
            Pen pen = new Pen(Color.OrangeRed);
            g.DrawString("创建位图测试保存图像", f, pen.Brush, 0, 0);
            pictureBox1.Image = bm;
            pictureBox1.Show();

            bm.Save("c:\\temp\\1.bmp");
            pen.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 加载图片并另存图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button14_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "图像文件*.bmp|*.bmp|*.jpg|*.png";
            if (openfile.ShowDialog() == DialogResult.OK)
            { 
                pictureBox1.Image = Image.FromFile(openfile.FileName);
                pictureBox1.Image.Save("c:\\temp\\2.bmp");
            }
        }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1132846.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

进阶课3——神经网络

1.定义与分类 神经网络是一种模仿动物神经网络行为特征&#xff0c;进行分布式并行信息处理的算法数学模型。它由大量的节点&#xff08;或神经元&#xff09;相互关联而成&#xff0c;每个节点代表一种特定的输出函数&#xff08;或称为运算&#xff09;&#xff0c;称为激励…

高等数学啃书汇总重难点(七)微分方程

同济高数上册的最后一章&#xff0c;总的来说&#xff0c;这篇章内容依旧是偏记忆为主&#xff0c;说难不难说简单不简单&#xff1a; 简单的是题型比较死&#xff0c;基本上就是记公式&#xff0c;不会出现不定积分一般花样繁多的情况&#xff1b;然而也就是背公式并不是想的…

Java-枚举

文章目录 枚举使用优缺点 方法 枚举 Java中的枚举是一种特殊的类&#xff0c;它用于定义一组有限的常量。 枚举在Java中被视为数据类型&#xff0c;你可以使用它们来创建枚举类型的变量&#xff0c;然后使用那些变量等。 枚举类使用enum关键字进行定义。 例如&#xff1a;在…

一篇了解springboot3请求参数种类及接口测试

SpringBoot3数据请求&#xff1a; 原始数据请求&#xff1a; //原始方式RequestMapping("/simpleParam")public String simpleParam(HttpServletRequest request){//获取请求参数String name request.getParameter("name");String age request.getParam…

我的前端笔记

HTML ./当前文件夹 ../返回上一级 ../../返回上上级 如果点击链接需要在新标签中打开<a href"#" target"_blank"> 网页中音频和视频都是默认不会自动播放的&#xff0c;视频得配合muted&#xff08;静音&#xff09;和autoplay配合使用 无语义…

Peter算法小课堂—球盒问题

球盒问题有8类&#xff0c;分别如下图 相同的球放入相同的盒子 根据上一篇Peter算法小课堂—正整数拆分-CSDN博客 简单来说&#xff0c;就这样&#x1f447; 将相同的球放入相同的盒子&#xff0c;其实相当于将正整数i分为j个正整数的个数 cin>>n>>m; if(n<…

Java|学习|多线程

1.多线程的一些概念 进程&#xff1a;是正在运行的程序 是系统进行资源分配和调用的独立单位 每一个进程都有它自己的内存空间和系统资源。 线程&#xff1a;是进程中的单个顺序控制流&#xff0c;是一条执行路径。 单线程&#xff1a;一个进程如果只有一条执行路径&#xff0…

前端,CSS,背景颜色跟随轮播图片改变而改变(附源码)

首先看效果&#xff01; 比如轮播图时红色&#xff0c;那么背景的底色也是红色&#xff0c;轮播图时黄色&#xff0c;背景的底色也是黄色&#xff0c;这就是根据轮播图的图片切换&#xff0c;而改变背景颜色随轮播图颜色一致 话不多说&#xff0c;直接上代码&#xff01;非常简…

Linux系统编程学习 NO.8 ——make和Makefile、进度条程序

前言 今天是1024程序员节&#xff0c;不知不觉离第一次写博客已经过去了一年了。在此祝各位程序员不写bug&#xff0c;不再秃头。 make和Makefile 什么是make和Makefile&#xff1f; make和Makefile是软件开发时所用到的工具和文件。make是一个指令工具。Makefile是一个当前…

避雷!新增2本期刊被标记为「On Hold」,1区TOP刊仍在调查中!

近期小编在Master Journal List上查询期刊时偶然发现&#xff0c;又有2本期刊被科睿唯安标记为「On Hold」&#xff01; 这2本期刊分别为MIGRATION LETTERS和REVISTA DE GESTAO E SECRETARIADO-GESEC. 此外还有6本期刊被标记为「On Hold」&#xff0c;目前共计8本期刊被「On …

Fedora系统的部署与MobaXterm的使用

Fedora Fedora简介 Fedora&#xff0c;Fedora Linux&#xff08;第七版以前为Fedora Core&#xff09;是由Fedora项目社区开发、红帽公司赞助&#xff0c;目标是创建一套新颖、多功能并且自由&#xff08;开放源代码&#xff09;的操作系统。Fedora是商业化的Red Hat Enterpr…

Bwapp靶场下载安装

bwapp安装 bWAPP&#xff08;Buggy Web Application&#xff09;是一个用于学习和测试Web应用程序安全的漏洞性Web应用程序。bWAPP通过提供多种常见Web应用程序漏洞&#xff08;例如跨站点脚本&#xff08;XSS&#xff09;、SQL注入、文件包含等&#xff09;来帮助用户了解和熟…

可观察性支柱:探索日志、指标和跟踪

通过检查系统输出来测量系统内部状态的能力称为可观察性。当可以仅使用输出信息&#xff08;即传感器数据&#xff09;来估计当前状态时&#xff0c;系统就变得“可观察”。您可以使用来自 Observability 的数据来识别和解决问题、优化性能并提高安全性。 在接下来的几节中&am…

幸狐LuckFox Pico RV1103微型Linux开发板 上手教程分享—02:SDK 环境部署

续上01教程分享&#xff0c;今天主要分享Luckfox Pico SDK 的环境部署 LuckfoxPico-SDK是基于Ubuntu LTS 系统开发测试的&#xff0c;在开发过程中&#xff0c;主要是用Ubuntu 18.04版本&#xff0c; 为了不必要的麻烦&#xff0c;我们推荐用户使用…

别再吹 GPT-4V 了!连北京烤鸭都不认识,你敢信??

夕小瑶科技说 原创 作者 | 智商掉了一地、ZenMoore GPT-4 被吹的神乎其神&#xff0c;作为具备视觉能力的 GPT-4 版本——GPT-4V&#xff0c;也被大众寄于了厚望。但如果告诉你&#xff0c;GPT-4V 连图片上的“北京烤鸭”和“广西烤鸭”都分不清楚&#xff0c;你是否觉得大跌眼…

电子邮件发送接收原理(附 go 语言实现发送邮件)

前言 首先要了解电子邮件的发送接收&#xff0c;不是点到点的。我想给你传达个消息&#xff0c;不是直接我跑到你家里喊你&#xff1a;“嘿&#xff0c;xxx&#xff0c;是你的益达&#xff0c;快拿走”。 而是类似快递的发送收取方式&#xff0c;是有服务器的中转的。我先将我…

1个月5次发版:测试人的模块测试策略分类归纳

笔者所在项目经历了一个月开发周期&#xff0c;该项目有5名开发人员&#xff0c;1名项目经理&#xff0c;1名测试人员&#xff0c;涵盖OA系统8个模块&#xff0c;在短短1个月中进行了5次发布。 现进行模块测试策略分类归纳。 已有模块 配置项优化 对于已有模块的配置项优化&…

【Python】collections.Counter

Python内置模块collections中的Counter是字典子类。Counter不是字典&#xff0c;但很像字典。 Counter具有字典的键和值&#xff0c;键是各个元素&#xff0c;值为该元素出现的次数。 Counter相当于计数器。常用于哈希映射&#xff08;哈希表&#xff09;。 from collection…

Unity Inspector编辑器扩展,枚举显示中文,枚举值自定义显示内容

记录&#xff01;Unity Inspector面板编辑器扩展&#xff0c;枚举显示中文&#xff0c;枚举值自定义显示内容&#xff0c;显示部分选项。效果如下&#xff1a; 枚举类代码&#xff1a; using System.Collections; using System.Collections.Generic; using UnityEngine;public…