问题描述
我试图在DatagridView中将Base64String显示为图像我的代码
Hi ,
Im trying to display a Base64String as an Image in a DatagridView my code
byte[] imageBytes = Convert.FromBase64String(dgvProducts.Rows[1].Cells["Picture"].Value.ToString());
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
如何使Image在DatagridView中显示为新列?
谢谢
我尝试过的事情:
我尝试将其转换为base64String,然后再转换为图像,但是我可以弄清楚如何将该图像显示回datagridview.
我已经尝试过,
dgvProducts.Columns.Add("Column","image");
How can i get Image to display as a new column in the DatagridView?
Thanks
What I have tried:
I have tried converting it into a base64String, then back into an image, but i can figure how to display that image back onto the datagridview.
i have tried,
dgvProducts.Columns.Add("Column","image");
this just gives me new new column with not data in the rows?
推荐答案
dgvProducts.Columns.Add("Column","image");
这只是为我提供了一个新的新列,但行中没有数据?"
好吧...是的...会的.它会添加一列,但不会向正在显示的行中添加任何数据-因此该列为空.
确切地说,如何添加数据将取决于DVG的数据源是什么,以及它来自何处.例如,如果要通过DataTable从数据库加载它,则需要向DT中添加一列,而不是DGV-然后为新列填充行数据.同样,如果您使用的是一个类实例的Collection,则需要向该类添加一个public属性,并将每个实例设置为Image本身.然后,将数据绑定到DVG时,所有这些都会自动发生.
如果要在代码中手动将行添加到DGV,则按照显示的方式添加列,并在相应的列名称下将图像添加到每一行.
"this just gives me new new column with not data in the rows?"
Well...yes...it will. It adds a column, but it doesn''t add any data to the rows that are being displayed - so the column is empty.
Exactly how you would add the data is going to depend on what your data source for thr DVG is, and where it comes from. If you are loading it from a DB via a DataTable for example, then you would need to add a column to the DT, rather than the DGV - and then populate the rows data for the new column. Similarly, if you are using a Collection of instances of a class, then you need to add a public property to the class and set each instance to the Image itself. Then it will all happen automatically when you bind the data to the DVG.
If you are manually adding rows to the DGV in your code, then add the column as you have shown, and add the Image to each row under the appropriate column name.
这篇关于将base64string转换为图像并显示在datagridview上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!