c#使用自定义像素绘制文字 [英] c# drawing text using custom pixels

查看:997
本文介绍了c#使用自定义像素绘制文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道这是否可能:
我得到了一个类似于由表单上的11000个圆圈组成的显示的ac#应用程序。我想要实现的是能够在该显示器上绘制文本,而不是使用真实像素,而是使用在窗体上绘制的圆(矩形)作为像素。

编辑1:

例如 Graphics.DrawString(...),给这个方法一个矩形(所以坐标),在其中绘制文本。然后在那个矩形使用屏幕像素。我想要做的是绘制文本,但不使用屏幕像素,但我的显示器组成的自定义像素。
$ b

编辑2

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

  public void DrawCircles(Graphics g)
{

graphics = g;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
笔笔=新笔(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);





$ b $ p

这可能吗?如果是的话,我该怎么做?

解决方案

你可以使用DrawText方法写一个不可见的BitMap,然后扫描位图的像素,并打开相应的圈子。

上周用DataGridView的单元格做了这个。真正简单。



以下是一些代码:

  public void drawText (字符串文本,字体drawFont)
{
位图bmp =新的位图(canvasWidth,canvasHeight);
Graphics G = Graphics.FromImage(bmp);
SolidBrush brush = new SolidBrush(paintColor);
Point point = new Point(yourOriginX,yourOriginY);

G.DrawString(text,drawFont,brush,point); (int y = 0; y< canvasHeight; y ++)



$ pix = bmp.GetPixel(x,y);
setCell(x,y,pix); //< - 在这里设置你的自定义像素!
}
bmp.Dispose();
brush.Dispose();
G.Dispose();

编辑:您可以使用尺寸和DrawString的原点,当然是

I'm wondering if that's possible: I got a c# application with something like a display consisting of about 11000 circles drawn on the Form.

What I want to achieve is to be able to draw text on that display, but not using the "real" pixels, but using the circles (rectangles) drawn on the form as pixels.

Edit 1:

When drawing text in c#, you would i.e. use something like Graphics.DrawString(...), giving the method a rectangle (so coordinates) in which the text should be drawn in. That text then is drawn in that rectangle using the screen pixels. What I want to do is draw text as well but not using the screen pixels but my custom pixels of which my display consists.

Edit 2

Method used to draw the circles on the Form; Circles is a list consisting of Circle objects, where circleRectangle returns the coordinates in which the circle should be drawn and Filled tells the method if the circle should be filled or not.

    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);

        }

    }

Is this possible and if yes, how would I do that?

解决方案

You could write on an invisible BitMap with the DrawText method and then scan the bitmap's pixels and turn the corresponding circles on.

Did that last week with the cells of DataGridView. Real easy.

Here is some code:

    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();
    }

Edit: You would use your dimensions and your origin for the DrawString, of course

这篇关于c#使用自定义像素绘制文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆