【100分求解】图片移动起来闪呀闪呀闪。。。。

我把一张图片设置为背景图片,当角色移动是总是闪呀闪。。。。求解决!谢谢!

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace 多线程带参数
{
class Form1 : Form
{
private PictureBox pic;

private static int Bit_X,Bit_Y;
private int X,Y;
Bitmap bit;
int[] Map1;

public Form1()
{
InitMap();
Initialize();

Thread thread1 = new Thread(new ThreadStart(ManMove));
thread1.IsBackground = true;
thread1.Start();
}

private void Initialize()
{
pic = new PictureBox();
pic.Size = new Size(32, 48);
pic.Location = new Point(0,0);
pic.BackColor = Color.Transparent;



this.SuspendLayout();

Panel panel1 = new Panel();

panel1.Dock = DockStyle.Fill;
panel1.Controls.Add(pic);
this.Controls.Add(panel1);
//20 9
this.Size = new Size(640, 432);
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout();

panel1.BackgroundImage = Image.FromFile("MAP.png");

X=Y=Bit_X =Bit_Y= 0;

// this.BackColor = Color.Red;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
// CheckForIllegalCrossThreadCalls = false;

//panel1.BackColor = Color.Black;
this.pic.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();


}



private void InitMap() //初始化地图数据
{
Map1 = new int[20];
int[] tree = { 6,10,20};


}

private void DrawMap()//画图
{

}

public void Form1_KeyDown(object sender, KeyEventArgs e) //按钮事件
{
if (e.KeyCode == Keys.Up)
{
Y -= 1;
Bit_Y=144;

if (Y < 0)
Y = 0;
}
else if (e.KeyCode == Keys.Down)
{
Y += 1;
Bit_Y=0;

if (Y > this.ClientSize.Height-pic.Size.Height)
Y = this.ClientSize.Height - pic.Size.Height;
}
else if (e.KeyCode == Keys.Left)
{
X -= 1;
Bit_Y = 48;

if (X < 0)
X = 0;
}
else if (e.KeyCode == Keys.Right)
{
X += 1;
Bit_Y = 96;

if (X > this.ClientSize.Width-pic.Size.Width)
X = this.ClientSize.Width - pic.Size.Width;
}

}



private void ManMove() //人物移动
{
while (true)
{
bit = new Bitmap(32, 48);


Graphics g = Graphics.FromImage(bit);

//bit.RotateFlip(RotateFlipType.Rotate90FlipY);
Rectangle rect = new Rectangle(Bit_X, Bit_Y, 32, 48);
Rectangle rectLoca = new Rectangle(0, 0, 32, 48);

Bitmap m2 = new Bitmap("MAN2.png");

//;


g.DrawImage(m2, rectLoca, rect, GraphicsUnit.Pixel);
pic.Image = bit;
Thread.Sleep(200);

Bit_X += 32;
if (Bit_X >= 128)
Bit_X = 0;

MethodInvoker m = new MethodInvoker(Move_Loca);
this.BeginInvoke(m); //异步执行委托


}

}

private void Move_Loca() //位置变换
{
pic.Location = new Point(X, Y);
}
}
}
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
全局变量 2012-03-26
  • 打赏
  • 举报
回复
我也遇到类似的问题。 控件显示滚动重绘,都会闪动。

顶个
kong19 2012-03-26
  • 打赏
  • 举报
回复
代码上没看到什么问题,都是先画在image最后放到控件上显示。
双缓存如果不好用的话可能还是多线程画图有什么地方没有解决好
EnForGrass 2012-03-26
  • 打赏
  • 举报
回复
设置
利用winForm的一个属性,DoubleBuffer 设置 true,用双缓存处理Form界面内容加载,可以提高页面显示质量。

110,499

社区成员

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

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

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