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);
}
在网上搜索了相关问题,好像是这种使用方式存在问题,具体不详。
目前采用的解决方法是,将图形绘制的代码封装在相应的类中,至于之前的问题的原因尚不明白,有了解的大侠望指教!
相关代码如下:
启动双缓存:
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);
}
在网上搜索了相关问题,好像是这种使用方式存在问题,具体不详。
目前采用的解决方法是,将图形绘制的代码封装在相应的类中,至于之前的问题的原因尚不明白,有了解的大侠望指教!
相关文章推荐
- eclipse配置虚拟路径后,每次启动tomcat都会虚拟路径失效的问题解决
- 网页随鼠标滚动自动加载 之 网页缩放后失效问题解决方法
- PHP使用Memcache时模拟命名空间及缓存失效问题的解决
- 遇到问题-----web-----uploadify上传工具的坑----清除浏览器缓存后初始化失败不报错--uploadify初始化失效
- 使用 Retrofit 和 okhttp 强制缓存失效问题
- spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题
- 使用gdi+绘制缩放位图的问题
- 简单处理K->V缓存失效后高并发访问导致数据库负载急剧上升的问题
- xampp中apache启动失效问题
- 如何用 GDI+ 高质量地缩放图片 - 回复 "程序牛" 的问题
- memcached缓存失效时的高并发访问问题解决
- ejb客户端的三种调用方法,以及InitialContext lookup后的jndi对象在服务重启后缓存失效的问题
- memcache 缓存失效问题
- 程序后台Activity被回收后再次启动fragment失效问题
- PHP使用Memcache时模拟命名空间及缓存失效问题的解决
- android手机root后的安全问题 (四) 禁止开机启动和应用失效
- Shiro用ehcache缓存session,关于JSESSIONID失效报错问题
- cache缓存失效高并发读数据库的问题
- 缓存穿透、缓存并发、缓存失效问题以及解决方案
- xampp apache启动失效问题的解决方法