C#、GDI+ - 为什么我的矩形被截断了?

发布于 2024-07-26 11:28:19 字数 1043 浏览 5 评论 0原文

当我运行以下代码时:

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(300, 400);
        using (Graphics g = Graphics.FromImage(b))
        {
            g.FillRectangle(Brushes.Black, new Rectangle(0, 0, 300, 400));
        }

        b.RotateFlip(RotateFlipType.Rotate90FlipNone);

        using (Graphics g2 = Graphics.FromImage(b))
        {
            g2.DrawRectangle(new Pen(Color.White, 7.2f), 200, 100, 150, 100);
        }

        using (Graphics g3 = this.panel1.CreateGraphics())
        {
            g3.DrawImage(b, 0, 0);
        }
    }

我得到以下内容:

alt text http://www.freeimagehosting. net/uploads/2c309ec21c.png

注意:

  • 只有当我旋转图像,然后绘制一个超出图像原始尺寸的矩形时才会发生这种情况。

  • 矩形未截断为原始图像宽度 - 只是矩形的右边缘未绘制。

  • 这种情况会在多种情况下发生。 我首先在一个更复杂的应用程序中注意到它 - 我只是编写这个应用程序来简单说明该问题。

谁能看到我做错了什么吗?

When I run the following code:

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(300, 400);
        using (Graphics g = Graphics.FromImage(b))
        {
            g.FillRectangle(Brushes.Black, new Rectangle(0, 0, 300, 400));
        }

        b.RotateFlip(RotateFlipType.Rotate90FlipNone);

        using (Graphics g2 = Graphics.FromImage(b))
        {
            g2.DrawRectangle(new Pen(Color.White, 7.2f), 200, 100, 150, 100);
        }

        using (Graphics g3 = this.panel1.CreateGraphics())
        {
            g3.DrawImage(b, 0, 0);
        }
    }

I get the following:

alt text http://www.freeimagehosting.net/uploads/2c309ec21c.png

Notes:

  • It only happens when I rotate an image, then draw a rectangle that extends past the original dimensions of the image.

  • The rectangle is not truncated to the original image width - just the right edge of the rectangle is not drawn.

  • This happens in a variety of scenarios. I first noticed it in a much more complicated app - I just wrote this app to make a simple illustration of the issue.

Can anyone see what I'm doing wrong?

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

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

发布评论

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

评论(3

高速公鹿 2024-08-02 11:28:19

这似乎是 Microsoft 至少从 2005 年起就意识到的 GDI+ 错误 (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=96328)。 我能够重现您描述的问题。 一种可能的解决方案是根据第一个位图创建第二个位图,并在其上进行绘制。 以下代码似乎正确绘制:

private void button1_Click(object sender, EventArgs e) {
    Bitmap b = new Bitmap(300, 400);
    using (Graphics g = Graphics.FromImage(b)) {
        g.FillRectangle(Brushes.Black, new Rectangle(0, 0, 300, 400));
    }

    b.RotateFlip(RotateFlipType.Rotate90FlipNone);
    Bitmap b2 = new Bitmap(b);

    using (Graphics g2 = Graphics.FromImage(b2)) {
        g2.DrawRectangle(new Pen(Color.White, 7.2f), 200, 100, 150, 100);
    }

    using (Graphics g3 = this.panel1.CreateGraphics()) {
        g3.DrawImage(b2, 0, 0);
    }
}

alt text http://www.freeimagehosting.net/uploads/ f6ae684547.png

This appears to be a GDI+ bug Microsoft has been aware of since at least 2005 (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=96328). I was able to repro the problem you describe. One possible solution would be to create a second bitmap off the first one, and draw on that. The following code seems to draw correctly:

private void button1_Click(object sender, EventArgs e) {
    Bitmap b = new Bitmap(300, 400);
    using (Graphics g = Graphics.FromImage(b)) {
        g.FillRectangle(Brushes.Black, new Rectangle(0, 0, 300, 400));
    }

    b.RotateFlip(RotateFlipType.Rotate90FlipNone);
    Bitmap b2 = new Bitmap(b);

    using (Graphics g2 = Graphics.FromImage(b2)) {
        g2.DrawRectangle(new Pen(Color.White, 7.2f), 200, 100, 150, 100);
    }

    using (Graphics g3 = this.panel1.CreateGraphics()) {
        g3.DrawImage(b2, 0, 0);
    }
}

alt text http://www.freeimagehosting.net/uploads/f6ae684547.png

救赎№ 2024-08-02 11:28:19

你的问题是DrawRectangle。 矩形的起始位置到达初始位图的末尾。

如果您更改矩形的位置,您将能够完全看到它。

using (Graphics g2 = Graphics.FromImage(b))
{
    g2.DrawRectangle(new Pen(Color.White, 7.2f), 50, 50, 150, 100);
}

Your problem is DrawRectangle. The starting location of your rectangle reaches the end of your initial bitmap.

If you change the location of your rectangle you will be able to see it completely.

using (Graphics g2 = Graphics.FromImage(b))
{
    g2.DrawRectangle(new Pen(Color.White, 7.2f), 50, 50, 150, 100);
}
悲念泪 2024-08-02 11:28:19

我用 TryGdiPlus 尝试了你的代码(顺便说一句,对于这类事情非常有用)。
我设法使矩形绘制时没有裁剪,宽度为 99 像素:

g2.DrawRectangle(new Pen(Color.White, 7.2f), 200, 100, 99, 100);

所以看起来即使在旋转后位图的宽度仍然是 300 像素。

I tried your code with TryGdiPlus (very useful for these kind of things, BTW).
I managed to make the rectangle draw without clipping with width of 99 pixels:

g2.DrawRectangle(new Pen(Color.White, 7.2f), 200, 100, 99, 100);

So it looks as though the bitmap's width is still 300 pixels even after rotating.

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