EDN Admin
Well-known member
I have this event where i select a file to load. When loading the file i want it to add all the .bmp files to the trackBar1 and pictureBox1 and then when im moving the scroll bar in the scroll event i want to move between the images.
<pre class="prettyprint private void loadHistogramFileToolStripMenuItem_Click(object sender, EventArgs e)
{
string file1 = "";
DialogResult result1; result1 = new DialogResult();
openFileDialog1.Title = "Select an histogram file to load";
openFileDialog1.InitialDirectory = "c:\";
openFileDialog1.FileName = null;
openFileDialog1.Filter = "Histogram File|*.dat|All|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
result1 = openFileDialog1.ShowDialog();
if (result1 == DialogResult.OK)
{
HistogramLoadedFile = true;
automaticDone = true;
button2.Enabled = true;
checkBox1.Enabled = true;
file1 = openFileDialog1.FileName;
directoryName = Path.GetDirectoryName(file1);
label1.Visible = true;
button1.Enabled = true;
label6.Text = "Loaded Histogram File: ";
label6.Visible = true;
label13.Visible = true;
label13.Text = file1;
if (File.Exists(file1))
{
BinaryReader binReader =
new BinaryReader(File.Open(file1, FileMode.Open));
try
{
//byte[] testArray = new byte[3];
int pos = 0;
int length = (int)binReader.BaseStream.Length;
binReader.BaseStream.Seek(0, SeekOrigin.Begin);
while (pos < length)
{
long[] l = new long[256];
for (int i = 0; i < 256; i++)
{
if (pos < length)
l = binReader.ReadInt64();
else
break;
pos += sizeof(Int64);
}
list_of_histograms.Add(l);
}
}
catch
{
}
finally
{
binReader.Close();
}
_indx = 0;
DirectoryInfo dir = new DirectoryInfo(directoryName);
if (_files == null)
_files = new List<FileInfo>();
fiAutomatic = dir.GetFiles("*.bmp");
_files.AddRange(fiAutomatic);
_files = _files.OrderBy(f => f.LastWriteTime).ToList();
button5.ForeColor = Color.Red;
button6.ForeColor = Color.Black;
button7.ForeColor = Color.Black;
button6.Enabled = true;
button6.Text = "Pause";
button7.Enabled = true;
Image iOLd = this.pictureBox1.Image;
Image img = Image.FromFile(_files[_indx].FullName);
myTrackPanelss1.trackBar1.Value = _indx;
label22.Text = _files[_indx].Name;
this.pictureBox1.Image = img;
if (iOLd != null)
iOLd.Dispose();
myTrackPanelss1.trackBar1.Enabled = true;
}
}
}[/code]
Then in the scroll event:
<pre class="prettyprint private void trackBar1_Scroll(object sender, EventArgs e)
{
trackbarCounter++;
if (manualDone == true)
{
myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = _fi.Length - 1;
setpicture(myTrackPanelss1.trackBar1.Value); this.pictureBox1.Refresh();
}
if (automaticDone == true || HistogramLoadedFile == true)
{
myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = fiAutomatic.Length - 1;
long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
HistogramGraphs1.DrawHistogram(tt);
long res = GetTopLumAmount(tt, 1000);
long max = GetHistogramMaximum(tt);
GetHistogramAverage(tt);
setpicture(myTrackPanelss1.trackBar1.Value); this.pictureBox1.Refresh();
}
}[/code]
In the scroll event the part is the one with the if (automaticDone == true || HistogramLoadedFile == true).
And the setpicture function is:
<pre class="prettyprint private void setpicture(int indx)
{
if (_fi == null)
{
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
button5.Enabled = false;
label5.Visible = true;
}
else
{
if (indx >= 0 && indx <= myTrackPanelss1.trackBar1.Maximum && _fi.Length > indx)
{
try
{
label19.ForeColor = Color.Red;
fileToolStripMenuItem.Enabled = true;
label19.Visible = false;
label20.Visible = false;
label14.Visible = true;
label15.Visible = true;
label8.Visible = true;
label9.Visible = true;
// try use a bool flag
myTrackPanelss1.trackBar1.Enabled = true;
using (FileStream fs = new FileStream(_fi[indx].FullName,FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
this.label8.Visible = true;
this.label9.Visible = true;
this.label9.Text = _fi[indx].Name;
Image img = null;
Bitmap bmp = null;
Image imgOLd = null;
try
{
// label5.Visible = false; // to make that the label5 "process done" in green will wait like 5 seconds and then will disapear ! using a timer !
img = Image.FromStream(fs);
bmp = new Bitmap(img);
imgOLd = this.pictureBox1.Image;
this.pictureBox1.Image = bmp;
if (imgOLd != null)
imgOLd.Dispose();
img.Dispose();
img = null;
}
catch
{
if (img != null)
img.Dispose();
if (bmp != null)
bmp.Dispose();
if (imgOLd != null)
imgOLd.Dispose();
}
}
}
catch
{
button1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label11.Visible = false;
label12.Visible = false;
checkBox2.Enabled = false;
label19.Visible = true;
label19.ForeColor = Color.Green;
label19.Text = "The Selected Directory Is Access Denied";
label20.Visible = true;
label20.ForeColor = Color.Green;
label20.Text = "Please Set A Different Directory";
fileToolStripMenuItem.Enabled = false;
label14.Visible = false;
label15.Visible = false;
label8.Visible = false;
label9.Visible = false;
myTrackPanelss1.trackBar1.Enabled = false;
timer2.Stop();
return;
}
}
else
{
Image imgOLd = this.pictureBox1.Image;
//this.pictureBox1.Image = null;
if (imgOLd != null)
{
imgOLd.Dispose();
imgOLd = null;
}
Application.DoEvents();
}
}
}[/code]
What i need is when i clicked ok and loaded the file it will add all the bmp files to the trackBar1 show them with the setpicture function and that i will be able to move between them with the scroll event.
Cant figure out how to do it. Can someone show me how ot do it with my code ?
Thanks.
<hr class="sig danieli
View the full article
<pre class="prettyprint private void loadHistogramFileToolStripMenuItem_Click(object sender, EventArgs e)
{
string file1 = "";
DialogResult result1; result1 = new DialogResult();
openFileDialog1.Title = "Select an histogram file to load";
openFileDialog1.InitialDirectory = "c:\";
openFileDialog1.FileName = null;
openFileDialog1.Filter = "Histogram File|*.dat|All|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
result1 = openFileDialog1.ShowDialog();
if (result1 == DialogResult.OK)
{
HistogramLoadedFile = true;
automaticDone = true;
button2.Enabled = true;
checkBox1.Enabled = true;
file1 = openFileDialog1.FileName;
directoryName = Path.GetDirectoryName(file1);
label1.Visible = true;
button1.Enabled = true;
label6.Text = "Loaded Histogram File: ";
label6.Visible = true;
label13.Visible = true;
label13.Text = file1;
if (File.Exists(file1))
{
BinaryReader binReader =
new BinaryReader(File.Open(file1, FileMode.Open));
try
{
//byte[] testArray = new byte[3];
int pos = 0;
int length = (int)binReader.BaseStream.Length;
binReader.BaseStream.Seek(0, SeekOrigin.Begin);
while (pos < length)
{
long[] l = new long[256];
for (int i = 0; i < 256; i++)
{
if (pos < length)
l = binReader.ReadInt64();
else
break;
pos += sizeof(Int64);
}
list_of_histograms.Add(l);
}
}
catch
{
}
finally
{
binReader.Close();
}
_indx = 0;
DirectoryInfo dir = new DirectoryInfo(directoryName);
if (_files == null)
_files = new List<FileInfo>();
fiAutomatic = dir.GetFiles("*.bmp");
_files.AddRange(fiAutomatic);
_files = _files.OrderBy(f => f.LastWriteTime).ToList();
button5.ForeColor = Color.Red;
button6.ForeColor = Color.Black;
button7.ForeColor = Color.Black;
button6.Enabled = true;
button6.Text = "Pause";
button7.Enabled = true;
Image iOLd = this.pictureBox1.Image;
Image img = Image.FromFile(_files[_indx].FullName);
myTrackPanelss1.trackBar1.Value = _indx;
label22.Text = _files[_indx].Name;
this.pictureBox1.Image = img;
if (iOLd != null)
iOLd.Dispose();
myTrackPanelss1.trackBar1.Enabled = true;
}
}
}[/code]
Then in the scroll event:
<pre class="prettyprint private void trackBar1_Scroll(object sender, EventArgs e)
{
trackbarCounter++;
if (manualDone == true)
{
myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = _fi.Length - 1;
setpicture(myTrackPanelss1.trackBar1.Value); this.pictureBox1.Refresh();
}
if (automaticDone == true || HistogramLoadedFile == true)
{
myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = fiAutomatic.Length - 1;
long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
HistogramGraphs1.DrawHistogram(tt);
long res = GetTopLumAmount(tt, 1000);
long max = GetHistogramMaximum(tt);
GetHistogramAverage(tt);
setpicture(myTrackPanelss1.trackBar1.Value); this.pictureBox1.Refresh();
}
}[/code]
In the scroll event the part is the one with the if (automaticDone == true || HistogramLoadedFile == true).
And the setpicture function is:
<pre class="prettyprint private void setpicture(int indx)
{
if (_fi == null)
{
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
button5.Enabled = false;
label5.Visible = true;
}
else
{
if (indx >= 0 && indx <= myTrackPanelss1.trackBar1.Maximum && _fi.Length > indx)
{
try
{
label19.ForeColor = Color.Red;
fileToolStripMenuItem.Enabled = true;
label19.Visible = false;
label20.Visible = false;
label14.Visible = true;
label15.Visible = true;
label8.Visible = true;
label9.Visible = true;
// try use a bool flag
myTrackPanelss1.trackBar1.Enabled = true;
using (FileStream fs = new FileStream(_fi[indx].FullName,FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
this.label8.Visible = true;
this.label9.Visible = true;
this.label9.Text = _fi[indx].Name;
Image img = null;
Bitmap bmp = null;
Image imgOLd = null;
try
{
// label5.Visible = false; // to make that the label5 "process done" in green will wait like 5 seconds and then will disapear ! using a timer !
img = Image.FromStream(fs);
bmp = new Bitmap(img);
imgOLd = this.pictureBox1.Image;
this.pictureBox1.Image = bmp;
if (imgOLd != null)
imgOLd.Dispose();
img.Dispose();
img = null;
}
catch
{
if (img != null)
img.Dispose();
if (bmp != null)
bmp.Dispose();
if (imgOLd != null)
imgOLd.Dispose();
}
}
}
catch
{
button1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label11.Visible = false;
label12.Visible = false;
checkBox2.Enabled = false;
label19.Visible = true;
label19.ForeColor = Color.Green;
label19.Text = "The Selected Directory Is Access Denied";
label20.Visible = true;
label20.ForeColor = Color.Green;
label20.Text = "Please Set A Different Directory";
fileToolStripMenuItem.Enabled = false;
label14.Visible = false;
label15.Visible = false;
label8.Visible = false;
label9.Visible = false;
myTrackPanelss1.trackBar1.Enabled = false;
timer2.Stop();
return;
}
}
else
{
Image imgOLd = this.pictureBox1.Image;
//this.pictureBox1.Image = null;
if (imgOLd != null)
{
imgOLd.Dispose();
imgOLd = null;
}
Application.DoEvents();
}
}
}[/code]
What i need is when i clicked ok and loaded the file it will add all the bmp files to the trackBar1 show them with the setpicture function and that i will be able to move between them with the scroll event.
Cant figure out how to do it. Can someone show me how ot do it with my code ?
Thanks.
<hr class="sig danieli
View the full article