Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

fonts - c# drawing text using custom pixels

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...