C# Graphics 类:获取绘制内容的大小

发布于 2024-08-25 03:45:45 字数 373 浏览 4 评论 0原文

我正在动态写入 Graphics 对象,并且在所有输出都通过之前不知道最终图像的实际大小。

因此,我创建一个大图像并从中创建 Graphics 对象:

int iWidth = 600;
int iHeight = 2000;

bmpImage = new Bitmap(iWidth, iHeight);            
graphics = Graphics.FromImage(bmpImage);
graphics.Clear(Color.White);        

如何找到写入内容的实际大小,以便我能够创建具有此大小的新位图并将内容复制到其中。

在绘制之前计算内容大小确实很困难,想知道是否有其他解决方案。

I am writing to a Graphics object dynamically and don't know the actual size of the final image until all output is passed.

So, I create a large image and create Graphics object from it:

int iWidth = 600;
int iHeight = 2000;

bmpImage = new Bitmap(iWidth, iHeight);            
graphics = Graphics.FromImage(bmpImage);
graphics.Clear(Color.White);        

How can I find the actual size of written content, so I will be able to create a new bitmap with this size and copy the content to it.

It is really hard to calculate the content size before drawing it and want to know if there is any other solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-09-01 03:45:45

最好的解决方案可能是跟踪绘制时使用的最大 X 和 Y 值,尽管这将是一个完全手动的过程。

另一种选择是扫描位图的完整行和列(从右侧和底部开始),直到遇到非白色像素,但这将是一个非常低效的过程。

int width = 0;
int height = 0;

for(int x = bmpImage.Width - 1, x >= 0, x++)
{
    bool foundNonWhite = false;

    width = x + 1;

    for(int y = 0; y < bmpImage.Height; y++)
    {
        if(bmpImage.GetPixel(x, y) != Color.White)
        {
            foundNonWhite = true;
            break;
        }
    }

    if(foundNonWhite) break;
}

for(int y = bmpImage.Height - 1, x >= 0, x++)
{
    bool foundNonWhite = false;

    height = y + 1;

    for(int x = 0; x < bmpImage.Width; x++)
    {
        if(bmpImage.GetPixel(x, y) != Color.White)
        {
            foundNonWhite = true;
            break;
        }
    }

    if(foundNonWhite) break;
}

再次强调,我不建议将此作为解决方案,但它做您想要的事情,而无需跟踪您实际使用的坐标空间。

The best solution is probably to keep track of the maximum X and Y values that get used as you draw, though this will be an entirely manual process.

Another option would be to scan full rows and columns of the bitmap (starting from the right and the bottom) until you encounter a non-white pixel, but this will be a very inefficient process.

int width = 0;
int height = 0;

for(int x = bmpImage.Width - 1, x >= 0, x++)
{
    bool foundNonWhite = false;

    width = x + 1;

    for(int y = 0; y < bmpImage.Height; y++)
    {
        if(bmpImage.GetPixel(x, y) != Color.White)
        {
            foundNonWhite = true;
            break;
        }
    }

    if(foundNonWhite) break;
}

for(int y = bmpImage.Height - 1, x >= 0, x++)
{
    bool foundNonWhite = false;

    height = y + 1;

    for(int x = 0; x < bmpImage.Width; x++)
    {
        if(bmpImage.GetPixel(x, y) != Color.White)
        {
            foundNonWhite = true;
            break;
        }
    }

    if(foundNonWhite) break;
}

Again, I don't recommend this as a solution, but it will do what you want without your having to keep track of the coordinate space that you actually use.

冷夜 2024-09-01 03:45:45

只需检查这些属性的价值

float width = graphics.VisibleClipBounds.Width;
float height = graphics.VisibleClipBounds.Height;

代表边界矩形的 RectangleF 结构此 Graphics 的可见剪切区域。

Just check the value of these properties

float width = graphics.VisibleClipBounds.Width;
float height = graphics.VisibleClipBounds.Height;

A RectangleF structure that represents a bounding rectangle for the visible clipping region of this Graphics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文