gpt4 book ai didi

c# backgroundWorker 和 trackBar

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:12 25 4
gpt4 key购买 nike

部分代码:

锁定位图数据的函数:

    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 按钮。当 worker 忙碌时,值更改会停止 worker,然后下一次值更改会启动它。

代码:

    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 不会停止 worker,它只是向线程发送一条消息说“我想停止”。

这是一个full sample .

关于c# backgroundWorker 和 trackBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com