Reputation: 161
I have to display images in a picturebox. The images are high resolution scans of archives. Because of this high resolution my panning en zoom features are very slow. To solve this problem I reduced the bitmap width and length while maintaining the images readable. In my code in drawOriginalImage(); the variable "quality" is thus the factor which I reduce the size of the bitmap. This is how I did it:
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;
}
}
But this solution brings another problem. This step can be very long:
pictureBox.Image = (Image)new Bitmap(originalImage, x, y);
Because of this slowness, I wanted to process this step with the Backgroundworker feature. Now my code looks like this:
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;
}
}
But it doesn't work. I see the cursor changing from "Cursors.AppStarting" to "Cursors.Default" and thus the task is probably completed. But there is no image in my picturebox? How can that be? What am I doing wrong?
When I debug it, the program never goes in "backgroundWorker_RunWorkerCompleted". How can it be?
Upvotes: 0
Views: 1492
Reputation: 161
I have found it. I had hook up all my events correctly by adding this:
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
Upvotes: 1