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

在写一个小的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);

        }

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

目前采用的解决方法是,将图形绘制的代码封装在相应的类中,至于之前的问题的原因尚不明白,有了解的大侠望指教!

posted @ 2011-07-11 15:26  超仔  阅读(457)  评论(0编辑  收藏  举报