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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP