当前位置: 首页 > news >正文

C#中的Graphics类和SetQuality()自定义方法

在 C# 中,Graphics 类是 System.Drawing 命名空间的一部分,它提供了一组方法和属性,用于在 Windows Forms 应用程序中进行二维绘图。Graphics 对象可以绘制文本、线条、曲线、形状和图像,并可以对它们进行变换和剪辑。

Graphics 类的一些常用功能和方法: 

1.绘制线条

DrawLine(Pen pen, int x1, int y1, int x2, int y2):使用指定的 Pen 对象绘制直线。

DrawLines(Pen pen, Point[] points):使用指定的 Pen 对象和点数组绘制一系列连续的线条。

public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel2.Paint += Panel2_Paint;
}
//DrawLine(Pen pen, int x1, int y1, int x2, int y2)
private void Panel1_Paint(object sender, PaintEventArgs e)
{//1.创建图形(画布,画板)Graphics g = e.Graphics;//2.设置绘制参数()SetQuality(g);//3.开始绘制Pen pen = new Pen(Color.Red, 10F);Point pt1 = new Point(50, 50);Point pt2 = new Point(100, 50);g.DrawLine(pen, pt1, pt2);//注意:起点的坐标,考虑画笔的宽度Pen pen1 = new Pen(Color.Blue, 10F);Point pt3 = new Point(100 + 5, 50 - 5);Point pt4 = new Point(100 + 5, 100);g.DrawLine(pen1, pt3, pt4);
}
//DrawLines(Pen pen, Point[] points)
private void Panel1_Paint(object sender, PaintEventArgs e)
{Pen pen = new Pen(Color.Red, 10F);Point[] points = new Point[]{new Point(80,150),new Point(80,20),new Point(20,100),new Point(120,100),};g.DrawLines(pen, points);
}

2.绘制形状: 

DrawRectangle(Pen pen, int x, int y, int width, int height):使用指定的 Pen 对象绘制矩形。

DrawEllipse(Pen pen, int x, int y, int width, int height):使用指定的 Pen 对象绘制椭圆。

DrawPolygon(Pen pen, Point[] points):使用指定的 Pen 对象和点数组绘制多边形。

public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel2.Paint += Panel2_Paint;this.panel3.Paint += Panel3_Paint;
}
//矩形DrawRectangle(Pen pen, int x, int y, int width, int height)
private void Panel1_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,100,100);g.DrawRectangle(pen1, rect);
}
//椭圆DrawEllipse(Pen pen, int x, int y, int width, int height)
private void Panel2_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,80,100);g.DrawEllipse(pen1, rect);//椭圆
}
//多边形DrawPolygon(Pen pen, Point[] points)
private void Panel3_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Point[] points = new Point[]{new Point(80,20),new Point(20,100),new Point(120,100),};g.DrawPolygon(pen1, points);
}

 3.填充形状

FillRectangle(Brush brush, int x, int y, int width, int height):使用指定的 Brush 对象填充矩形。

FillEllipse(Brush brush, int x, int y, int width, int height):使用指定的 Brush 对象填充椭圆。

FillPolygon(Brush brush, Point[] points):使用指定的 Brush 对象填充多边形。

public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel2.Paint += Panel2_Paint;this.panel3.Paint += Panel3_Paint;
}
//FillRectangle(Brush brush, int x, int y, int width, int height)
private void Panel1_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,100,100);Brush brush = new SolidBrush(Color.Yellow);g.FillRectangle(brush, rect);g.DrawRectangle(pen1, rect);//矩形
}
//FillEllipse(Brush brush, int x, int y, int width, int height)
private void Panel2_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,80,100);Brush brush = new SolidBrush(Color.Yellow);g.FillEllipse(brush, rect);g.DrawEllipse(pen1, rect);//椭圆
}
//FillPolygon(Brush brush, Point[] points)
private void Panel3_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Point[] points = new Point[]{new Point(80,20),new Point(20,100),new Point(120,100),};Brush brush = new SolidBrush(Color.Yellow);g.DrawPolygon(pen1, points);g.FillPolygon(brush,points);
}

4.绘制文本: 

DrawString(String s, Font font, Brush brush, float x, float y):在指定位置绘制文本字符串。

public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;
}private void Panel1_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string s = "相寻梦里路,飞雨落花中";Font font = new Font("华文琥珀",20F);Brush brush  = new SolidBrush(Color.Pink);g.DrawString(s,font,brush,10,10);}

5.图像处理

DrawImage(Image image, Point point):在指定位置绘制图像。

DrawImage(Image image, Rectangle rect):在指定矩形区域内绘制图像。

public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel1.Paint += Panel2_Paint;
}        
private void Panel1_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);Point point = new Point( 0, 0);g.DrawImage(img, point);//在指定位置绘制图像img.Dispose(); // 释放图像资源
}
private void Panel2_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 rect = new Rectangle(0,0,300,240);g.DrawImage(img, rect);img.Dispose(); // 释放图像资源
}

 

6.变换和剪辑: 

TranslateTransform(float dx, float dy):对当前的坐标系统进行平移变换。

ScaleTransform(float sx, float sy):对当前的坐标系统进行缩放变换。

RotateTransform(float angle):对当前的坐标系统进行旋转变换。

SetClip(Rectangle rect):设置当前的剪辑区域。

代码在下面

1.平移变换                         2.缩放变换                        3. 旋转变换         

 

 7.获取信息

VisibleClipBounds:获取当前剪辑区域的边界。

IsVisible(Point point):判断一个点是否在可见区域内。

代码

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace 图形变换和剪辑
{public partial class Form1 : Form{public Form1(){InitializeComponent();Panel panel1 = new Panel{Size = new Size(400, 400),Location = new Point(50, 50),BackColor = Color.Green,};this.Controls.Add(panel1);panel1.Paint += Panel1_Paint; 平移变换panel1.Paint += Panel2_Paint; // 缩放变换panel1.Paint += Panel3_Paint; // 旋转变换panel1.Paint += Panel4_Paint; // 设置剪辑区域}private void Panel1_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);g.TranslateTransform(100, 0);g.DrawImage(img, new Point(0,0));}private void Panel2_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);g.ScaleTransform(1.5F,1.5F);//缩放g.DrawImage(img, new Point(-100, -100));}private void Panel3_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);g.RotateTransform(45); // 旋转变换g.DrawImage(img, new Point(0, 0));}private void Panel4_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 clipRect = new Rectangle(50, 50, 300, 300);g.SetClip(clipRect);//设置当前编辑区// 获取信息RectangleF visibleClipBounds = g.VisibleClipBounds;Console.WriteLine($"Visible Clip Bounds: {visibleClipBounds}");Point testPoint = new Point(100, 100);bool isVisible = g.IsVisible(testPoint);Console.WriteLine($"Point {testPoint} is visible: {isVisible}");}private static void SetQuality(Graphics g){g.SmoothingMode = SmoothingMode.AntiAlias;g.CompositingQuality = CompositingQuality.HighQuality;g.InterpolationMode = InterpolationMode.HighQualityBicubic;}}
}

SetQuality()

SetQuality 方法是一个自定义方法,它不是 System.Drawing 命名空间的一部分。这个方法通常用于设置 Graphics 对象的属性,以提高绘制质量,特别是在进行图形变换、绘制文本或图像时。

SetQuality 方法设置了以下几个关键属性:

  1. SmoothingMode:设置为 SmoothingMode.AntiAlias,以启用抗锯齿,使线条和曲线更平滑。
  2. InterpolationMode:设置为 InterpolationMode.HighQualityBicubic,以在图像缩放时使用高质量的双三次插值算法。
  3. PixelOffsetMode:设置为 PixelOffsetMode.HighQuality,以减少图像旋转和大字体文本时的像素偏移。
  4. TextRenderingHint:设置为 TextRenderingHint.AntiAliasGridFit,以提高文本渲染的质量和清晰度。
  5. CompositingQuality:设置为 CompositingQuality.HighQuality,以确保在合成图像时使用高质量的算法。
using System.Drawing;
using System.Drawing.Drawing2D;
public void SetQuality(Graphics g)
{// 设置高质量渲染模式g.SmoothingMode = SmoothingMode.AntiAlias; // 抗锯齿// 设置高质量的插值模式g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 高质量双三次插值// 设置高质量的像素偏移模式g.PixelOffsetMode = PixelOffsetMode.HighQuality; // 高质量像素偏移// 设置高质量的路径渐变g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; // 文本抗锯齿// 设置图形对象的线性变换和旋转变换的精度g.CompositingQuality = CompositingQuality.HighQuality; // 高质量合成
}// 在 Paint 事件处理程序中使用 SetQuality 方法
private void MyControl_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g); // 应用高质量设置// 现在使用 g 绘制文本、线条、形状等g.DrawString("Hello, World!", new Font("Arial", 16), Brushes.Black, new PointF(10, 10));
}

http://www.lryc.cn/news/434113.html

相关文章:

  • 圣诞节:白酒与西式料理的异国风情
  • 2.ChatGPT的发展历程:从GPT-1到GPT-4(2/10)
  • yjs01——机器学习的过程
  • Git工作流程
  • Qt-QWidget的font属性(18)
  • Go语言概述
  • P6627 [省选联考 2020 B 卷] 幸运数字
  • 活动|华院计算宣晓华受邀出席“AI引领新工业革命”大会,探讨全球科技的最新趋势
  • k8s配置
  • 力扣第79题 单词搜索
  • 【系统架构设计师】抽象工厂设计模式
  • 海外云手机有哪些推荐?
  • 旋转目标检测对照实验-mmrotate基础教程
  • Spring常见的面试问答题(一)
  • STM32 之 SDRAM 详解
  • 基于图神经网络的最大独立集问题的目标分支
  • 【Qt】事件过滤器
  • 字符串转换为整数、整数转换为字符串
  • 解决samba无权限创建文件问题
  • Ribbon快速了解
  • SpringBoot闲一品交易平台
  • 基于SpringBoot的物流管理系统
  • uniapp微信小程序开发踩坑日记:Pinia持久化报错Cannot read property ‘localStorage‘ of undefined
  • 负载均衡调度器--LVS
  • TinyWebSever源码逐行注释(五)_ http_conn.cpp
  • windows手工杀毒-寻找可疑进程之句柄
  • java开发后端
  • Redis 的标准使用规范之数据类型使用规范
  • 人工智能技术导论——基于产生式规则的机器推理
  • Apache Guacamole 安装及配置VNC远程桌面控制