日期:2014-05-18  浏览次数:21016 次

【C# GDI+绘制图片问题!!!!!!】
我用Graphics 绘制图片,刚开始为什么绘制不出来呢,就是没有显示,当循环多次后台显示图片。
求解答,完整代码如下:
C# code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace 多线程带参数
{
    class Form1 : Form
    {
        private PictureBox pic;
        Graphics g;
        private int Bit_X,Loca;
        Bitmap bit;

        public Form1()
        {
            Initialize();
        
        }

        private void Initialize()
        {
            pic = new PictureBox();
           pic.Size = new Size(32, 48);
            pic.Location = new Point(0,0);
       
            g = pic.CreateGraphics();

            this.SuspendLayout();
            this.Controls.Add(pic);

            this.Size = new Size(500, 280);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ResumeLayout();


            bit = new Bitmap("MAN.png");
            Loca=Bit_X = 0;
            Thread thread1 = new Thread(new ThreadStart(ManMove));
            thread1.IsBackground = true;
            thread1.Start();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void ManMove()
        {
            while (pic.Location.X < 500 - 32)
            {
                //bit.RotateFlip(RotateFlipType.Rotate90FlipY);
                Rectangle rect = new Rectangle(Bit_X, 96, 32, 48);
                Rectangle rectLoca=new Rectangle(0, 0, 32, 48);
                g.DrawImage(bit, rectLoca, rect, GraphicsUnit.Pixel);

                Thread.Sleep(5000);
             //   Bit_X += 32;
                if (Bit_X >= 192)
                    Bit_X = 0;
             //   g.Clear(pic.BackColor);
           //     pic.Location =new Point(Loca+=5,0);
            }
            MessageBox.Show("Exit");
        }
    }
}


------解决方案--------------------
CheckForIllegalCrossThreadCalls = false;这种方式并不好还会留下隐患
声明一个开始绘制和绘制完成的事件来通知主线程