2 teebenson00 teebenson00 于 2014.05.06 16:37 提问

C# 多线程读写删除文件并绘制在picture上

namespace PictureSD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool flag;
bool flag2;
private Object thisLock = new object();
ComputePic face;
Thread ThreadSamp;// 图片

    Bitmap bc1;
    Bitmap bc2;
    Graphics gc1;
    private string dir = @"C:\Users\Teefan\Desktop\C#聊天\NEWFACElog\Camera\Camera\bin\Debug\Images\7.jpg";
    private string dir2 = @"C:\Users\Teefan\Desktop\C#聊天\NEWFACElog\Camera\Camera\bin\Debug\Images";
    private void button1_Click(object sender, EventArgs e)
    {
          System.Drawing.Image img= System.Drawing.Image.FromFile(dir);// 读7号图片测试    
          Bitmap imgBit = new Bitmap(img);
          SaveBit(imgBit, dir2, 2, "jpg");//存2号
          imgBit.Dispose();

          if (face == null)
          {
              ThreadSamp = new Thread(createSamp); // 计算人脸
              ThreadSamp.IsBackground = true;
              ThreadSamp.Start();                  
          }
                   ThreadSamp.Join();
                    bc1.Dispose();
                    gc1.Dispose();
                     string img2 = dir2 + "/" + 2.ToString() + ".jpg";
                     File.Delete(img2);           
    }

    class ComputePic
    {
        public int weight;
        public int height;
        public ComputePic(int a,int b)
        {
            weight = a;
            height = b;          
        }

    }
    private void createSamp()
    {       
            flag2 = false;
            face = new ComputePic(pictureBox1.Width, pictureBox1.Height);           
            //假定在计算
            bc1 = new Bitmap(dir2 + "/" + "2.jpg");
             gc1 = Graphics.FromImage(bc1);
            RectangleF rect1 = new RectangleF(10, 20, face.weight++, face.height++);
            gc1.DrawImage(bc1, 0, 0, rect1, GraphicsUnit.Pixel);             
        pictureBox1.Invoke((MethodInvoker)delegate //操作其他控件的语句
           {
                pictureBox1.Image = bc1;
                pictureBox1.Refresh();
          });

        face = null;
    }
    private bool SaveBit(Bitmap bitmap, string path, int num, string kind)
    {
        string img = path + "/" + num.ToString() + "." + kind;
        bitmap.Save(img);
        return flag = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      //  button1_Click(button1, null);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        System.Timers.Timer t = new System.Timers.Timer(2000);   //实例化Timer类,设置间隔时间为10000毫秒;   
        t.Elapsed += new System.Timers.ElapsedEventHandler(button1_Click); //到达时间的时候执行事件;   
        t.AutoReset = true;   //设置是执行一次(false)还是一直执行(true);   
        t.Enabled = true;     
    }
}

}
是这样的,我做了一个界面想实现一个功能,相当于读7号图片,存时命名为2号,然后利用多线程计算2号图片,得到坐标,在绘制picture上,最后删除2号图片,再读7号,在2号计算、坐标、绘制。基本就是这个顺序不要乱,可是我琢磨几天了,要么gdi错误,要么bc1对象被别处引用,我不知道我这样的顺序应该怎么实现!或许很简单,可是我琢磨好久了,真心求教多线程而且能做到不会冲突的高手,我的代码只能基本实现图片删除,不能体现在picture画的样子刷新上!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!