简体   繁体   English

Windows窗体中用于绘制的C#双精度坐标

[英]C# double precision coordinates for drawing in Windows Form

first of all I'm new with C#, I always used Java. 首先,我是C#的新手,我一直使用Java。 First, 第一,

I wanted to ask "Can you have double coords?", I mean "Can you go deeper than a pixel when you draw to screen?", 我想问“你能有双座标吗?”,我的意思是“画到屏幕上时可以比像素更深吗?”,

and also, "Is windows form the best way to make a simple graphic application (like a simple doodle jump game)?" 而且,“ Windows是制作简单图形应用程序(例如简单的Doodle Jump游戏)的最佳方法吗?”

but that's simple curiosity. 但这很简单。 The problem is, that I need to draw some simple objects (points, lines, especially rectangles) with double/float/decimal precision (it doesn't matter, with more precision than an integer) on a windows form (I'm using Visual Studio), but I can't find anything. 问题是,我需要在Windows窗体上绘制一些简单的对象(点,线,尤其是矩形),其精度为double / float / decimal精度(没关系,比整数精度更高) Visual Studio),但我找不到任何东西。

I've seen like Pointf or RectangleF etc. but I'm not sure if they are used for this purpose. 我见过像Pointf或RectangleF等。但是我不确定它们是否用于此目的。 I've also seen that Rect (System.Windows.Shapes) uses double precision but are not usable with windows form (I'm not sure, but if possible, I would like to find the easiest way to do this thing on windows form). 我还看到Rect(System.Windows.Shapes)使用双精度,但不适用于Windows窗体(我不确定,但是如果可能的话,我想找到在Windows窗体上执行此操作的最简单方法)。

Hoping that I've been clear enough, thanks in advance. 希望我已经足够清楚,在此先感谢。

As noted in my comment, maybe that is even what you need. 正如我在评论中指出的那样,也许这就是您所需要的。

Graphics.DrawLine() and other drawing methods take overloads with single precision coordinates, either in form of individual floats or PointF , RectangleF structures . Graphics.DrawLine()和其他绘制方法采用单个精度坐标重载,可以是单个浮点数PointFRectangleF结构的形式

Results may vary depending on the SmoothingMode however. 但是,结果可能因SmoothingMode而异。

Eg: 例如:

using (var bmp = new Bitmap(100, 100))
{
    using (var g = Graphics.FromImage(bmp))
    {
        //Note that this enabled anti-aliasing
        g.SmoothingMode = SmoothingMode.HighQuality;

        g.Clear(Color.White);
        float x = 20.0f;
        float y1 = 1.0f;
        float y2 = 10.0f;
        g.DrawLine(Pens.Red, x, y1, x, y2);
        x = 20.5f;
        y1 = 11.0f;
        y2 = 20.0f;
        g.DrawLine(Pens.Red, x, y1, x, y2);
        x = 21.0f;
        y1 = 21.0f;
        y2 = 30.0f;
        g.DrawLine(Pens.Red, x, y1, x, y2);
    }
}

Here, the line is actually interpolated in intensity between the two coordinates. 在此,实际上是在两个坐标之间对强度进行了插值。 If SmoothingMode would be left at default, the line would be exactly 1 pixel wide and rounded to the nearest pixel. 如果将SmoothingMode保留为默认值,则该行将恰好为1像素宽,并四舍五入到最接近的像素。 You should test and choose the method that suits your graphic the most. 您应该测试并选择最适合您的图形的方法。

Here is the zoomed-in result with interpolation using SmoothingMode.HighQuality : 这是使用SmoothingMode.HighQuality进行插值的放大结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM