EDN Admin
Well-known member
I checked on the hard disk the animated gif is created and i opened it in internet explorer and it worked.
So i dont understand why im getting this white with big x red.
I used breakpoint and didnt get any errors so far.
This is the class im doing the animated gif:
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using unfreez_wrapper;
using DannyGeneral;
using System.Threading;
namespace AnimationEditor
{
public partial class Animation_Preview : Form
{
bool makeGif;
Image gifImage;
DirectoryInfo tempdir;
private const int _blinkFrequency = 1000;
private const int _maxNumberOfBlinks = 5;
private int _blinkCount = 0;
FileInfo[] pngFileInfo;
FileInfo[] fi;
DirectoryInfo di;
private MemoryStream _memSt = null;
Image img;
int numberOfFiles;
string nameOfStartFile;
string nameOfEndFile;
List<string> Directories;
DirectoryInfo dir1;
string previewFileName;
string previewDirectory;
UnFreezWrapper unfreez;
String filesForanimation;
String tempBmpToGif;
string path_exe;
int mtpStart;
int mtpEnd;
public Animation_Preview()
{
InitializeComponent();
button1.Enabled = true;
button2.Enabled = true;
makeGif = true;
timer1.Interval = _blinkFrequency;
//radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
filesForanimation = @"dictureBoxToBitmap";
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempBmpToGif = Path.Combine(path_exe, "tempBmpToGifDirectory");
if (Directory.Exists(tempBmpToGif))
{
}
else
{
Directory.CreateDirectory(tempBmpToGif);
}
dir1 = new DirectoryInfo(tempBmpToGif);
pictureBox1.Image.Dispose();
if (gifImage != null)
{
gifImage.Dispose();
}
foreach (FileInfo file in dir1.GetFiles())
{
file.Delete();
}
Directories = new List<string>();
Directories.Add(filesForanimation);
Directories.Add(tempBmpToGif);
backgroundWorker1.RunWorkerAsync(Directories);
// mtpStart = Picturebox1_Fullscreen.mtp1Start;
// mtpEnd = Picturebox1_Fullscreen.mtp1End;
unfreez = new UnFreezWrapper();
previewDirectory = path_exe + "\" + "previewDirectory";
if (Directory.Exists(previewDirectory))
{
}
else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + "\" + "preview.gif";
// numberOfFiles = (mtpEnd - mtpStart)+1;
label2.Text = numberOfFiles.ToString();
di = new DirectoryInfo(filesForanimation);
fi = di.GetFiles("*.bmp");
numberOfFiles = fi.Length;
//nameOfStartFile = fi[mtpStart].Name.ToString();
//nameOfEndFile = fi[mtpEnd].Name.ToString();
// label6.Text = nameOfStartFile.ToString();
// label4.Text = nameOfEndFile.ToString();
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
List<string> Directories = (List<string>)e.Argument;
String filesForAnimation = Directories[0];
String TempBmpTogif = Directories[1];
tempdir = new DirectoryInfo(TempBmpTogif);
List<string> myGifList = new List<string>();
dir1 = new DirectoryInfo(filesForAnimation);
pngFileInfo = dir1.GetFiles("*.bmp");
int total = pngFileInfo.Length;
int Counter = 0;
int percentage = 0;
//for (int i = mtpStart; i < mtpEnd; i++)//for (int i = mtpStart; i< mtpEnd;i++)//foreach (FileInfo SingleFileInfo in pngFileInfo)
for (int i = 0 ; i<numberOfFiles;i++)
// *** To find and fix and work with the mtpStart and mtpEnd since in the for they are both 0 wich make everything not to work *** \
{
if (bgw.CancellationPending == true)
{
pictureBox1.Image.Dispose();
e.Cancel = true;
gifImage.Dispose();
File.Delete(previewFileName);
makeGif = false;
/*foreach (FileInfo file in tempdir.GetFiles())
{
try
{
File.SetAttributes(file.FullName, FileAttributes.Normal);
file.Delete();
}
catch (UnauthorizedAccessException err)
{
MessageBox.Show(err.ToString());
}
}*/
break;
}
else
{
try
{
String FileName = tempBmpToGif + "\" + Counter.ToString("D6") + ".Gif";
// loading png and convert it to gif
gifImage = Image.FromFile(pngFileInfo.FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
gifImage.Dispose();
Counter += 1;
// add the filename to the List
myGifList.Add(FileName);
// calculating percentage and report it
percentage = Counter * 100 / total;//(total / 100) * Counter;
bgw.ReportProgress(percentage);
}
catch (Exception ex)
{
// this is a bad idea inside a backgroundworker
MessageBox.Show("error >>>>>> " + ex);
}
}
}
numberOfFiles = myGifList.Count();
if (makeGif == false)
{
}
else
{
unfreez.MakeGIF(myGifList, previewFileName, 8, true);
}
bgw.ReportProgress(100);
// whatever this will do
//unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);
// setting the myGifList as the result of the method
e.Result = myGifList;
}
private void backGroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar2.Value = e.ProgressPercentage;
}
List<string> myGifList;
private void backGroundWorker1_Completed(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
label7.Visible = true;
label7.ForeColor = Color.Red;
label7.Text = "Operation Has Been Cancelled";
button2.Enabled = false;
}
else if (!(e.Error == null))
{
}
else
{
myGifList = (List<string>)e.Result;
pictureBoxImage(previewFileName);
timer1.Stop();
label7.Visible = true;
label7.ForeColor = Color.Green;
label7.Text = "Operation Have Been Completed";
button1.Enabled = false;
progressBar2.EndColor = Color.FromArgb(0, 211, 040);
}
}
public static bool IsDirectoryEmpty(DirectoryInfo directory)
{
FileInfo[] files = directory.GetFiles();
DirectoryInfo[] subdirs = directory.GetDirectories();
return (files.Length == 0 && subdirs.Length == 0);
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result1;
result1 = new DialogResult();
SaveFileDialog sd = new SaveFileDialog();
sd.Title = "Select a folder to save the animated gif to";
sd.InitialDirectory = "c:\";
sd.FileName = null;
sd.Filter = "Gif File|*.gif;*.jpg|Gif|*.gif";
sd.FilterIndex = 1;
sd.RestoreDirectory = true;
result1 = sd.ShowDialog();
string file1 = sd.FileName;
if (result1 == DialogResult.OK)
{
File.Move(previewFileName, file1);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
progressBar2.EndColor = Color.FromArgb(210, 0, 0);
label7.ForeColor = Color.Red;
label7.Text = "Operation Has Been Cancelled";
timer1.Stop();
backgroundWorker1.CancelAsync();
}
}
public void pictureBoxImage(string pbImage)
{
Image img2 = null;
try
{
using (img = Image.FromFile(pbImage))
{
//get the old image thats loaded from the _memSt memorystream
//and dispose it
Image i = this.pictureBox1.Image;
this.pictureBox1.Image = null;
if (i != null)
i.Dispose();
//grab the old stream
MemoryStream m = _memSt;
//save the new image to this stream
_memSt = new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
if (m != null)
m.Dispose();
//create our image to display
img2 = Image.FromStream(_memSt);
}
if (img2 != null)
pictureBox1.Image = img2;
//label2.Text = numberOfFiles.ToString();
//label6.Text = nameOfStartFile.ToString();
//label4.Text = nameOfEndFile.ToString();
//File.Delete(pbImage);
}
catch (Exception err)
{
Logger.Write("Animation Error >>> " + err);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label7.Visible = !this.label7.Visible;
_blinkCount++;
}
private void Animation_Radar_Preview_FormClosing(object sender, FormClosingEventArgs e)
{
if (progressBar2.EndColor == Color.FromArgb(210, 0, 0))
{
}
else
{
DialogResult dlg = MessageBox.Show("Are you sure you want to exit and cancel the operation ?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlg == DialogResult.Yes)
{
backgroundWorker1.CancelAsync();
if (gifImage != null)
{
gifImage.Dispose();
}
dir1 = new DirectoryInfo(tempBmpToGif);
foreach (FileInfo file in dir1.GetFiles())
{
file.Delete();
}
File.Delete(previewFileName);
//maybe more code
}
else if (dlg == DialogResult.No)
{
e.Cancel = true;
return;
}
}
//more code
//...
}
}
}
[/code]
The real creation of the animated gif in this class is this line number 175:
<pre class="prettyprint unfreez.MakeGIF(myGifList, previewFileName, 8, true);[/code]
And the animated gif is realy exist and created.In line 210 im calling the function that load the animated gif to the pictureBox:
<pre class="prettyprint pictureBoxImage(previewFileName);[/code]
And this is the pictureBoxImage function where i used breakpoint and it didnt show any errors:
<pre class="prettyprint public void pictureBoxImage(string pbImage)
{
Image img2 = null;
try
{
using (img = Image.FromFile(pbImage))
{
//get the old image thats loaded from the _memSt memorystream
//and dispose it
Image i = this.pictureBox1.Image;
this.pictureBox1.Image = null;
if (i != null)
i.Dispose();
//grab the old stream
MemoryStream m = _memSt;
//save the new image to this stream
_memSt = new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
if (m != null)
m.Dispose();
//create our image to display
img2 = Image.FromStream(_memSt);
}
if (img2 != null)
pictureBox1.Image = img2;
//label2.Text = numberOfFiles.ToString();
//label6.Text = nameOfStartFile.ToString();
//label4.Text = nameOfEndFile.ToString();
//File.Delete(pbImage);
}
catch (Exception err)
{
Logger.Write("Animation Error >>> " + err);
}
}[/code]
<br/>
Cant figure out why ? The red x and white background.
<hr class="sig danieli
View the full article
So i dont understand why im getting this white with big x red.
I used breakpoint and didnt get any errors so far.
This is the class im doing the animated gif:
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using unfreez_wrapper;
using DannyGeneral;
using System.Threading;
namespace AnimationEditor
{
public partial class Animation_Preview : Form
{
bool makeGif;
Image gifImage;
DirectoryInfo tempdir;
private const int _blinkFrequency = 1000;
private const int _maxNumberOfBlinks = 5;
private int _blinkCount = 0;
FileInfo[] pngFileInfo;
FileInfo[] fi;
DirectoryInfo di;
private MemoryStream _memSt = null;
Image img;
int numberOfFiles;
string nameOfStartFile;
string nameOfEndFile;
List<string> Directories;
DirectoryInfo dir1;
string previewFileName;
string previewDirectory;
UnFreezWrapper unfreez;
String filesForanimation;
String tempBmpToGif;
string path_exe;
int mtpStart;
int mtpEnd;
public Animation_Preview()
{
InitializeComponent();
button1.Enabled = true;
button2.Enabled = true;
makeGif = true;
timer1.Interval = _blinkFrequency;
//radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
filesForanimation = @"dictureBoxToBitmap";
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempBmpToGif = Path.Combine(path_exe, "tempBmpToGifDirectory");
if (Directory.Exists(tempBmpToGif))
{
}
else
{
Directory.CreateDirectory(tempBmpToGif);
}
dir1 = new DirectoryInfo(tempBmpToGif);
pictureBox1.Image.Dispose();
if (gifImage != null)
{
gifImage.Dispose();
}
foreach (FileInfo file in dir1.GetFiles())
{
file.Delete();
}
Directories = new List<string>();
Directories.Add(filesForanimation);
Directories.Add(tempBmpToGif);
backgroundWorker1.RunWorkerAsync(Directories);
// mtpStart = Picturebox1_Fullscreen.mtp1Start;
// mtpEnd = Picturebox1_Fullscreen.mtp1End;
unfreez = new UnFreezWrapper();
previewDirectory = path_exe + "\" + "previewDirectory";
if (Directory.Exists(previewDirectory))
{
}
else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + "\" + "preview.gif";
// numberOfFiles = (mtpEnd - mtpStart)+1;
label2.Text = numberOfFiles.ToString();
di = new DirectoryInfo(filesForanimation);
fi = di.GetFiles("*.bmp");
numberOfFiles = fi.Length;
//nameOfStartFile = fi[mtpStart].Name.ToString();
//nameOfEndFile = fi[mtpEnd].Name.ToString();
// label6.Text = nameOfStartFile.ToString();
// label4.Text = nameOfEndFile.ToString();
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
List<string> Directories = (List<string>)e.Argument;
String filesForAnimation = Directories[0];
String TempBmpTogif = Directories[1];
tempdir = new DirectoryInfo(TempBmpTogif);
List<string> myGifList = new List<string>();
dir1 = new DirectoryInfo(filesForAnimation);
pngFileInfo = dir1.GetFiles("*.bmp");
int total = pngFileInfo.Length;
int Counter = 0;
int percentage = 0;
//for (int i = mtpStart; i < mtpEnd; i++)//for (int i = mtpStart; i< mtpEnd;i++)//foreach (FileInfo SingleFileInfo in pngFileInfo)
for (int i = 0 ; i<numberOfFiles;i++)
// *** To find and fix and work with the mtpStart and mtpEnd since in the for they are both 0 wich make everything not to work *** \
{
if (bgw.CancellationPending == true)
{
pictureBox1.Image.Dispose();
e.Cancel = true;
gifImage.Dispose();
File.Delete(previewFileName);
makeGif = false;
/*foreach (FileInfo file in tempdir.GetFiles())
{
try
{
File.SetAttributes(file.FullName, FileAttributes.Normal);
file.Delete();
}
catch (UnauthorizedAccessException err)
{
MessageBox.Show(err.ToString());
}
}*/
break;
}
else
{
try
{
String FileName = tempBmpToGif + "\" + Counter.ToString("D6") + ".Gif";
// loading png and convert it to gif
gifImage = Image.FromFile(pngFileInfo.FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
gifImage.Dispose();
Counter += 1;
// add the filename to the List
myGifList.Add(FileName);
// calculating percentage and report it
percentage = Counter * 100 / total;//(total / 100) * Counter;
bgw.ReportProgress(percentage);
}
catch (Exception ex)
{
// this is a bad idea inside a backgroundworker
MessageBox.Show("error >>>>>> " + ex);
}
}
}
numberOfFiles = myGifList.Count();
if (makeGif == false)
{
}
else
{
unfreez.MakeGIF(myGifList, previewFileName, 8, true);
}
bgw.ReportProgress(100);
// whatever this will do
//unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);
// setting the myGifList as the result of the method
e.Result = myGifList;
}
private void backGroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar2.Value = e.ProgressPercentage;
}
List<string> myGifList;
private void backGroundWorker1_Completed(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
label7.Visible = true;
label7.ForeColor = Color.Red;
label7.Text = "Operation Has Been Cancelled";
button2.Enabled = false;
}
else if (!(e.Error == null))
{
}
else
{
myGifList = (List<string>)e.Result;
pictureBoxImage(previewFileName);
timer1.Stop();
label7.Visible = true;
label7.ForeColor = Color.Green;
label7.Text = "Operation Have Been Completed";
button1.Enabled = false;
progressBar2.EndColor = Color.FromArgb(0, 211, 040);
}
}
public static bool IsDirectoryEmpty(DirectoryInfo directory)
{
FileInfo[] files = directory.GetFiles();
DirectoryInfo[] subdirs = directory.GetDirectories();
return (files.Length == 0 && subdirs.Length == 0);
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result1;
result1 = new DialogResult();
SaveFileDialog sd = new SaveFileDialog();
sd.Title = "Select a folder to save the animated gif to";
sd.InitialDirectory = "c:\";
sd.FileName = null;
sd.Filter = "Gif File|*.gif;*.jpg|Gif|*.gif";
sd.FilterIndex = 1;
sd.RestoreDirectory = true;
result1 = sd.ShowDialog();
string file1 = sd.FileName;
if (result1 == DialogResult.OK)
{
File.Move(previewFileName, file1);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
progressBar2.EndColor = Color.FromArgb(210, 0, 0);
label7.ForeColor = Color.Red;
label7.Text = "Operation Has Been Cancelled";
timer1.Stop();
backgroundWorker1.CancelAsync();
}
}
public void pictureBoxImage(string pbImage)
{
Image img2 = null;
try
{
using (img = Image.FromFile(pbImage))
{
//get the old image thats loaded from the _memSt memorystream
//and dispose it
Image i = this.pictureBox1.Image;
this.pictureBox1.Image = null;
if (i != null)
i.Dispose();
//grab the old stream
MemoryStream m = _memSt;
//save the new image to this stream
_memSt = new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
if (m != null)
m.Dispose();
//create our image to display
img2 = Image.FromStream(_memSt);
}
if (img2 != null)
pictureBox1.Image = img2;
//label2.Text = numberOfFiles.ToString();
//label6.Text = nameOfStartFile.ToString();
//label4.Text = nameOfEndFile.ToString();
//File.Delete(pbImage);
}
catch (Exception err)
{
Logger.Write("Animation Error >>> " + err);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label7.Visible = !this.label7.Visible;
_blinkCount++;
}
private void Animation_Radar_Preview_FormClosing(object sender, FormClosingEventArgs e)
{
if (progressBar2.EndColor == Color.FromArgb(210, 0, 0))
{
}
else
{
DialogResult dlg = MessageBox.Show("Are you sure you want to exit and cancel the operation ?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlg == DialogResult.Yes)
{
backgroundWorker1.CancelAsync();
if (gifImage != null)
{
gifImage.Dispose();
}
dir1 = new DirectoryInfo(tempBmpToGif);
foreach (FileInfo file in dir1.GetFiles())
{
file.Delete();
}
File.Delete(previewFileName);
//maybe more code
}
else if (dlg == DialogResult.No)
{
e.Cancel = true;
return;
}
}
//more code
//...
}
}
}
[/code]
The real creation of the animated gif in this class is this line number 175:
<pre class="prettyprint unfreez.MakeGIF(myGifList, previewFileName, 8, true);[/code]
And the animated gif is realy exist and created.In line 210 im calling the function that load the animated gif to the pictureBox:
<pre class="prettyprint pictureBoxImage(previewFileName);[/code]
And this is the pictureBoxImage function where i used breakpoint and it didnt show any errors:
<pre class="prettyprint public void pictureBoxImage(string pbImage)
{
Image img2 = null;
try
{
using (img = Image.FromFile(pbImage))
{
//get the old image thats loaded from the _memSt memorystream
//and dispose it
Image i = this.pictureBox1.Image;
this.pictureBox1.Image = null;
if (i != null)
i.Dispose();
//grab the old stream
MemoryStream m = _memSt;
//save the new image to this stream
_memSt = new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
if (m != null)
m.Dispose();
//create our image to display
img2 = Image.FromStream(_memSt);
}
if (img2 != null)
pictureBox1.Image = img2;
//label2.Text = numberOfFiles.ToString();
//label6.Text = nameOfStartFile.ToString();
//label4.Text = nameOfEndFile.ToString();
//File.Delete(pbImage);
}
catch (Exception err)
{
Logger.Write("Animation Error >>> " + err);
}
}[/code]
<br/>
Cant figure out why ? The red x and white background.
<hr class="sig danieli
View the full article