【问题标题】:c# drawing text using custom pixelsc#使用自定义像素绘制文本
【发布时间】:2014-05-17 20:38:19
【问题描述】:

我想知道这是否可能: 我有一个 c# 应用程序,它的内容类似于在窗体上绘制的由大约 11000 个圆圈组成的显示。

我想要实现的是能够在该显示器上绘制文本,但不是使用“真实”像素,而是使用在表单上绘制的圆圈(矩形)作为像素。

编辑 1:

当在 C# 中绘制文本时,您可以使用类似 Graphics.DrawString(...) 的方法,为该方法提供一个矩形(即坐标),文本应在其中绘制。然后,该文本使用屏幕像素在该矩形中绘制。我想做的是也绘制文本,但不使用屏幕像素,而是使用我的显示器组成的自定义像素。

编辑 2

用于在Form上绘制圆圈的方法; Circles 是一个由Circle 对象组成的列表,其中circleRectangle 返回应该绘制圆的坐标,Filled 告诉方法是否应该填充圆。

    public void DrawCircles(Graphics g)
    {

        graphics = g;
        graphics.SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        Pen pen = new Pen(Color.Black, penthickness);
        SolidBrush brush = new SolidBrush(Color.White);
        for (int j = 0; j < Circles.Count;j++ )
        {
            graphics.DrawEllipse(pen, Circles[j].CircleRectangle);
            if (Circles[j].Filled)
                brush.Color = fillColor;
            else
                brush.Color = Color.White;
            graphics.FillEllipse(brush, Circles[j].CircleRectangle);

        }

    }

这可能吗?如果可以,我该怎么做?

【问题讨论】:

标签: c# fonts pixels


【解决方案1】:

您可以使用 DrawText 方法在不可见的位图上书写,然后扫描位图的像素并打开相应的圆圈。

上周使用 DataGridView 的单元格进行了该操作。真的很简单。

这里有一些代码:

    public void drawText(string text, Font drawFont)
    {
        Bitmap bmp = new Bitmap(canvasWidth, canvasHeight);
        Graphics G = Graphics.FromImage(bmp);
        SolidBrush brush = new SolidBrush(paintColor);
        Point point = new Point( yourOriginX, yourOriginY );

        G.DrawString(text, drawFont, brush, point);

        for (int x = 0; x < canvasWidth; x++)
            for (int y = 0; y < canvasHeight; y++)
            {
                Color pix = bmp.GetPixel(x, y);
                    setCell(x, y, pix);     //< -- set your custom pixels here!
            }
        bmp.Dispose();
        brush.Dispose();
        G.Dispose();
    }

编辑:当然,您将使用您的尺寸和原点作为拉绳

【讨论】:

  • 非常感谢 - 看来这就是我要找的东西;我会尝试并尽快接受答案!
  • 当然,祝你好运。我注意到两件事: - 未设置的像素的 Aplha 通道值为 0。 - 放大显示抗锯齿像素光晕所有颜色的字母。如果您有任何问题,请提出!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 2021-10-31
  • 2015-06-20
  • 1970-01-01
相关资源
最近更新 更多