SaveAdd, multi-frame TIFF files and background workers

  • Thread starter Thread starter Franco.Cut
  • Start date Start date
F

Franco.Cut

Guest
Hello all!

Im trying to write a code with a background worker that generates a random image, plots it and saves it in a multi-layer Tiff file. I would like to precise Im still new to the C# world so this might be a trivial question.

The structure is like this:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//generates a random image and puts it into a //bitmap named bmp
e.Result = bmp;
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//this part adapted from the ms example
EncoderParameter ep;
EncoderParameters eps;

System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
ImageCodecInfo info=null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
if (ice.MimeType == "image/tiff")
info = ice;
eps = new EncoderParameters(1);
if (e.ProgressPercentage == 1)
{
//first frame

//bitmap_global is a class containing a public bmp
ep = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
eps.Param[0] = ep;
bitmap_global.bmp = (Bitmap)e.UserState;
bitmap_global.bmp.Save(bitmap_global.destination_folder, info, eps);
}

if (e.ProgressPercentage>1 & e.ProgressPercentage<10)
{
//consecutive frames
ep = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
eps.Param[0] = ep;
// ((Bitmap)e.UserState).SaveAdd(
bitmap_global.bmp.SaveAdd(((Bitmap)e.UserState), eps);
}

if (e.ProgressPercentage == 10)
{
//close up
ep = new EncoderParameter(enc, (long)EncoderValue.Flush);
eps.Param[0] = ep;
bitmap_global.bmp.SaveAdd(eps);
}




The code works only for 2 images and then returns exeption. I thought the reason was due to the changing bmp, thats why I placed it as a global variable but that wont help.

Is there anybody who knows where I am mistaken?

Thank you!

Continue reading...
 
Back
Top