C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Drawing 类

C#中的Drawing 类案例详解

作者:工程师007

文章解析WPF与WinForms的Drawing类差异,涵盖命名空间、继承链、常用类及应用场景,通过案例展示如何创建带阴影圆角矩形按钮,强调WPF的轻量、可动画特性与WinForms的即时绘制模式,对C# Drawing类相关知识感兴趣的朋友一起看看吧

一、Drawing 是什么?

命名空间

继承链(WPF)

常用派生类:

特点

二、典型用法

三、案例:画一个“带阴影的圆角矩形按钮”

目标:

WPF:

<Window x:Class="DemoDrawing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Drawing 示例" Width="300" Height="200">
    <Grid>
        <!-- 使用 DrawingBrush 做背景 -->
        <Rectangle x:Name="btnRect" Width="180" Height="60"
                   MouseEnter="BtnRect_MouseEnter"
                   MouseLeave="BtnRect_MouseLeave">
            <Rectangle.Resources>
                <!-- 阴影 Drawing -->
                <GeometryDrawing x:Key="shadow"
                                 Geometry="M 5,5 175,5 175,55 5,55 Z"
                                 Brush="#80000000">
                    <GeometryDrawing.Pen>
                        <Pen Brush="Transparent" Thickness="1"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
                <!-- 按钮主体 Drawing -->
                <GeometryDrawing x:Key="body" Geometry="M 0,0 170,0 170,50 0,50 Z">
                    <GeometryDrawing.Brush>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Offset="0"  Color="#FF4C9AFF"/>
                            <GradientStop Offset="1"  Color="#FF0050DD"/>
                        </LinearGradientBrush>
                    </GeometryDrawing.Brush>
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF003399" Thickness="2"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
                <!-- 组合 DrawingGroup -->
                <DrawingGroup x:Key="combined">
                    <!-- 先画阴影 -->
                    <DrawingGroup.Children>
                        <DrawingGroup>
                            <DrawingGroup.Children>
                                <StaticResource ResourceKey="shadow"/>
                            </DrawingGroup.Children>
                            <DrawingGroup.BitmapEffect>
                                <BlurBitmapEffect Radius="5"/>
                            </DrawingGroup.BitmapEffect>
                        </DrawingGroup>
                        <!-- 再画按钮主体 -->
                        <StaticResource ResourceKey="body"/>
                    </DrawingGroup.Children>
                </DrawingGroup>
                <!-- 把 DrawingGroup 变成 Brush -->
                <DrawingBrush x:Key="btnBrush" Drawing="{StaticResource combined}"/>
            </Rectangle.Resources>
            <Rectangle.Fill>
                <StaticResource ResourceKey="btnBrush"/>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Window>

后台代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void BtnRect_MouseEnter(object sender, MouseEventArgs e)
    {
        // 找到 Drawing 里的渐变刷
        var rect = (Rectangle)sender;
        var brush = (DrawingBrush)rect.Fill;
        var dg = (DrawingGroup)brush.Drawing;
        var body = (GeometryDrawing)((DrawingGroup)dg.Children[1]).Children[0];
        var lg = (LinearGradientBrush)body.Brush;
        // 动画高亮
        var da = new ColorAnimation(Color.FromRgb(0x6F, 0xBA, 0xFF),
                                    TimeSpan.FromMilliseconds(300));
        lg.GradientStops[0].BeginAnimation(GradientStop.ColorProperty, da);
    }
    private void BtnRect_MouseLeave(object sender, MouseEventArgs e)
    {
        var rect = (Rectangle)sender;
        var brush = (DrawingBrush)rect.Fill;
        var dg = (DrawingGroup)brush.Drawing;
        var body = (GeometryDrawing)((DrawingGroup)dg.Children[1]).Children[0];
        var lg = (LinearGradientBrush)body.Brush;
        var da = new ColorAnimation(Color.FromRgb(0x4C, 0x9A, 0xFF),
                                    TimeSpan.FromMilliseconds(300));
        lg.GradientStops[0].BeginAnimation(GradientStop.ColorProperty, da);
    }
}

四、WinForms(GDI+)对应写法

WinForms 没有 Drawing 类,而是 GraphicsPath + LinearGradientBrush + Bitmap 的“即时模式”绘制。核心步骤:

protected override void OnPaint(PaintEventArgs e)
{
    var g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    // 1. 阴影
    using (var path = CreateRoundRect(5, 5, 175, 55, 8))
    using (var brush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
    using (var blur = new Bitmap(180, 60))
    {
        using (var g2 = Graphics.FromImage(blur))
        {
            g2.SmoothingMode = SmoothingMode.AntiAlias;
            g2.FillPath(brush, path);
        }
        // 手动高斯模糊(略)...
        g.DrawImage(blur, 0, 0);
    }
    // 2. 主体
    using (var path = CreateRoundRect(0, 0, 170, 50, 8))
    using (var brush = new LinearGradientBrush(
        new Point(0, 0), new Point(0, 50),
        Color.FromArgb(255, 0x4C, 0x9A, 0xFF),
        Color.FromArgb(255, 0x00, 0x50, 0xDD)))
    using (var pen = new Pen(Color.FromArgb(255, 0x00, 0x33, 0x99), 2))
    {
        g.FillPath(brush, path);
        g.DrawPath(pen, path);
    }
}
private GraphicsPath CreateRoundRect(float x, float y, float w, float h, float r)
{
    var gp = new GraphicsPath();
    gp.AddArc(x + w - r, y, r, r, 270, 90);
    gp.AddArc(x + w - r, y + h - r, r, r, 0, 90);
    gp.AddArc(x, y + h - r, r, r, 90, 90);
    gp.AddArc(x, y, r, r, 180, 90);
    gp.CloseFigure();
    return gp;
}

到此这篇关于C#Drawing 类详解的文章就介绍到这了,更多相关C# Drawing 类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文