这是一个简短的代码示例,以便您可以测试控件的内存要求和性能。我看不出你应该怎么做才能避免位图,我认为大多数 3rd 方控件都以类似的方式工作。我确信我的代码可以通过多种方式进行优化,但您可以从一些方面着手。不确定何时希望在网格中有 20000 行,反正没有用户可以看到所有这些。也许人们可以想办法一次显示子集..?
图像的创建可能不应该在测试对象中完成(因为它是数据模型),而是在表示层中完成(我添加了 DataBindingComplete 事件,因为它可以用于类似的事情),我在这里这样做是因为它更容易。没有图像被保存到文件或类似的东西。
我使用名为 dataGridView1 的 DataGridView 创建了一个表单。
这是表单的代码:
List<TestObject> _list = new List<TestObject>();
public Form1()
{
InitializeComponent();
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
}
void dataGridView1_DataBindingComplete( object sender, DataGridViewBindingCompleteEventArgs e )
{
}
private void Form1_Load( object sender, EventArgs e )
{
// Populate the grid, here you should add as many rows as you want to display
_list.Add(new TestObject("Obj1", 20, Brushes.Red, new int[]{3,4,5,3,5,6}));
_list.Add(new TestObject("Obj2", 10, Brushes.Green, new int[] { 1, 2, 3, 4, 5, 6 }));
_list.Add(new TestObject("Obj3", 30, Brushes.Blue, new int[] { 3, 2, 1, 1, 2, 3 }));
dataGridView1.DataSource = _list;
}
我还创建了一个测试对象来填充网格:
public class TestObject
{
private const int BitmapWidth = 100;
private const int BitmapHeight = 20;
private System.Drawing.Brush _color;
private string _name;
private int[] _numbers;
private int _value;
public TestObject( string name, int value, System.Drawing.Brush color, int[] series )
{
_name = name;
_numbers = series;
_color = color;
_value = value;
}
public string Name
{
get { return _name; }
}
public string Value { get { return _value.ToString(); } }
public Image Series
{
get
{
int width = BitmapWidth / _numbers.Length - _numbers.Length;
System.Drawing.Bitmap b = new Bitmap(BitmapWidth, BitmapHeight);
Graphics g = Graphics.FromImage(b);
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
int current = 0;
for (int i = 0;i < _numbers.Length;i++)
{
g.FillRectangle(_color, current, BitmapHeight - (BitmapHeight / 10) * _numbers[i], width, (BitmapHeight / 10) * _numbers[i]);
current+=width + 2;
}
return b;
}
}
}