Im trying to use backgroundworker but getting error: ExternalException:

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
A generic error occurred in GDI+
The full exception is:

System.Runtime.InteropServices.ExternalException was unhandled by user code<br/>
Message=A generic error occurred in GDI+.<br/>
Source=System.Drawing<br/>
ErrorCode=-2147467259<br/>
StackTrace:<br/>
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)<br/>
at System.Drawing.Image.Save(String filename, ImageFormat format)<br/>
at mws.Animation_Radar_Preview.backGroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:C-SharpDownload FileDownloading-File-Project-Version-012Downloading FileAnimation_Radar_Preview.cs:line 161<br/>
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)<br/>
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)<br/>
InnerException:



The excpetion is on the line: for (int x = 0; x < myList.Count(); x++) the x++ is painted with green.

This is the the full code of the form wich i show() when i click a button from another form.


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using DannyGeneral;
<span style="color:Blue; using unfreez_wrapper;


<span style="color:Blue; namespace mws
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Animation_Radar_Preview : Form
{
<span style="color:Blue; int mtpStart;
<span style="color:Blue; int mtpEnd;
Picturebox1_Fullscreen pb1;
<span style="color:Blue; string radar_images_download_directory;
<span style="color:Blue; string tempRadarPngToGifDirectory;
<span style="color:Blue; int numberOfFiles;
UnFreezWrapper unfreez;
<span style="color:Blue; string path_exe;
List<<span style="color:Blue; string> myList;
<span style="color:Blue; string previewDirectory;
<span style="color:Blue; int animatedGifSpeed;
<span style="color:Blue; bool loop;
<span style="color:Blue; string nameOfStartFile;
<span style="color:Blue; string nameOfEndFile;
<span style="color:Blue; string previewFileName;
BackgroundWorker backGroundWorker1;
Image img;
<span style="color:Blue; private MemoryStream _memSt = <span style="color:Blue; null;
<span style="color:Blue; public Animation_Radar_Preview()
{
InitializeComponent();
mtpStart = Picturebox1_Fullscreen.mtp1Start;
mtpEnd = Picturebox1_Fullscreen.mtp1End;
animatedGifSpeed = Picturebox1_Fullscreen.animatedSpeed;
loop = Picturebox1_Fullscreen.looping;
pb1 = <span style="color:Blue; new Picturebox1_Fullscreen();
radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempRadarPngToGifDirectory = path_exe + <span style="color:#A31515; "\" + "tempRadarPngToGifDirectory";
<span style="color:Blue; if (Directory.Exists(tempRadarPngToGifDirectory))
{
}
<span style="color:Blue; else
{
Directory.CreateDirectory(tempRadarPngToGifDirectory);
}
previewDirectory = path_exe + <span style="color:#A31515; "\" + "previewDirectory";
<span style="color:Blue; if (Directory.Exists(previewDirectory))
{
}
<span style="color:Blue; else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + <span style="color:#A31515; "\" + "preview.gif";
loop = <span style="color:Blue; false;
animatedGifSpeed = 0;
unfreez = <span style="color:Blue; new UnFreezWrapper();
backGroundWorker1 = <span style="color:Blue; new BackgroundWorker();
backGroundWorker1.WorkerSupportsCancellation = <span style="color:Blue; true;
<span style="color:Blue; this.backGroundWorker1.WorkerReportsProgress = <span style="color:Blue; true;
backGroundWorker1.ProgressChanged += <span style="color:Blue; new ProgressChangedEventHandler(backGroundWorker1_ProgressChanged);
backGroundWorker1.DoWork += <span style="color:Blue; new DoWorkEventHandler(backGroundWorker1_DoWork);
backGroundWorker1.RunWorkerCompleted += <span style="color:Blue; new RunWorkerCompletedEventHandler(backGroundWorker1_RunWorkerCompleted);
backGroundWorker1.RunWorkerAsync();
progressBar1.Value = 0;
}

<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
DialogResult result1;
result1 = <span style="color:Blue; new DialogResult();
SaveFileDialog sd = <span style="color:Blue; new SaveFileDialog();
sd.Title = <span style="color:#A31515; "Select a folder to save the animated gif to";
sd.InitialDirectory = "c:\";
sd.FileName = <span style="color:Blue; null;
sd.Filter = <span style="color:#A31515; "Gif File|*.gif;*.jpg|Gif|*.gif";
sd.FilterIndex = 1;
sd.RestoreDirectory = <span style="color:Blue; true;
result1 = sd.ShowDialog();
<span style="color:Blue; string file1 = sd.FileName;
<span style="color:Blue; if (result1 == DialogResult.OK)
{
File.Move(previewFileName, file1);
}
}

<span style="color:Blue; public <span style="color:Blue; void pictureBoxImage(<span style="color:Blue; string pbImage)
{
Image img2 = <span style="color:Blue; null;
<span style="color:Blue; try
{
<span style="color:Blue; using (img = Image.FromFile(pbImage))
{
<span style="color:Green; //get the old image thats loaded from the _memSt memorystream
<span style="color:Green; //and dispose it
Image i = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; this.pictureBox1.Image = <span style="color:Blue; null;

<span style="color:Blue; if (i != <span style="color:Blue; null)
i.Dispose();

<span style="color:Green; //grab the old stream
MemoryStream m = _memSt;

<span style="color:Green; //save the new image to this stream
_memSt = <span style="color:Blue; new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);

<span style="color:Blue; if (m != <span style="color:Blue; null)
m.Dispose();

<span style="color:Green; //create our image to display
img2 = Image.FromStream(_memSt);
}

<span style="color:Blue; if (img2 != <span style="color:Blue; null)
pictureBox1.Image = img2;
label2.Text = numberOfFiles.ToString();
label6.Text = nameOfStartFile.ToString();
label4.Text = nameOfEndFile.ToString();
<span style="color:Green; //File.Delete(pbImage);
}
<span style="color:Blue; catch(Exception err)
{
Logger.Write(<span style="color:#A31515; "Animation Error >>> " + err);
}
}

<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_ProgressChanged(<span style="color:Blue; object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_DoWork(<span style="color:Blue; object sender, DoWorkEventArgs e)
{
List<<span style="color:Blue; string> myGifList;
Image gifImages;
<span style="color:Green; //button1.Enabled = false;
Animation_Radar_Preview ap = <span style="color:Blue; new Animation_Radar_Preview();
<span style="color:Green; //ap.FormClosing += new FormClosingEventHandler(ap_FormClosing);
FileInfo[] fi;
DirectoryInfo dir1 = <span style="color:Blue; new DirectoryInfo(radar_images_download_directory);
fi = dir1.GetFiles(<span style="color:#A31515; "*.png");
myList = <span style="color:Blue; new List<<span style="color:Blue; string>();
myGifList = <span style="color:Blue; new List<<span style="color:Blue; string>();
<span style="color:Blue; for (<span style="color:Blue; int i = mtpStart; i < mtpEnd; i++)
{
myList.Add(fi.FullName);
}
<span style="color:Blue; for (<span style="color:Blue; int x = 0; x < myList.Count(); x++)
{
gifImages = Image.FromFile(myList[x]);
gifImages.Save(tempRadarPngToGifDirectory + <span style="color:#A31515; "\" + x.ToString("D6<span style="color:#A31515; ") + ".Gif", System.Drawing.Imaging.ImageFormat.Gif);
}
myGifList = <span style="color:Blue; new List<<span style="color:Blue; string>();
dir1 = <span style="color:Blue; new DirectoryInfo(tempRadarPngToGifDirectory);
fi = dir1.GetFiles(<span style="color:#A31515; "*.gif");
nameOfStartFile = fi[0].Name;
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < fi.Length; i++)
{
myGifList.Add(fi.FullName);
backGroundWorker1.ReportProgress(i);
nameOfEndFile = fi.Name.Length.ToString();
}
numberOfFiles = myGifList.Count();
unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);
pictureBoxImage(previewFileName);
}

<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_RunWorkerCompleted(<span style="color:Blue; object sender, RunWorkerCompletedEventArgs e)
{
}


}
}

[/code]
<br/>
What i want is that when i click the button in the other form so in this form ill see the progressBar1 progress when its doing all the work.

But im getting this exception.

Thanks. <hr class="sig danieli

View the full article
 
Back
Top