使用自定义像素绘制文本的 C# 图文
本文关键字:图文 文本 绘制 自定义 像素 | 更新日期: 2023-09-27 17:56:42
我想知道这是否可能:我得到了一个 c# 应用程序,它类似于在窗体上绘制的大约 11000 个圆圈组成的显示。
我想要实现的是能够在该显示器上绘制文本,但不使用"真实"像素,而是使用在表单上绘制的圆形(矩形)作为像素。
编辑 1:
在 c# 中绘制文本时,您将使用类似 Graphics.DrawString(...)
的东西,给方法一个矩形(所以坐标),文本应该在其中绘制。然后,使用屏幕像素在该矩形中绘制该文本。我想做的是绘制文本,但不要使用屏幕像素,而是使用我的显示器组成的自定义像素。
编辑 2
用于在表单上绘制圆圈的方法; 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);
}
}
这可能吗,如果是,我将如何做?
您可以使用 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();
}
编辑:当然,您将使用DrawString的尺寸和原点