using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace BitmapTrans
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Image = Bitmap.FromFile("3.jpg");
        }        private void button1_Click(object sender, EventArgs e)
        {
            Tool tool = new Tool(new Bitmap("3.jpg"));
            Thread trans = new Thread(new ThreadStart(tool.ConvertToGrayscale));
            trans.Start();
            trans.Join();
            pictureBox1.Image = tool.BitmapPic;
        }    }    class Tool
    {
        private Bitmap btpic;
        public Tool(Bitmap bt)
        {
            btpic = bt;
        }
        
        public void ConvertToGrayscale()
        {            Bitmap bm = new Bitmap(btpic.Width, btpic.Height);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = btpic.GetPixel(x, y);
                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                }
            }
            btpic = bm;
        }        public Bitmap BitmapPic
        {
            get
            {
                return btpic;
            }
        }
    }
}上面是代码,很简单,我只是想把一张图片从彩色的转成黑白的,但是我的转化方法比较浪费时间(尤其是对大图像),如果不用线程去做的话,那在转化的过程中前台Form这个线程就会死掉片刻,当然这是很正常的。在这里为了保证前台GUI线程的正常运行,所以我想用线程来转化图像。
但是现在就出现这样一个问题,大家可能看到我在启动线程后调用了Join方法,是为了等线程结束,我可以将转化过后的Bitmap对象显示出来,但是这样的等侍依然中止了当前Form的线程,使Form不能做任何响应,我现在想问一下有没有方法让Form这个前台线程不中止在这里,可以继续做自己的事,而当转化线程完成自己的事后,可以发了类似中断的事件告诉前台Form线程转化已经完成,可以来获取转化结果了!