2

一些代码:

锁定 bitmapData 的函数:

    public static BitmapData LockData(Bitmap bitmap)
    {
        Rectangle bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        BitmapData bitmapData = bitmap.LockBits(bitmapRect, ImageLockMode.ReadWrite, bitmap.PixelFormat);

        return bitmapData;
    }

解锁 bitnapData 的函数:

    public static void UnlockData(Bitmap bitmap, BitmapData bitmapData)
    {
        bitmap.UnlockBits(bitmapData);
    }

改变图像亮度的功能:

    public static void Bright(int bright, BitmapData bitmapData, BackgroundWorker worker, DoWorkEventArgs e)
    {
        byte nPixelFormat = 0;
        if (bitmapData.PixelFormat == PixelFormat.Format24bppRgb) nPixelFormat = 3;
        else if (bitmapData.PixelFormat == PixelFormat.Format32bppArgb) nPixelFormat = 4;

        int max = bitmapData.Height - 1;
        int percent = 0;

        unsafe
        {
            for (int y = 0; y < bitmapData.Height; y++)
            {
                byte* pBmpRow = (byte*)(bitmapData.Scan0 + y * bitmapData.Stride);

                for (int x = 0; x < bitmapData.Width; x++)
                {
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                    int bVal = pBmpRow[nPixelFormat * x] + bright;
                    int gVal = pBmpRow[nPixelFormat * x + 1] + bright;
                    int rVal = pBmpRow[nPixelFormat * x + 2] + bright;



                    Clamp(ref bVal);
                    Clamp(ref gVal);
                    Clamp(ref rVal);


                    pBmpRow[nPixelFormat * x] = (byte)bVal;
                    pBmpRow[nPixelFormat * x + 1] = (byte)gVal;
                    pBmpRow[nPixelFormat * x + 2] = (byte)rVal;
                }
                //System.Threading.Thread.Sleep(5);
                int curPercent = (y * 100) / max;

                if (curPercent > percent)
                {
                    percent = curPercent;
                    worker.ReportProgress(percent);
                }
            }
        }
    }

在这个函数中,我正在检查 backgroundWorker 中止和更新进度。

现在我有 w trackbar 和 numericBox 我用来改变亮度。当 trackbar 或 numericBox 值改变时, backgroundWorker 启动。问题是,当 backgroundWorker 很忙并且我更改了 trackbarValue 时,backgroundWorker 应该停止并重新启动。现在在我的代码中它可以工作 liku 按钮。当工人忙时,价值改变停止工人,然后下一个价值改变开始它。

代码:

    private void OnNumericBoxValueChanged(object sender, EventArgs e)
    {


        m_trkBarBright.Value = (int)m_numericBright.Value;

        brightValue = m_trkBarBright.Value;
        if (backgroundWorker.IsBusy)
        {
            backgroundWorker.CancelAsync();
        }
        else
        {
            backgroundWorker.RunWorkerAsync();
        }


        m_btnOK.Focus();
    }


    private void WorkerDoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker)sender;


        Colors.CopyImgData(m_orgImage, m_tempImage);
        data = Colors.LockData(m_tempImage);

        Colors.Bright(brightValue, data,worker, e);

    }

    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            Colors.UnlockData(m_tempImage, data);
            MessageBox.Show(e.Error.Message);
            progressBar1.Value = 0;

        }
        else if (e.Cancelled)
        {
            Colors.UnlockData(m_tempImage, data);
            if(!exit)
            m_imgBox.UpdateImage(m_tempImage);
            MessageBox.Show("Canceled");
            progressBar1.Value = 0;

        }
        else
        {
            Colors.UnlockData(m_tempImage, data);
            if (!exit)
            m_imgBox.UpdateImage(m_tempImage);
            progressBar1.Value = 0;
            MessageBox.Show("Done");

        }
    }

    private void WorkerProgress(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

有人知道如何解决这个问题吗?

4

1 回答 1

0

执行此操作的正确方法是检查 abackgroundWorker.CancellationPending并设置e.Cancel = truefrom WorkerDoWork

CancelAsync不会停止工作人员,它只是向线程发送一条消息,说“我想停止”。

这是一个完整的示例

于 2013-04-28T09:31:15.653 回答