teebenson00 2014-05-06 08:37
浏览 2436

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画的样子刷新上!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler