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
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?
谁能看到我做错了什么?
3 个解决方案
#1
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:
这似乎是微软自至少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
alt text http://www.freeimagehosting.net/uploads/f6ae684547.png
#2
Your problem is DrawRectangle. The starting location of your rectangle reaches the end of your initial bitmap.
你的问题是DrawRectangle。矩形的起始位置到达初始位图的末尾。
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);
}
#3
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:
我用TryGdiPlus尝试了你的代码(对于这类东西非常有用,BTW)。我设法制作矩形绘图而没有剪裁,宽度为99像素:
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.
所以看起来即使旋转后位图的宽度仍然是300像素。
#1
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:
这似乎是微软自至少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
alt text http://www.freeimagehosting.net/uploads/f6ae684547.png
#2
Your problem is DrawRectangle. The starting location of your rectangle reaches the end of your initial bitmap.
你的问题是DrawRectangle。矩形的起始位置到达初始位图的末尾。
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);
}
#3
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:
我用TryGdiPlus尝试了你的代码(对于这类东西非常有用,BTW)。我设法制作矩形绘图而没有剪裁,宽度为99像素:
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.
所以看起来即使旋转后位图的宽度仍然是300像素。