簡體   English   中英

標簽c#backgroundWorker和trackBar

[英]c# backgroundWorker and trackBar

一些代碼:

鎖定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跟蹤欄和numericBox,它們用於更改亮度。 跟蹤欄或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;
    }

有人知道如何解決此問題嗎?

正確的方法是檢查backgroundWorker.CancellationPending並從WorkerDoWork設置e.Cancel = true

CancelAsync不會停止工作程序,它只是向線程發送一條消息,說“我想停止”。

這是一個完整的樣本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM