EDN Admin
Well-known member
This is the code in scroll event:
<pre class="prettyprint void trackBar1_Scroll(object sender, EventArgs e)
{
myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = counter - 1;//list_of_histograms.Count-1;
long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
histogramControl1.DrawHistogram(tt);
long res = GetTopLumAmount(tt, 1000);
long max = GetHistogramMaximum(tt);
if (res > -1)
label24.Text = (res / 1000.0).ToString();
setpicture(myTrackPanelss1.trackBar1.Value);
this.pictureBox1.Refresh();
}[/code]
And for some reason i used a breakpoint for some reason the scroll event is called twice in a row so if in GetHistogramMaximum function im using messagebox.Show wich i do its showing it twice in a row one after one instead showing it once only.
I used a breakpoint and saw its entering the scroll event twice in a row cant figure out why.
I tried now again with a breakpoint and i did step by step each function and line in the scroll event.
Right after its doing the this.PictureBox1.Refresh(); its doing right away everything in the scroll event again it dosent even leave the event just doing it right away again.
The first function its getting in in the scroll event is DrawHistogram wich is in histogramControl.cs its a userControl:
<pre class="prettyprint public void DrawHistogram(long[] Values)
{
myValues = new long[Values.Length];
Values.CopyTo(myValues, 0);
myIsDrawing = true;
myMaxValue = getMaxim(myValues);
ComputeXYUnitValues();
this.Refresh();
}[/code]
<br/>
Then in the DrawHistogram its using getMaxim function wich is also in the histogramControl.cs:
<pre class="prettyprint private long getMaxim(long[] Vals)
{
if (myIsDrawing)
{
long max = 0;
for (int i = 0; i < Vals.Length; i++)
{
if (Vals > max)
max = Vals;
}
return max;
}
return 1;
}[/code]
<br/>
Then the DrawHistogram is calling and using the function also in the histogramControl.cs:
<pre class="prettyprint private void ComputeXYUnitValues()
{
myYUnit = (float)(this.Height - (2 * myOffset)) / myMaxValue;
myXUnit = (float)(this.Width - (2 * myOffset)) / (myValues.Length - 1);
}[/code]
Then its jumping to the histogramControl_Paint event also in the histogramControl.cs:
<pre class="prettyprint private void histogramControl_Paint(object sender, PaintEventArgs e)
{
if (myIsDrawing)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(new SolidBrush(myColor), myXUnit);
//The width of the pen is given by the XUnit for the control.
for (int i = 0; i < myValues.Length; i++)
{
//We draw each line
g.DrawLine(myPen,
new PointF(myOffset + (i * myXUnit), this.Height - myOffset),
new PointF(myOffset + (i * myXUnit), this.Height - myOffset - myValues * myYUnit));
//We plot the coresponding index for the maximum value.
/* if (myValues == myMaxValue)
{
SizeF mySize = g.MeasureString(i.ToString(), myFont);
g.DrawString(i.ToString(), myFont, new SolidBrush(myColor),
new PointF(myOffset + (i * myXUnit) - (mySize.Width / 2), this.Height - myFont.Height),
System.Drawing.StringFormat.GenericDefault);
}*/
}
//We draw the indexes for 0 and for the length of the array beeing plotted
g.DrawString("0", myFont, new SolidBrush(myColor), new PointF(myOffset, this.Height - myFont.Height), System.Drawing.StringFormat.GenericDefault);
g.DrawString((myValues.Length - 1).ToString(), myFont,
new SolidBrush(myColor),
new PointF(myOffset + (myValues.Length * myXUnit) - g.MeasureString((myValues.Length - 1).ToString(), myFont).Width,
this.Height - myFont.Height),
System.Drawing.StringFormat.GenericDefault);
//We draw a rectangle surrounding the control.
g.DrawRectangle(new System.Drawing.Pen(new SolidBrush(Color.Black), 1), 0, 0, this.Width - 1, this.Height - 1);
}
}
long myMaxValue;
private long[] myValues;
private bool myIsDrawing;
private float myYUnit; //this gives the vertical unit used to scale our values
private float myXUnit; //this gives the horizontal unit used to scale our values
private int myOffset = 20; //the offset, in pixels, from the control margins.
private Color myColor = Color.Black;
private Font myFont = new Font("Tahoma", 10);
[Category("Histogram Options")]
[Description("The distance from the margins for the histogram")]
public int Offset
{
set
{
if (value > 0)
myOffset = value;
}
get
{
return myOffset;
}
}
[Category("Histogram Options")]
[Description("The color used within the control")]
public Color DisplayColor
{
set
{
myColor = value;
}
get
{
return myColor;
}
}[/code]
Then its back to the scroll event in the form1 amd calling using the function:
<pre class="prettyprint public long GetTopLumAmount(long[] histogram, int amount)
{
if (histogram.Sum() < amount)
return -1;
long result = 0;
long counted = 0;
for (int i = histogram.Length - 1; i >= 0; i--)
{
if (counted < amount)
{
long current = Math.Min(histogram, amount - counted);
counted += current;
result += current * i;
}
else
break;
}
return result;
}[/code]
And then the scroll event is calling using the function:
<pre class="prettyprint public long GetHistogramMaximum(long[] histogram)
{
long result = 0;
for (int i = 0; i < histogram.Length; i++)
{
if (histogram > result)
{
// MessageBox.Show(string.Format("{0} is greater than {1}", histogram, result));
label28.Text = "Thw maximum value is " + histogram;
label27.Text = "The Maximum Value Is Present At Index " + i;
result = histogram;
}
}
return result;
}[/code]
Then its doing the setpicture function the code of the setpicture and in the end its doing this.pictureBox1.Refresh();
This is the setpicture function code:
<pre class="prettyprint private void setpicture(int indx)
{
if (_fi == null)
{
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
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))
{
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;
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]
<br/>
And then its doing everything in the scroll event again it dosent quit it or something just doing it all over again right away.
Maybe something in the setpicture function ? Or the pictureBox1.Refresh doing something ?
Thanks.
<
danieli
<br/>
View the full article
<pre class="prettyprint void trackBar1_Scroll(object sender, EventArgs e)
{
myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = counter - 1;//list_of_histograms.Count-1;
long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
histogramControl1.DrawHistogram(tt);
long res = GetTopLumAmount(tt, 1000);
long max = GetHistogramMaximum(tt);
if (res > -1)
label24.Text = (res / 1000.0).ToString();
setpicture(myTrackPanelss1.trackBar1.Value);
this.pictureBox1.Refresh();
}[/code]
And for some reason i used a breakpoint for some reason the scroll event is called twice in a row so if in GetHistogramMaximum function im using messagebox.Show wich i do its showing it twice in a row one after one instead showing it once only.
I used a breakpoint and saw its entering the scroll event twice in a row cant figure out why.
I tried now again with a breakpoint and i did step by step each function and line in the scroll event.
Right after its doing the this.PictureBox1.Refresh(); its doing right away everything in the scroll event again it dosent even leave the event just doing it right away again.
The first function its getting in in the scroll event is DrawHistogram wich is in histogramControl.cs its a userControl:
<pre class="prettyprint public void DrawHistogram(long[] Values)
{
myValues = new long[Values.Length];
Values.CopyTo(myValues, 0);
myIsDrawing = true;
myMaxValue = getMaxim(myValues);
ComputeXYUnitValues();
this.Refresh();
}[/code]
<br/>
Then in the DrawHistogram its using getMaxim function wich is also in the histogramControl.cs:
<pre class="prettyprint private long getMaxim(long[] Vals)
{
if (myIsDrawing)
{
long max = 0;
for (int i = 0; i < Vals.Length; i++)
{
if (Vals > max)
max = Vals;
}
return max;
}
return 1;
}[/code]
<br/>
Then the DrawHistogram is calling and using the function also in the histogramControl.cs:
<pre class="prettyprint private void ComputeXYUnitValues()
{
myYUnit = (float)(this.Height - (2 * myOffset)) / myMaxValue;
myXUnit = (float)(this.Width - (2 * myOffset)) / (myValues.Length - 1);
}[/code]
Then its jumping to the histogramControl_Paint event also in the histogramControl.cs:
<pre class="prettyprint private void histogramControl_Paint(object sender, PaintEventArgs e)
{
if (myIsDrawing)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(new SolidBrush(myColor), myXUnit);
//The width of the pen is given by the XUnit for the control.
for (int i = 0; i < myValues.Length; i++)
{
//We draw each line
g.DrawLine(myPen,
new PointF(myOffset + (i * myXUnit), this.Height - myOffset),
new PointF(myOffset + (i * myXUnit), this.Height - myOffset - myValues * myYUnit));
//We plot the coresponding index for the maximum value.
/* if (myValues == myMaxValue)
{
SizeF mySize = g.MeasureString(i.ToString(), myFont);
g.DrawString(i.ToString(), myFont, new SolidBrush(myColor),
new PointF(myOffset + (i * myXUnit) - (mySize.Width / 2), this.Height - myFont.Height),
System.Drawing.StringFormat.GenericDefault);
}*/
}
//We draw the indexes for 0 and for the length of the array beeing plotted
g.DrawString("0", myFont, new SolidBrush(myColor), new PointF(myOffset, this.Height - myFont.Height), System.Drawing.StringFormat.GenericDefault);
g.DrawString((myValues.Length - 1).ToString(), myFont,
new SolidBrush(myColor),
new PointF(myOffset + (myValues.Length * myXUnit) - g.MeasureString((myValues.Length - 1).ToString(), myFont).Width,
this.Height - myFont.Height),
System.Drawing.StringFormat.GenericDefault);
//We draw a rectangle surrounding the control.
g.DrawRectangle(new System.Drawing.Pen(new SolidBrush(Color.Black), 1), 0, 0, this.Width - 1, this.Height - 1);
}
}
long myMaxValue;
private long[] myValues;
private bool myIsDrawing;
private float myYUnit; //this gives the vertical unit used to scale our values
private float myXUnit; //this gives the horizontal unit used to scale our values
private int myOffset = 20; //the offset, in pixels, from the control margins.
private Color myColor = Color.Black;
private Font myFont = new Font("Tahoma", 10);
[Category("Histogram Options")]
[Description("The distance from the margins for the histogram")]
public int Offset
{
set
{
if (value > 0)
myOffset = value;
}
get
{
return myOffset;
}
}
[Category("Histogram Options")]
[Description("The color used within the control")]
public Color DisplayColor
{
set
{
myColor = value;
}
get
{
return myColor;
}
}[/code]
Then its back to the scroll event in the form1 amd calling using the function:
<pre class="prettyprint public long GetTopLumAmount(long[] histogram, int amount)
{
if (histogram.Sum() < amount)
return -1;
long result = 0;
long counted = 0;
for (int i = histogram.Length - 1; i >= 0; i--)
{
if (counted < amount)
{
long current = Math.Min(histogram, amount - counted);
counted += current;
result += current * i;
}
else
break;
}
return result;
}[/code]
And then the scroll event is calling using the function:
<pre class="prettyprint public long GetHistogramMaximum(long[] histogram)
{
long result = 0;
for (int i = 0; i < histogram.Length; i++)
{
if (histogram > result)
{
// MessageBox.Show(string.Format("{0} is greater than {1}", histogram, result));
label28.Text = "Thw maximum value is " + histogram;
label27.Text = "The Maximum Value Is Present At Index " + i;
result = histogram;
}
}
return result;
}[/code]
Then its doing the setpicture function the code of the setpicture and in the end its doing this.pictureBox1.Refresh();
This is the setpicture function code:
<pre class="prettyprint private void setpicture(int indx)
{
if (_fi == null)
{
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
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))
{
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;
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]
<br/>
And then its doing everything in the scroll event again it dosent quit it or something just doing it all over again right away.
Maybe something in the setpicture function ? Or the pictureBox1.Refresh doing something ?
Thanks.
<
danieli
<br/>
View the full article