如何实现图片平滑移动

deyter 2009-09-25 10:40:29
我现在用的是timer+label来实现的,但是就是感觉很抖,还有点闪烁的感觉
哪位能给个控件从A点平滑移动到B点的例子啊
谢谢
...全文
1012 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lmllouk 2010-05-14
  • 打赏
  • 举报
回复
现在才看到
biny101 2009-12-15
  • 打赏
  • 举报
回复
留名学习
c254392385 2009-11-29
  • 打赏
  • 举报
回复
如果在移动的过程中,图片还要缩小,能有什么好的方法让他平滑过渡吗
dylike 2009-09-25
  • 打赏
  • 举报
回复
直接画在屏幕上的话,会导致窗体移动时的不同步.还是画在原窗体上.
deyter 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 deyter 的回复:]
引用 4 楼 dylike 的回复:
不要使用控件而改用贴图的方式进行.
[/Quote]
你说的是直接画在屏幕是上吗?
wartim 2009-09-25
  • 打赏
  • 举报
回复
不要用time,label
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication73
{
public partial class Form1 : Form
{
Bitmap OrgBmp = null;
int PosX = 0;
Thread T = null;

delegate void DrawForm(Bitmap CacheBmp);

public Form1()
{
InitializeComponent();

OrgBmp = new Bitmap(this.Width, this.Height);
Graphics G = Graphics.FromImage(OrgBmp);
G.FillRectangle(new SolidBrush(this.BackColor), this.Bounds);
G.Dispose();

T = new Thread(new ThreadStart(Run));
T.IsBackground = true;
T.Start();

}

void Run()
{
Bitmap CacheBmp = null;
Graphics G = null;

try
{
while (true)
{
if (++PosX > this.Width)
PosX = 0;
CacheBmp = new Bitmap(OrgBmp);
G = Graphics.FromImage(CacheBmp);
G.DrawString("ABCDEFG12345", new Font("宋体", 10), Brushes.Green, new PointF(PosX, 0));
if (!this.IsDisposed && this.IsHandleCreated)
this.Invoke(new DrawForm(DoDrawForm), new Object[] { CacheBmp });
}
}
catch
{
}
finally
{
if (G != null)
G.Dispose();
if (CacheBmp != null)
{
CacheBmp.Dispose();
GC.Collect();
}
}
}

void DoDrawForm(Bitmap CacheBmp)
{
Graphics G = null;

try
{
G = this.CreateGraphics();
G.DrawImage(CacheBmp, new Point(0, 0));
}
finally
{
if (G != null)
G.Dispose();
GC.Collect();
}
}
}
}
deyter 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dylike 的回复:]
不要使用控件而改用贴图的方式进行.
[/Quote]
请问 贴图的方式如何做啊?
yuanhuiqiao 2009-09-25
  • 打赏
  • 举报
回复
没想到更好的方法。
个人体会是,lable显示效果不理想,有时刷新容易出问题,换个别的吧,比如panel啥的。闪烁的话DoubleBuffer之类的设置一下应该有改善。
dylike 2009-09-25
  • 打赏
  • 举报
回复
双缓存(注意同时双缓冲也要开启)
不要使用控件而改用贴图的方式进行.
panzhaojl 2009-09-25
  • 打赏
  • 举报
回复
我帮你搜了N久也没看到一个好的。 只好狂顶你咯。
zcandyly20211 2009-09-25
  • 打赏
  • 举报
回复
up
sedate952 2009-09-25
  • 打赏
  • 举报
回复
将timer的间距设大一点
deyter 2009-09-25
  • 打赏
  • 举报
回复
上面的两段代码是不是实现相同的功能,只是第一段直接在内存上创建Graphics对象,这才是用了双缓存啊?
用缓存处理图片,只是在窗口刷新一次的过程中,让所有图元同时显示到窗口上,但是怎么保证图片是平滑的移动呢?
deyter 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dylike 的回复:]
双缓存(注意同时双缓冲也要开启)
[/Quote]

BufferedGraphicsContext current = BufferedGraphicsManager.Current; //(1)
BufferedGraphics bg;
bg = current.Allocate(this.CreateGraphics(),this.DisplayRectangle); //(2)
Graphics g = bg.Graphics;//(3)
//随机 宽400 高400

System.Random rnd = new Random();
int x,y,w,h,r,i;
for (i = 0; i < 10000; i++)
{
x = rnd.Next(400);
y = rnd.Next(400);
r = rnd.Next(20);
w = rnd.Next(10);
h = rnd.Next(10);
g.DrawEllipse(Pens.Blue, x, y, w, h);
}
bg.Render();//(4)
//bg.Render(this.CreateGraphics());
bg.Dispose();//(5)
自己开辟一个缓冲区(如一个不显示的Bitmap对象),在其中绘制完成后,再一次性显示。
完整代码如下:
Bitmap bt = new Bitmap(400, 400);
Graphics bg = Graphics.FromImage(bt);
System.Random rnd = new Random();
int x, y, w, h, r, i;
for (i = 0; i < 10000; i++)
{
x = rnd.Next(400);
y = rnd.Next(400);
r = rnd.Next(20);
w = rnd.Next(10);
h = rnd.Next(10);
bg.DrawEllipse(Pens.Blue, x, y, w, h);

}
this.CreateGraphics().DrawImage(bt, new Point(0, 0));
本文来源于网络小筑 http://www.iwebtrados.com.cn/ , 原文地址:http://www.iwebtrados.com.cn/post/41.html

这段代码就是用双缓存画10000个小圆,能稍微说下这两节代码的作用吗?


110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧