C#、GDI+ - 为什么我的矩形被截断了?
当我运行以下代码时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是 Microsoft 至少从 2005 年起就意识到的 GDI+ 错误 (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=96328)。 我能够重现您描述的问题。 一种可能的解决方案是根据第一个位图创建第二个位图,并在其上进行绘制。 以下代码似乎正确绘制:
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:
alt text http://www.freeimagehosting.net/uploads/f6ae684547.png
你的问题是DrawRectangle。 矩形的起始位置到达初始位图的末尾。
如果您更改矩形的位置,您将能够完全看到它。
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.
我用 TryGdiPlus 尝试了你的代码(顺便说一句,对于这类事情非常有用)。
我设法使矩形绘制时没有裁剪,宽度为 99 像素:
所以看起来即使在旋转后位图的宽度仍然是 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:
So it looks as though the bitmap's width is still 300 pixels even after rotating.