首页 > 编程语言 >c#中Graphics常用的属性

c#中Graphics常用的属性

时间:2024-09-04 11:51:59浏览次数:6  
标签:Point c# Pen Graphics new 100 绘制 属性

Graphics介绍

在C#中,Graphics 类是 System.Drawing 命名空间的一部分,它提供了用于绘制线条、形状和图像的方法。Graphics 对象通常与 Graphics 类的 FromImageFromHdcFromHwnd 等方法一起使用,以获取用于绘制的 Graphics 对象。以下是一些 Graphics 类中常用的属性和方法:

  1. Clip:获取或设置用于限定绘制区域的 Region 对象。

  2. CompositingMode:获取或设置绘图的合成模式。

  3. CompositingQuality:获取或设置绘图的合成质量。

  4. InterpolationMode:获取或设置插值模式,影响图像缩放时的图像质量。

  5. PageScale:获取或设置绘图时的缩放比例。

  6. PageUnit:获取或设置绘图时使用的单位。

  7. PixelOffsetMode:获取或设置像素偏移模式,影响绘制文本和线条时的像素对齐方式。

  8. RenderingOrigin:获取或设置绘图原点的位置。

  9. SmoothingMode:获取或设置绘图时的平滑模式,用于控制抗锯齿。

  10. TextRenderingHint:获取或设置文本渲染的提示,影响文本的渲染质量。

  11. Transform:获取或设置 Graphics 对象的变换矩阵,用于执行平移、缩放、旋转等变换。

除了这些属性,Graphics 类还提供了许多绘制方法,如:

  • DrawLine:绘制一条直线。

  • DrawLines:绘制多条直线。

  • DrawCurve:绘制一条曲线。

  • DrawRectangle:绘制一个矩形。

  • DrawEllipse:绘制一个椭圆。

  • DrawPolygon:绘制一个多边形。

  • DrawPath:绘制一个路径。

  • DrawString:绘制文本字符串。

  • FillRectangle:用指定的画笔填充矩形。

  • FillEllipse:用指定的画笔填充椭圆。

  • FillPolygon:用指定的画笔填充多边形。

  • FillPath:用指定的画笔填充路径。

  • MeasureString:测量文本字符串的尺寸。

使用 Graphics 类时,通常需要处理 Graphics 对象的生命周期,包括创建、使用和释放。例如,当在窗体或控件上绘制时,可以在 Paint 事件处理器中获取 Graphics 对象,并在绘制完成后释放它。

Graphics具体使用方法

在C#中,Graphics 类的属性和方法用于在图像、窗口、打印机等表面上进行绘制。以下是一些常用 Graphics 属性和方法的具体使用方法:

属性

  1. Clip

    using System.Drawing;
    using System.Drawing.Drawing2D;
    ​
    Graphics g = ...;
    Region clipRegion = new Region(0, 0, 100, 100); // 定义一个矩形区域
    g.Clip = clipRegion;

  2. CompositingMode

    g.CompositingMode = CompositingMode.SourceOver; // 设置合成模式为源覆盖

  3. CompositingQuality

    g.CompositingQuality = CompositingQuality.HighQuality; // 设置合成质量为高质量

  4. InterpolationMode

    g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 设置插值模式为高质量双三次插值

  5. PageScale

    g.PageScale = 2.0f; // 设置缩放比例为2

  6. PageUnit

    g.PageUnit = GraphicsUnit.Pixel; // 设置绘图单位为像素

  7. PixelOffsetMode

    g.PixelOffsetMode = PixelOffsetMode.HighQuality; // 设置像素偏移模式为高质量

  8. RenderingOrigin

    g.RenderingOrigin = new Point(10, 10); // 设置绘图原点偏移

  9. SmoothingMode

    g.SmoothingMode = SmoothingMode.AntiAlias; // 设置平滑模式为抗锯齿

  10. TextRenderingHint

    g.TextRenderingHint = TextRenderingHint.AntiAlias; // 设置文本渲染提示为抗锯齿

  11. Transform

    g.Transform = new Matrix(); // 获取或设置变换矩阵

方法

  1. DrawLine

    Pen pen = new Pen(Color.Black);
    g.DrawLine(pen, 0, 0, 100, 100); // 绘制从(0,0)到(100,100)的直线

  2. DrawLines

    Pen pen = new Pen(Color.Black);
    Point[] points = { new Point(0, 0), new Point(100, 100), new Point(200, 200) };
    g.DrawLines(pen, points); // 绘制多条线段

  3. DrawCurve

    Pen pen = new Pen(Color.Black);
    Point[] points = { new Point(0, 0), new Point(100, 100), new Point(200, 0) };
    g.DrawCurve(pen, points); // 绘制一条曲线

  4. DrawRectangle

    Pen pen = new Pen(Color.Black);
    g.DrawRectangle(pen, 0, 0, 100, 100); // 绘制一个矩形

  5. DrawEllipse

    Pen pen = new Pen(Color.Black);
    g.DrawEllipse(pen, 0, 0, 100, 100); // 绘制一个椭圆

  6. DrawPolygon

    Pen pen = new Pen(Color.Black);
    Point[] points = { new Point(0, 0), new Point(100, 0), new Point(50, 100) };
    g.DrawPolygon(pen, points); // 绘制一个多边形

  7. DrawPath

    Pen pen = new Pen(Color.Black);
    GraphicsPath path = new GraphicsPath();
    path.AddEllipse(0, 0, 100, 100);
    g.DrawPath(pen, path); // 绘制一个路径

  8. DrawString

    using (Font font = new Font("Arial", 12))
    {
        Brush brush = new SolidBrush(Color.Black);
        g.DrawString("Hello, World!", font, brush, 10, 10); // 绘制文本
    }

  9. FillRectangle

    Brush brush = new SolidBrush(Color.Blue);
    g.FillRectangle(brush, 0, 0, 100, 100); // 填充一个矩形

  10. FillEllipse

    Brush brush = new SolidBrush(Color.Blue);
    g.FillEllipse(brush, 0, 0, 100, 100); // 填充一个椭圆

  11. FillPolygon

    Brush brush = new SolidBrush(Color.Blue);
    Point[] points = { new Point(0, 0), new Point(100, 0), new Point(50, 100) };
    g.FillPolygon(brush, points); // 填充一个多边形

  12. FillPath

    Brush brush = new SolidBrush(Color.Blue);
    GraphicsPath path = new GraphicsPath();
    path.AddEllipse(0, 0, 100, 100);
    g.FillPath(brush, path); // 填充一个路径

  13. MeasureString

    using (Font font = new Font("Arial", 12))
    {
        SizeF size = g.MeasureString("Hello, World!", font);
        Console.WriteLine($"Width: {size.Width}, Height: {size.Height}"); // 测量文本尺寸
    }

在使用 Graphics 对象时,通常在 Paint 事件中获取 Graphics 对象,例如在 Windows Forms 应用程序中:

private void MyControl_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    // 使用 g 对象进行绘制
}

请确保在使用完 Graphics 对象后正确地释放资源,例如通过调用 Dispose 方法。

标签:Point,c#,Pen,Graphics,new,100,绘制,属性
From: https://blog.csdn.net/weixin_64532720/article/details/141889573

相关文章

  • ioc
    publicclassIoc:IServiceProvider{privateIoc_root;privateConcurrentDictionary<Type,ServiceRegistry>_registries=newConcurrentDictionary<Type,ServiceRegistry>();privateConcurrentDictionary<Type,object?>_s......
  • 打卡信奥刷题(696)用Scratch图形化工具信奥B3922[普及组/提高] [GESP202312 一级] 小杨
    [GESP202312一级]小杨报数题目描述小杨需要从111到NNN报数......
  • pip install 安装时,提示【 Could not install packages due to an OSError: [Errno 13
    参考资料:【Python】已解决:ERROR:CouldnotinstallpackagesduetoanOSError:[WinError5]拒绝访问。我的问题:使用pipinstall安装时,遇到【CouldnotinstallpackagesduetoanOSError:[Errno13]Permissiondenied】的错误,提示可能需要【--user】选项:pipinstall......
  • c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
    1.方法一:在contextMenuStrip1打开时获取控件名称双击contextMenuStrip1在它的opening事件中写入下面的代码:privatevoidcontextMenuStrip1_Opening(objectsender,CancelEventArgse){stringwhichcontrol_name=(senderasContextMenuStrip).So......
  • C# HttpUtility.HtmlAttributeEncode 改用 js 实现
    System.Web.HttpUtility.HtmlAttributeEncode()的实现privatestaticvoidHtmlAttributeEncodeInternal(stringvalue,HttpWriterwriter){intindex=HttpEncoder.IndexOfHtmlAttributeEncodingChars(value,0);if(index==-1){write......
  • C#设计模式入门实战教程
    思维导航什么是设计模式设计模式的作用设计模式分类创建型模式(CreationalPatterns)结构型模式(StructuralPatterns)行为型模式(BehavioralPatterns)C#实现的设计模式示例代码推荐学习书籍项目源码地址优秀项目和框架精选什么是设计模式设计模式是对面向对象设计中......
  • PLC结构化文本(ST)——This&Super(关键字)
    PLCStructuredTextObjectOrientedProgrammingPLC结构化文本(ST)——This&Super(关键字)关键字This和Super是什么?this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针。super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类......
  • 前端项目实战Uniapp移动端项目+Vue3+Typescript+AntdVue管理平台
    ‌前端项目实战:‌构建Uniapp移动端项目与Vue3+Typescript+AntdVue管理平台‌在当今的前端开发领域,‌技术的不断迭代和创新为开发者带来了更多的选择和可能性。‌本文将介绍如何使用Uniapp框架开发移动端项目,‌并结合Vue3、‌Typescript以及AntdVue来构建一个高效的管理平台。......
  • js async/await 用法
    1.使用async/await可以更好地控制事件循环,像处理DOM事件或定时器等场合。eg1......
  • 鸿蒙内核源码分析 (协处理器篇) | CPU 的好帮手
    本篇很重要,对CP15协处理所有16个寄存器一一介绍,可能是全网介绍CP15最全面的一篇,鸿蒙内核的汇编部分(尤其开机启动)中会使用,熟练掌握后看汇编代码将如虎添翼。协处理器协处理器 (co-processor)顾名思义是协助主处理器完成工作,例如浮点、图像、音频处理这一类外围工作。角色相当于......