C#画图解决闪烁问题
Graphics g = Graphics.FromImage(bufferimage);
g.SmoothingMode = SmoothingMode.HighQuality; //高质量
g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
{
{
drawobject.Draw(g);
if (drawobject.TrackerState == config.Module.Core.TrackerState.Selected
&& this.CurrentOperator == Enum.Operator.Transfrom)//仅当编辑节点操作时显示图元热点
{
drawobject.DrawTracker(g);
}
}
}
{
tg.DrawImage(bufferimage, 0, 0); //把画布贴到画面上
}
BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = myBuffer.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
g.Clear(this.BackColor);
foreach (IShape drawobject in doc.drawObjectList)
{
if (rect.IntersectsWith(drawobject.Rect))
{
drawobject.Draw(g);
if (drawobject.TrackerState == config.Module.Core.TrackerState.Selected
&& this.CurrentOperator == Enum.Operator.Transfrom)//仅当编辑节点操作时显示图元热点
{
drawobject.DrawTracker(g);
}
}
}
g.Dispose();
myBuffer.Dispose();//释放资源
或者:
Bitmap bmp = new Bitmap(600, 600);
2、获取这块内存画布的Graphics引用:
Graphics g = Graphics.FromImage(bmp);
3、在这块内存画布上绘图:
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
4、将内存画布画到窗口中
this.CreateGraphics().DrawImage(bmp, 0, 0);
还有的方式
在构造函数中加如下代码
代码一:
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
代码二:
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
=======================================================================
使用 GDI+ 双缓冲 解决绘图闪烁问题
现在的问题是很多人不知道怎么怎么使用GDI+ 双缓冲
public partial class Form1 : Form
{
//记录矩形位置的变量
Point p = Point .Empty ;
Point location = new Point(0, 0);
int x = 0;
int y = 0;
public Form1()
{
InitializeComponent();
//采用双缓冲技术的控件必需的设置
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Black, x, y, 200, 200);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) return;
p = e.Location;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) return;
location.X += e.X - p.X;
location.Y += e.Y - p.Y;
p = Point.Empty;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (p == Point.Empty) return;
x = e.X - p.X + location.X;
y = e.Y - p.Y + location.Y;
this.Invalidate(true);//触发Paint事件
}
}
这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.
在这个例子上我犯的错误:
在 OnPaint(PaintEventArgs e)中,我使用下面两种方法获取graphics 对象
Graphics g = this.CreateGraphics();
Graphics g = Graphics.FromHwnd(this.Handle);
这都使双缓冲失效.
获得graphics 对象还有两种方法是
Graphics g = Graphics.FromImage(image); //后面将用此方法实现双缓冲
Graphics g = e.Graphics; //这是唯一好使的方法
上面是在Form窗口直接绘制图形,那么如何在控件上(比如Panel)利用双缓冲技术绘图呢?
在窗口窗创建一个Panel , 并修改一下代码
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Black, x, y, 200, 200);
}
运行后发现拖动在panel1上绘制的图形依然有闪烁,那么是不是应该这样设置
panel1.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//这样并不行,
因为SetStyle()在Panel类中不是public方法
使用从Panel类继承的MyPanel类 的构造函数中设置双缓冲
public class MyPanel:Panel
{
public MyPanel()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
}
}
不管怎么说这个方法的确好用,不过注意以后你使用的是MyPanel类,而不是Panel.
把自己定义MyPanel从工具栏里拖到窗口上和Panle一样使用.
注意:在控件上绘制就必须设置该控件的DoubleBuffer,而不是Form的DoubleBuffer.
在此之前我采用自己的方法实现双缓冲而不是控件自身的DoubleBuffer
先在内存里绘制图形,包括清除旧画面和绘制新画面,然后将内存的图形绘制到屏幕上
public void Draw(System.Windows.Forms.Panel _panel, float _x, float _y)
{
Graphics g = Graphics.FromHwnd(_panel.Handle);
try{
//在内存创建一块和panel一大小的区域
Bitmap bitmap = new Bitmap(_panel.ClientSize.Width, _panel.ClientSize.Height);
using (Graphics buffer = Graphics.FromImage(bitmap))
{
//buffer中绘图
buffer.Clear(_panel.BackColor); //用背景色填充画面
buffer.Transform = matrix;
buffer.DrawImage(source, _x/Scale , _y/Scale ); //绘制新画面
//屏幕绘图
g.DrawImage(bitmap, 0, 0); //将buffer绘制到屏幕上
}
}
finally
{
g.Dispose();
}
}
使用上面方法不需要任何设置.
总结一下
与绘图有关的ControlStyles
enum ControlStyles{
AllPainingInWmPaint, //将绘制阶段折叠入Paint事件
DoubleBuffer, //直到Paint返回,再显示绘制对象
UserPaint, //用于自身有着特别绘制的控件
Opaque, //忽略OnPaintBackground,Paint事件绘制整个区域
ResizeRedraw,//当调整控件大小时使整个工作区无效
SupportsTransparentBackColor,//模拟透明控件
...
}
1.在OnPaint(PaintEventArgs e)或Paint中 使用e获取graphics,我之所以费了很大周折就是因为在网上找到一篇实现双缓冲文章介绍,不要使用e获取graphics,而用this.CreateGraphics(),还有的文章介绍了奇怪的方法居然最终也好使.
2.在继承了Form和control 的控件上利用双缓冲绘制的时候,可以在控件的构造函数里设置双缓冲属性,而在窗口Form 里设置doublebuffer,只针对窗口的绘制起作用.
3.使用自己的方法,双缓冲的原理都是一样的.
示例:
private void DrawRectBackImage()
{
Graphics g = Graphics.FromHwnd(_currentChart.Handle);
try
{
//在内存创建一块和panel一大小的区域
Bitmap bitmap = new Bitmap(_currentChart.ClientSize.Width, _currentChart.ClientSize.Height);
using (Graphics buffer = Graphics.FromImage(bitmap))
{
buffer.Transform =new Matrix();
//要画的背景图片
buffer.DrawImage(curChartImage, _currentChart.ClientRectangle);
//背景图片上的内容
DrawFitAdjustRect(buffer);
//屏幕绘图
g.DrawImage(bitmap, 0, 0); //将buffer绘制到屏幕上
}
}
finally
{
g.Dispose();
}
}
C#画图解决闪烁问题的更多相关文章
- ddraw 视频下画图 不闪烁的方法
我们如果是在在RGB视频上画图(直线,矩形等),一般采用双缓冲区继续,使用内存MemoryDC,来实现画的图形在视频上显示不闪烁的功能,但是我们知道用RGB显示视频都是使用GDI进行渲染,这样很耗CP ...
- VC++ 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
之前写的一个VC应用程序,是程序启动时就直接出现在任务栏, 窗体不出现,等用户点击任务栏图标再出现窗口.和一些防火墙什么的软件类似. 这种效果实现并不是很困难的,硬是找不到最好的.为什么呢? 首先,在 ...
- cefsharp解决闪烁
CefSharp禁用GPU的命令行参数 其中,Major和Minor分别指代系统的主版本(大版本).次版本(小版本)版本号.其中指定了Windows7系统会禁用 GPU.,突发奇想,是否windows ...
- c# winfrom 更新控件时停止刷新,解决闪烁问题
static Dictionary<Control, bool> m_lstFreezeControl = new Dictionary<Control, bool>(); / ...
- C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】
http://blog.sina.com.cn/s/blog_402c071e0102x4rl.html 以下内容,对于想要使用C#实现PNG图片背景透明显示,同时动态显示时无闪烁问题的人来说, ...
- winform画图闪烁问题
问题:在winform程序的onpaint方法中画图, 连续画, 如鼠标移动时就要不断画图, 会闪烁. 解决方法:将要画图的部分放到一个自定义控件中, 自定义控件的onpaint方法里面画图, 然后再 ...
- vue解决加载闪烁问题
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- C# 中DataGridView和ListView闪烁问题的解决方法
C# 中DataGridView和ListView闪烁问题的解决方法 方法一首先定义类,将此类放在datagridview或ListView所在的窗体类外面,然后代码如下, <span styl ...
- 使用Three.js挖空安装门来解决重叠闪烁的问题
一.挖空原理说明 subtract 用墙面减去与门重叠的部分,产生一个新的对象,导入材质安装门即可 //参与减去几何体 //平行于x轴门 var meshH4Door = new ThreeBSP( ...
随机推荐
- Windows Phone中的图形渲染处理
前言 这篇文章主要介绍Windows Phone中XAML的渲染处理过程,你可以根据下文中所提到的内容来优化现有的XAML页面,来提高页面渲染的性能. XAML的渲染包括以下两个阶段: 1. 光 ...
- 帝国cms后台不停的登录成功
http://bbs.phome.net/showthread-13-305985-1.html 感谢各位的支持与帮助! e/data/adminlogin目录权限没有问题 问题在:计划任务中,刷新自 ...
- iOS 进阶 第七天(0403)
0403 QQ列表展开收起的原理 更改 numbersOfRowsInSection 的返回的数目,为0则收起group,不为零则为展开.先修改模型数据 刷新对应的表格 代码如下: 这个地方会出现一个 ...
- nodejs redis
0. install redis library for node npm install redis 1.node command example > var _redis = require ...
- iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述
本文档尝试用Video Toolbox进行H.265(HEVC)硬件编码,视频源为iPhone后置摄像头.去年做完硬解H.264,没做编码,技能上感觉有些缺失.正好刚才发现CMFormatDescri ...
- textview点击后selector的pressed无效果
原因: 需要配置 android:clickable="true" 这个跟开发环境有关,我之前用的android studio 就不需要这一项,默认可以点击. ********* ...
- C#中类型分析中的常见问题 Type - 转
http://www.cnblogs.com/yuanyuan/archive/2012/08/16/2642281.html 写代码的时候经常需要分析已有类型的信息例如:分析现有类型自动生成类, 或 ...
- 3139:[HNOI2013]比赛 - BZOJ
题目描述 Description 沫沫非常喜欢看足球赛,但因为沉迷于射箭游戏,错过了最近的一次足球联赛.此次联赛共N只队伍参加,比赛规则如下: (1) 每两支球队之间踢一场比赛. (2) 若平局,两支 ...
- Smarty插件简单开发
smarty的插件相当简单,你的网站经常会用到一些特定的Url或Controller或Action的路由地址的动态生成,通过smarty插件方式可以很轻松的实现. 在smarty/plugins目录下 ...
- Ext学习-基础组件介绍
1.目标 学习对象获取,组件基础,事件模型以及学习ExtJS中的基础组件的应用. 2.内容 1.对象获取 2.组件原理以及基础 3.事件模型 4.常用组件的介绍 3.学习步骤 1 ...