public partial class Form1 : Form
{
public Form1()
{
//InitializeComponent();
string filename = "c:\\temp\\example.dat";
if (!File.Exists(filename))
{
MessageBox.Show("First run. Will create 300M example data file: " + filename);
GenerateData(filename);
}
this.memoryMappedFile = MemoryMappedFile.CreateFromFile(filename);
this.scrollbar = new HScrollBar()
{
Height = 32,
Dock = DockStyle.Bottom,
Maximum = (int)(new FileInfo(filename).Length / 4),
};
this.scrollbar.ValueChanged += (sender, e) => this.Invalidate(invalidateChildren: false);
this.Controls.Add(scrollbar);
this.DoubleBuffered = true;
this.Size = new Size(800, 600);
}
ScrollBar scrollbar;
MemoryMappedFile memoryMappedFile;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
int sampling = 100;
int start = Math.Min(this.scrollbar.Value, this.scrollbar.Maximum - sampling);
e.Graphics.DrawCurve(Pens.Black, GetPoints(start, sampling));
e.Graphics.DrawString(start.ToString("n0"), this.Font, Brushes.DarkBlue, new Point(200, 15));
}
private PointF[] GetPoints(int start, int count)
{
List<PointF> points = new List<PointF>(count);
using (var viewStream = memoryMappedFile.CreateViewStream(start * sizeof(float), count * sizeof(float)))
using (var reader = new BinaryReader(viewStream))
{
for (int i = 0; i < count; i++)
{
float x = i * this.ClientRectangle.Width / 100;
float y = reader.ReadSingle() * this.ClientRectangle.Height;
points.Add(new PointF(x, y));
}
}
return points.ToArray();
}
private void GenerateData(string outputFile = "c:\\temp\\my.dat", int count = 75 * 1024 * 1024)
{
Random random = new Random(123);
if (!Directory.Exists(Path.GetDirectoryName(outputFile))) Directory.CreateDirectory(Path.GetDirectoryName(outputFile));
using (BinaryWriter writer = new BinaryWriter(new FileStream(outputFile, FileMode.Create)))
{
float last = 0.5f;
for (int i = 0; i < count; i++)
{
last = last + (float)(random.NextDouble() - 0.5) / 20;
last = Math.Max(0, Math.Min(last, 1));
writer.Write(last);
}
}
}
}