GDI绘图常用的方法

1.画矩形

Rectangle textCellBounds = new Rectangle(X,Y,Width,Height);
graphics.DrawRectangle(new Pen(ColorTranslator.FromHtml("#D7D7D7")), textCellBounds);

2.填充矩形

LinearGradientBrush backBrush = new LinearGradientBrush(
                            this.Bounds,
                            SystemColors.ControlLightLight,
                            SystemColors.ControlLight,
                            LinearGradientMode.Vertical);
Rectangle textCellBounds = new Rectangle(X,Y,Width,Height);
pevent.Graphics.FillRectangle(backBrush, this.Bounds);

3.画/填充椭圆(圆形)

以矩形为框,获得内接椭圆

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle textCellBounds = new Rectangle(X,Y,Width,Height);
path.AddEllipse(textCellBounds);
Brush fillBrush = new SolidBrush(GetRandomColor());
//画椭圆 : DrawPath(Pen pen, GraphicsPath path);
graphics.FillPath(fillBrush, path);//填充椭圆

4.画/填充路径

graphics.DrawPath(Pen pen, GraphicsPath path);//划线
graphics.FillPath(fillBrush, path);//填充

5.加线

path.AddLine(left, top, left, top);
path.AddLine(left, top, right, top);
path.AddLine(right, top, right, botton);
path.AddLine(right, botton, left, botton);

6.画弧

GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(x, y, width, height, 180, 90);

参考https://blog.csdn.net/wqq_9/article/details/73188077?utm_source=blogxgwz1

6.几个点连线

Graphics类
public void FillPolygon (System.Drawing.Brush brush, System.Drawing.Point[] points);

7.写文本

方法一
//提供的快捷方式,类似的还有CheckBoxRenderer.DrawCheckBox 绘制checkBox
TextRenderer.DrawText
方法二
Graphics.DrawString

8.图片的缩放与裁剪(裁剪成圆)

private Image CutEllipse(Image img, Size size)
{
    try
    {
        Bitmap bitmap = new Bitmap(size.Width, size.Height);
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            using (TextureBrush brush = new TextureBrush(img, WrapMode.Tile))
            {
                brush.ScaleTransform((float)size.Width / img.Width, (float)size.Height / img.Height);
                InitGraphics(graphics, true);
                graphics.FillEllipse(brush, new Rectangle(Point.Empty, size));
            }
        }
        return bitmap;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.StackTrace);//TODO:v6.0 zkk 考虑写入日志
        return null;
    }
}

9.矩形放大

Rectangle textCellBounds = new Rectangle(X,Y,Width,Height);
textCellBounds.Inflate(X,Y);

10. 抗锯齿

private void InitGraphics(Graphics graphics, bool needOffsets = false)
{
    graphics.CompositingQuality = CompositingQuality.HighQuality;
    graphics.PixelOffsetMode = needOffsets ? PixelOffsetMode.Half : PixelOffsetMode.HighQuality;
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
}

微软API文档https://docs.microsoft.com/zh-cn/dotnet/api/

猜你喜欢

转载自blog.csdn.net/u013986317/article/details/83243755