您的位置:首页 > 其它

GDI+中启动双缓存后缩放失效的问题

2011-07-11 15:26 190 查看
在写一个小的C# Form测试程序时遇到一个奇怪的问题,情况是这样:在启动双缓存情况下,利用画布的PageScale属性实现缩放功能,发现并没有效果。

相关代码如下:

启动双缓存:

public Form1()
{
InitializeComponent();

this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

}

实现缩放功能:

private void Form1_Paint(object sender, PaintEventArgs e)
{

// Create a rectangle.
Rectangle rectangle1 = new Rectangle(20, 20, 50, 100);

// Draw its outline.
e.Graphics.DrawRectangle(Pens.SlateBlue, rectangle1);

// Change the page scale.
e.Graphics.PageScale = 2.0F;

// Call TranslateTransform to change the origin of the
// Graphics object.
e.Graphics.TranslateTransform(10.0F, 10.0F);

// Draw the rectangle again.
e.Graphics.DrawRectangle(Pens.Tomato, rectangle1);

// Set the page scale and origin back to their original values.
e.Graphics.PageScale = 1.0F;
e.Graphics.TranslateTransform(0.0F, 0.0F);

SolidBrush transparentBrush = new SolidBrush(Color.FromArgb(50,
Color.Yellow));

// Create a new rectangle with the coordinates you expect
// after setting PageScale and calling TranslateTransform:
// x = 10 + (20 * 2)
// y = 10 + (20 * 2)
// Width = 50 * 2
// Length = 100 * 2
Rectangle newRectangle = new Rectangle(50, 50, 100, 200);

// Fill in the rectangle with a semi-transparent color.
e.Graphics.FillRectangle(transparentBrush, newRectangle);

}

在网上搜索了相关问题,好像是这种使用方式存在问题,具体不详。

目前采用的解决方法是,将图形绘制的代码封装在相应的类中,至于之前的问题的原因尚不明白,有了解的大侠望指教!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: