问题描述
我把一张图片设置为背景图片,当角色移动是总是闪呀闪。。。。求解决!谢谢!usingSystem;usingSystem.Windows.Forms;usingSystem.Drawing;usingSystem.Threading;namespace多线程带参数{classForm1:Form{privatePictureBoxpic;privatestaticintBit_X,Bit_Y;privateintX,Y;Bitmapbit;int[]Map1;publicForm1(){InitMap();Initialize();Threadthread1=newThread(newThreadStart(ManMove));thread1.IsBackground=true;thread1.Start();}privatevoidInitialize(){pic=newPictureBox();pic.Size=newSize(32,48);pic.Location=newPoint(0,0);pic.BackColor=Color.Transparent;this.SuspendLayout();Panelpanel1=newPanel();panel1.Dock=DockStyle.Fill;panel1.Controls.Add(pic);this.Controls.Add(panel1);//209this.Size=newSize(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+=newKeyEventHandler(Form1_KeyDown);//CheckForIllegalCrossThreadCalls=false;//panel1.BackColor=Color.Black;this.pic.ResumeLayout(false);((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();this.ResumeLayout(false);this.PerformLayout();}privatevoidInitMap()//初始化地图数据{Map1=newint[20];int[]tree={6,10,20};}privatevoidDrawMap()//画图{}publicvoidForm1_KeyDown(objectsender,KeyEventArgse)//按钮事件{if(e.KeyCode==Keys.Up){Y-=1;Bit_Y=144;if(Y<0)Y=0;}elseif(e.KeyCode==Keys.Down){Y+=1;Bit_Y=0;if(Y>this.ClientSize.Height-pic.Size.Height)Y=this.ClientSize.Height-pic.Size.Height;}elseif(e.KeyCode==Keys.Left){X-=1;Bit_Y=48;if(X<0)X=0;}elseif(e.KeyCode==Keys.Right){X+=1;Bit_Y=96;if(X>this.ClientSize.Width-pic.Size.Width)X=this.ClientSize.Width-pic.Size.Width;}}privatevoidManMove()//人物移动{while(true){bit=newBitmap(32,48);Graphicsg=Graphics.FromImage(bit);//bit.RotateFlip(RotateFlipType.Rotate90FlipY);Rectanglerect=newRectangle(Bit_X,Bit_Y,32,48);RectanglerectLoca=newRectangle(0,0,32,48);Bitmapm2=newBitmap("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;MethodInvokerm=newMethodInvoker(Move_Loca);this.BeginInvoke(m);//异步执行委托}}privatevoidMove_Loca()//位置变换{pic.Location=newPoint(X,Y);}}}
解决方案
解决方案二:
设置利用winForm的一个属性,DoubleBuffer设置true,用双缓存处理Form界面内容加载,可以提高页面显示质量。
解决方案三:
代码上没看到什么问题,都是先画在image最后放到控件上显示。双缓存如果不好用的话可能还是多线程画图有什么地方没有解决好
解决方案四:
我也遇到类似的问题。控件显示滚动重绘,都会闪动。顶个