C# 绘制直线 圆形 矩形(工业上位机)

一、WinForms 工业绘图

适合:设备状态图、工艺流程、实时数据可视化

1、基础绘图(无闪烁)

csharp 复制代码
using System.Drawing;
using System.Windows.Forms;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        DoubleBuffered = true; // 工业画面必开:防闪烁
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g = e.Graphics;

        // 高质量绘图(工业画面必须)
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

        // 1. 画直线(管道、连接线)
        using (Pen linePen = new Pen(Color.DodgerBlue, 2))
        {
            g.DrawLine(linePen, 50, 50, 300, 50);
        }

        // 2. 画矩形(设备、阀门)
        using (Pen rectPen = new Pen(Color.LimeGreen, 3))
        {
            g.DrawRectangle(rectPen, 50, 80, 200, 100);
        }

        // 3. 画实心矩形(状态指示)
        using (SolidBrush brush = new SolidBrush(Color.Orange))
        {
            g.FillRectangle(brush, 60, 90, 180, 80);
        }

        // 4. 画圆形(泵、电机)
        using (Pen circlePen = new Pen(Color.Red, 3))
        {
            g.DrawEllipse(circlePen, 350, 80, 100, 100);
        }

        // 5. 画实心圆(指示灯)
        using (SolidBrush brush = new SolidBrush(Color.Green))
        {
            g.FillEllipse(brush, 360, 90, 80, 80);
        }

        // 6. 画文字(设备编号)
        using (Font font = new Font("微软雅黑", 12, FontStyle.Bold))
        {
            g.DrawString("PUMP-01", font, Brushes.White, 365, 120);
        }
    }
}

2、工业设备示意图

csharp 复制代码
protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    int tankX = 50, tankY = 50, tankW = 200, tankH = 300;

    // 储罐(矩形)
    g.DrawRectangle(Pens.SteelBlue, tankX, tankY, tankW, tankH);

    // 液位(动态高度)
    int liquidHeight = 180;
    g.FillRectangle(Brushes.DeepSkyBlue, tankX + 2, tankY + tankH - liquidHeight, tankW - 4, liquidHeight);

    // 管道(直线)
    g.DrawLine(new Pen(Color.DimGray, 6), tankX + tankW / 2, tankY + tankH, tankX + tankW / 2, tankY + tankH + 100);

    // 泵(圆形)
    g.DrawEllipse(Pens.Red, tankX + tankW / 2 - 40, tankY + tankH + 120, 80, 80);
    g.FillEllipse(Brushes.LightCoral, tankX + tankW / 2 - 38, tankY + tankH + 122, 76, 76);

    // 阀门(矩形)
    g.DrawRectangle(Pens.DarkOrange, tankX + tankW / 2 - 25, tankY + tankH + 220, 50, 40);
}

3、实时刷新(绑定传感器数据)

csharp 复制代码
public class Device
{
    public float LiquidLevel { get; set; }
}

Device device = new Device();

private void timer1_Tick(object sender, EventArgs e)
{
    device.LiquidLevel = GetSensorValue(); // 从PLC/Modbus读取
    Invalidate(); // 触发重绘
}

protected override void OnPaint(PaintEventArgs e)
{
    // 根据液位动态绘制
    int h = (int)(300 * device.LiquidLevel / 100f);
    e.Graphics.FillRectangle(Brushes.Blue, 52, 348 - h, 196, h);
}

二、WPF 工业绘图

适合:高端上位机、触摸屏、大屏监控

1、XAML 绘图

xml 复制代码
<Window x:Class="WpfDrawing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Canvas>
        <!-- 直线 -->
        <Line X1="50" Y1="50" X2="300" Y2="50"
              Stroke="DodgerBlue" StrokeThickness="3"/>

        <!-- 矩形 -->
        <Rectangle Canvas.Left="50" Canvas.Top="100"
                   Width="200" Height="120"
                   Stroke="LimeGreen" StrokeThickness="3"
                   Fill="#8032CD32"/>

        <!-- 圆形 -->
        <Ellipse Canvas.Left="350" Canvas.Top="100"
                 Width="120" Height="120"
                 Stroke="Red" StrokeThickness="3"
                 Fill="LightCoral"/>
    </Canvas>
</Window>

2、WPF 动态绑定

xml 复制代码
<Ellipse Width="100" Height="100"
         Fill="{Binding PumpStatus, Converter={StaticResource StatusToColor}}"/>
csharp 复制代码
public bool PumpStatus { get; set; } // true=绿色,false=红色

参考代码 C# 绘制直线/圆形/矩形 示例源码 www.youwenfan.com/contentcsu/62731.html

三、工业绘图核心技巧

问题 解决方案
画面闪烁 DoubleBuffered = true
实时卡顿 定时刷新,不在 Paint 里做计算
坐标不准 使用逻辑坐标 → 屏幕坐标转换
图形选中 记录图形边界矩形
图层混乱 背景层 / 设备层 / 动态层
性能差 只刷新变化区域
相关推荐
无限进步_1 小时前
【C++】深入右值引用:移动语义与完美转发
java·开发语言·c++
弹简特1 小时前
【零基础学Python】01-注释+变量+标识符+输入输出
开发语言·python
小王C语言1 小时前
【线程同步与互斥】:互斥量(锁)、条件变量(唤醒等待线程)、生产者消费者模型
java·开发语言
idingzhi1 小时前
A股量化策略日报(2026年05月11日)
android·开发语言·python·kotlin
idolao1 小时前
CentOS 7 安装 libtool-1.5.22.tar.gz 详细步骤(源码编译、配置、验证)
开发语言·python
c++之路1 小时前
单例模式(Singleton Pattern)
开发语言·c++·单例模式
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第44题】【JVM篇】第4题:什么时候会触发 Young GC?什么时候会触发 Full GC?
java·开发语言·jvm·后端·面试
c++之路1 小时前
代理模式(Proxy Pattern)
开发语言·c++·代理模式