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.Net;
using System.IO;
using AboutIt;
using General_utility;
using DannyGeneral;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool Check_For_Closing;
OptionsFile setting_file;
string satellite_time;
string satellite_dir;
string settings_dir;
string settings_file;
string testing_file;
string bad_file_test_dir;
string temp_dir;
string path_exe;
string remote_image_on_server;
Int64 time_left;
Int64 numbers;
string file_array;
int Numbers_Timer;
About ab;
string temp_file;
string last_file;
string file_array_dl;
int file_indexs;
bool button_switch;
WebClient Client = new WebClient();
FolderBrowserDialog fb = new FolderBrowserDialog();
string sf;
public Form1()
{
SplashForm splash = new SplashForm();
splash.Show();
splash.Update();
satellite_time = DateTime.Now.ToString("yyyyMMddHHmm");
Numbers_Timer = 0;
testing_file="\\testing_file.jpg";
temp_file = "\\untitled.jpg";
settings_file = "\\settings.txt";
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
satellite_dir = path_exe + @"\satellite";
temp_dir = path_exe + @"\temp";
bad_file_test_dir = path_exe + @"\Bad_File-Test";
settings_dir = path_exe + @"\settings";
Directory.CreateDirectory(temp_dir);
Directory.CreateDirectory(bad_file_test_dir);
Directory.CreateDirectory(settings_dir);
Directory.CreateDirectory(satellite_dir);
setting_file = new OptionsFile(settings_dir + settings_file);
sf=setting_file.GetKey("Set-Directory");
setting_file.GetKey("Set_Time");
button_switch = true;
//a = 0;
ab = new About();
remote_image_on_server = "http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=";
Client.DownloadFile(remote_image_on_server, temp_dir + temp_file);
// string selectedFolder = fb.SelectedPath;
ProgressBar prgBar = new ProgressBar();
PictureBox pb = new PictureBox();
InitializeComponent();
}
/* private void timer4_Tick(object sender, EventArgs e)
{
int a;
a = 0;
a = a + 1;
pictureBox2.Load(@"d:\Weather_Michmoret.jpg");
if (a == 5)
{
pictureBox2.Enabled = false;
}
}*/
private void button1_Click(object sender, EventArgs e)
{
/* The stream str do nothing its good for reading files */
// Stream strm = Client.OpenRead("http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=");
// StreamReader sr = new StreamReader(strm);
if (sf == null)
{
MessageBox.Show("You haven't choose a directory to save the pictures into yet, please press Set Download Directory first, then press Start Download .");
textBox1.Clear();
button1.Enabled = true;
}
else
{
label4.Text = "Current Download Directory Is :" + sf;
if (textBox1.Text == "0" | textBox1.Text == "")
{
textBox1.Text = "";
MessageBox.Show("Please enter time in seconds to the Box first");
}
else
{
// File_Utility.File_Comparison(sf + "\\untitled.jpg", temp_file);
textBox1.Enabled = false;
button3.Enabled = true;
button4.Enabled = true;
// filesdownload();
button1.Enabled = false;
timer1.Enabled = true;
// Bitmap image1 = new Bitmap(@"d:\testfile\untitled.jpg", true);
// pictureBox1.Image = image1;
}
// image1 = new Bitmap(@"d:\testfile\untitled.jpg", true);
// pictureBox1.Image = image1;
// image1.Dispose();
// pictureBox1.Dispose();
}
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
/* private void button2_Click(object sender, EventArgs e)
{
// FileInfo MyFile = new FileInfo(@"d:\testfile\*.jpg");
// FileInfo MyFile = new FileInfo(sf+@"\");
// MyFile.Delete();
string temp ;
temp = Path.GetDirectoryName(Application.ExecutablePath);
Path.GetDirectoryName((Application.ExecutablePath)+@"d:\tree");
Directory.CreateDirectory(temp);
// string temp_path = Application.CommonAppDataPath;
}
*/
private void timer1_Tick(object sender, EventArgs e)
{
try
{
numbers = Convert.ToInt64(textBox1.Text);
if (numbers < 0)
{
timer1.Stop();
button1.Enabled = true;
textBox1.Enabled = true;
MessageBox.Show("Value cannot be below zero");
textBox1.Text = "";
}
else
{
Numbers_Timer = Numbers_Timer + 1;
if (Numbers_Timer == numbers)
{
filesdownload();
button1.Enabled = false;
Numbers_Timer = 0;
}
// String str = string.Format("Hours {0} Minutes {1} Seconds {2}", c, d, e);
time_left = numbers-Numbers_Timer;
label1.Text = "Time left for next download: " + time_left;
// y = numbers - 1;
// label1.Text = "Time left for next download: " +y;
/* if (a == 1)
{
a=numbers;
button1.Enabled = false;
} */
//String str = string.Format("Hours {0} Minutes {1} Seconds {2}", c, d, e);
// this.label1.Text = str;
/* String str = string.Format("{0} {1}", c, d);
this.textBox1.Text = str;
c = c - 1;
d = d - 1; */
}
}
catch (Exception err1)
{
timer1.Stop();
MessageBox.Show(err1.ToString());
textBox1.Clear();
button1.Enabled = true;
textBox1.Enabled = true;
}
}
private void filesdownload()
{
string Next_File;
bool file_compare;
int i;
bool bad_file;
Client.DownloadFile(remote_image_on_server, temp_dir + temp_file);
progressBar1.Maximum = 10;
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
progressBar1.Step = 10;
progressBar1.PerformStep();
button1.Enabled = true;
file_array_dl = Directory.GetFiles(sf, "radar*.jpg");
if (file_array_dl.Length == 0)
{
File.Copy(temp_dir + temp_file, sf + @"\radar001.jpg");
}
else
{
i = file_array_dl.Length;
last_file = sf + @"\radar" + i.ToString("D3") + ".jpg";
File.Copy(last_file, bad_file_test_dir + testing_file);
bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);
if (bad_file == true)
{
File.Delete(bad_file_test_dir+testing_file);
if (File.Exists(last_file))
{
file_compare = File_Utility.File_Comparison(temp_dir + temp_file, last_file);
if (file_compare == true)
{
return;
}
i = 0;
i = file_array_dl.Length + 1;
Next_File = sf + @"\radar" + i.ToString("D3") + ".jpg";
File.Copy(temp_dir + temp_file, Next_File);
progressBar1.Maximum = 10;
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
progressBar1.Step = 10;
progressBar1.PerformStep();
button1.Enabled = true;
}
/* else
{
Next_File = last_file;
}*/
}
else
{
File.Delete(last_file);
File.Delete(bad_file_test_dir+testing_file);
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
file_array = Directory.GetFiles(sf + @"\\");
file_indexs = file_array.Length - 1;
timer2.Enabled = true;
timer3.Enabled = false;
}
private void timer2_Tick(object sender, EventArgs e)
{
pictureBox1.Load(file_array);
file_indexs = file_indexs - 1;
if (file_indexs == 0)
{
file_indexs = file_array.Length - 1;
}
}
private void button4_Click(object sender, EventArgs e)
{
file_array = Directory.GetFiles(sf + @"\\");
file_indexs = 0;
file_indexs = file_array.Length - 1;
timer3.Enabled = true;
timer2.Enabled = false;
}
private void timer3_Tick(object sender, EventArgs e)
{
pictureBox1.Load(file_array);
file_indexs = file_indexs + 1;
if (file_indexs == file_array.Length)
{
file_indexs = 1;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (button_switch == true)
{
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.BringToFront();
button_switch = false;
}
else
{
pictureBox1.Dock = DockStyle.None;
button_switch = true;
}
}
private void button5_Click(object sender, EventArgs e)
{
timer2.Stop();
timer3.Stop();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
Activate();
}
private void ProgramAbout()
{
MessageBox.Show("Image Downloader Version 1.0");
}
private void Form1_Load(object sender, EventArgs e)
{
if (sf == null)
{
label4.Text = "Directory haven't chosen yet.";
}
else
{
label4.Text = "Current Download Directory Is :" + sf;
}
try
{
// ab.AboutProgram();
// ProgramAbout();
// MessageBox.Show("Testing Website if picture is available, press ok to continue . ");
pictureBox1.Load(temp_dir+temp_file);
//File.Delete(temp_dir + "\\untitled1.jpg");
}
catch
{
MessageBox.Show("There was a problem downloading the first picture, please try again later.");
textBox1.Text = "";
Check_For_Closing = true;
Close();
}
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
ProgramAbout();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void StopDownload_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
Numbers_Timer = 0;
timer1.Stop();
button1.Enabled = true;
textBox1.Enabled = true;
}
private void fgfdgfgfToolStripMenuItem_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
Activate();
}
private void button6_Click(object sender, EventArgs e)
{
this.fb.Description = "Select the directory that you want to use as the default.";
if (fb.ShowDialog() == DialogResult.OK)
{
string message = "Are you sure you want to download the images to this location ?";
string caption = "Operation cancelled";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result= MessageBox.Show(message,caption,buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
sf = fb.SelectedPath;
label4.Text = "Current Download Directory Is :" + sf;
setting_file.SetKey("Set-Directory", sf);
Logger.Write("Current Download Directory Is :" + sf);
}
}
}
/* private void store_files(string source_file , string destination_dir ,string destination_name)
{
} */
private bool Bad_File_Testing(string file_test)
{
try
{
// FileStream fs= File.OpenRead(bad_file_test_dir+testing_file);
Image jpg =Bitmap.FromFile(bad_file_test_dir + testing_file);
//Bitmap.FromFile(bad_file_test_dir + testing_file);
jpg.Dispose();
return true;
}
catch
{
MessageBox.Show("Last image was damaged and have been deleted");
return false;
}
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Load(temp_dir+temp_file);
}
private void label4_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Check_For_Closing == true)
{
}
else
{
if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
setting_file.SetKey("Set_Time",textBox1.Text);
}
/* private void gif_animated()
{
String imageFilePaths = new String { sf,"*.jpg" };
String outputFilePath = "c:\\test.gif";
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.Start(outputFilePath);
e.SetDelay(500);
//-1:no repeat,0:always repeat
e.SetRepeat(0);
for (int i = 0, count = imageFilePaths.Length; i < count; i++)
{
e.AddFrame(Image.FromFile(imageFilePaths));
}
e.Finish();
}*/
}
}
// To make double click again to bring the picturebox to original size .
// To make comparing between first picture everytime and the last
// picture and if they are the same so to delete one of them .
// To try to make backwords wnimation of the pictures !!!
// To try using picturebox and make animation of all pictures.
// And to try the file name idea .
// Make a play button and timer load a picture to this picturebox instance
/*
string my_file_name;
my_file_name = @"weather"+my_running_number;
and use my_file_name as the file ?
*/