显示使用C#中的dataGridView位图 [英] Showing a bitmap in dataGridView using C#

查看:161
本文介绍了显示使用C#中的dataGridView位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的dataGridView显示一些图像。我有两个组成部分,的dataGridView,DataTable,并一个位图。数据表有两列。

I want to show in dataGridView some image. I have two components, dataGridView, dataTable and one Bitmap. DataTable has two columns.

dataGridview.source = dataTable;

现在,我想的dataGridView显示三列,两个来自DataTable,并新三板与我的位图。如果是比较容易的,我可以修改DataTable,并的dataGridView。

Now, I want to show in dataGridView three columns, two from dataTable and new third with my bitmap. If it is easier, I can modify dataTable and dataGridView.

是否有可能呢?

推荐答案

有几种方法你可以接近这一点。

There are several ways you can approach this.

有是一个图像列类型在DataGridView的DataGridViewImageColumn,里面有,你可以通过一个位图图像属性。

There is an image column type for the DataGridView, the DataGridViewImageColumn, which has an image property that you can pass a bitmap to.

类似下面应该工作:

private void createGraphicsColumn(Bitmap image)
{
    DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
    imageColumn.Image = image;
    imageColumn.Name = "Tree";
    imageColumn.HeaderText = "Nice tree";
    dataGridView1.Columns.Insert(2, imageColumn);
}

您还可以,如果需要设置单个单元格的Value属性在此列。

You can also set the Value property of individual cells in this column if needed.

上面的例子加上一大堆其他的讨论上可以的 MSDN

The example above plus a whole lot of other discussion can be found on MSDN.

另一种方法是添加映像成数据表 - 这会自动生成图像列,但在数据表需要新列的类型为字节数组

Another option is to add your image into the datatable - this will automatically generate your image column, but the new column in the datatable needs to be of type byte array.

我发现下面的代码来做到这一点快速谷歌:

I found the following code to do this with a quick google:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    return  ms.ToArray();
}

如果你采用这种方法,我建议做一个小小的研究,以找到创建字节数组的最佳方法 - 我不能担保该代码是最好的。

If you do take this approach I'd suggest doing a little research to find the best method of creating the byte array - I couldn't vouch for that code being the best.

这篇关于显示使用C#中的dataGridView位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆