Torna al Thread
private void Genera_Spettro(string filename)
{
FileStream inStream = File.OpenRead(filename);
WaveReader wr = new WaveReader(inStream);
this.Wave = wr.Data[0];
if (this.wave == null)
{
this.GenerateSampleWave();
}
if (this.wave.Length > 0)
{
// qui dove carico la bitmap.
Bitmap bmp = new Bitmap(pictureBox1.Width - 1, pictureBox1.Height - 1);
Graphics g = Graphics.FromImage(bmp);
Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
Color frmColor = this.BackColor;
Brush bckColor;
alpha = (opacity * 255) / 100;
if (drag)
{
Color dragBckColor;
if (BackColor != Color.Transparent)
{
int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragBckColor = Color.FromArgb(Rb, Gb, Bb);
}
else dragBckColor = frmColor;
alpha = 255;
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
}
else
{
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
Pen pen = new Pen(this.ForeColor);
if (this.BackColor != Color.Transparent | drag)
{
g.FillRectangle(bckColor, bounds);
}
int middle = bounds.Top + (int)Math.Round((double)bounds.Height / 2d);
double factor = Math.Floor(((double)bounds.Height) / 2d) / ((double)this.wave.Max(i => Math.Abs(i - WaveAverage)));
double dataPerPixel = ((double)this.wave.Length) / ((double)(bounds.Width + 1));
int length = (int)Math.Ceiling(dataPerPixel);
int posA = 0;
if (length == 1)
{
int yA = middle + (int)Math.Round((double)(this.wave[0] - WaveAverage) * factor);
int yB = yA;
for (int x = bounds.Left + 1; x <= bounds.Right; x++)
{
int posB = (int)Math.Floor(((double)(x - bounds.Left)) * dataPerPixel);
if (posB > posA)
{
posA = posB;
yB = middle + (int)Math.Round((double)(this.wave[posB] - WaveAverage) * factor);
}
g.DrawLine(pen, new Point(x - 1, yA), new Point(x, yB));
yA = yB;
}
}
else
{
int yA = middle + (int)Math.Round((double)(this.wave[0] - WaveAverage) * factor);
int yB = yA;
for (int x = bounds.Left + 1; x <= bounds.Right; x++)
{
int posB = (int)Math.Floor(((double)(x - bounds.Left)) * dataPerPixel);
yB = middle + (int)Math.Round((double)(this.wave[posB] - WaveAverage) * factor);
if ((posB - posA) > 1)
{
Int32[] values = new Int32[posB - posA];
Array.Copy(wave, posA + 1, values, 0, posB - posA);
int max = (int)Math.Round((double)(values.Max() - WaveAverage) * factor);
int min = (int)Math.Round((double)(values.Min() - WaveAverage) * factor);
if (max == min)
g.DrawLine(pen, new Point(x - 1, yA), new Point(x, middle + max));
else
g.DrawLine(pen, new Point(x, middle + min), new Point(x, middle + max));
}
else
{
g.DrawLine(pen, new Point(x - 1, yA), new Point(x, yB));
}
posA = posB;
yA = yB;
}
}
pictureBox1.Image = bmp;
pen.Dispose();
bckColor.Dispose();
g.Dispose();
}
}