我必须在图片框中显示图像。图像是档案的高分辨率扫描。由于这种高分辨率,我的平移缩放功能非常慢。为了解决这个问题,我减少了位图的宽度和长度,同时保持图像可读。在我的drawOriginalImage()代码中;变量"质量"因此,我减少了位图的大小。我就这样做了:
private void drawOriginalImage(int quality) {
try {
int x = originalImage.Width / quality,
y = originalImage.Height / quality;
pictureBox.Image = (Image)new Bitmap(originalImage, x, y);
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
}
catch (Exception ex) {
throw ex;
}
}
但是这个解决方案带来了另一个问题。这一步可能很长:
pictureBox.Image = (Image)new Bitmap(originalImage, x, y);
由于这种缓慢,我想使用Backgroundworker功能处理此步骤。现在我的代码看起来像这样:
private void drawOriginalImage(int quality) {
Cursor = Cursors.AppStarting;
backgroundWorker.RunWorkerAsync(new Point(
originalImage.Width / quality,
originalImage.Height / quality
));
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
Point p = (Point)e.Argument;
e.Result = new Bitmap(originalImage, p.X, p.Y);
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (e.Error != null) {
MessageBox.Show("Image too big.\nOriginal error:\n" + e.Error);
}
else {
pictureBox.Image = (Image)e.Result;
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
Cursor = Cursors.Default;
}
}
但它不起作用。我看到光标从" Cursors.AppStarting" to" Cursors.Default"因此任务可能已完成。但我的图片框中没有图片?怎么可能?我做错了什么?
当我调试它时,程序永远不会进入" backgroundWorker_RunWorkerCompleted"。怎么会这样?
答案 0 :(得分:1)
我找到了它。我通过添加以下内容正确地连接了所有事件:
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);