将base64string转换为图像并显示在datagridview上 [英] Convert base64string to image and display on datagridview

查看:248
本文介绍了将base64string转换为图像并显示在datagridview上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在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.


您并没有真正尝试任何事情.要显示图像,您需要一个能够显示图像的单元格:
DataGridViewImageCell类(System.Windows.Forms) [ DataGridViewImageColumn类(System.Windows.Forms) [ 魔术字符串-维基百科,免费的百科全书 [魔术数(编程)—维基百科,免费的百科全书 [反模式-维基百科,免费的百科全书 [
You did not really try anything. To show an image, you need a cell of the type capable of showing an image:
DataGridViewImageCell Class (System.Windows.Forms)[^].

You can have a whole column of such cells: DataGridViewImageColumn Class (System.Windows.Forms)[^].

Your major problem is hard-coding everything around: "Picture", "Columns". I hope it''s only for illustration. But if you do it in a real application, you cannot get well maintainable code. Please see:
Magic string — Wikipedia, the free encyclopedia[^],
Magic number (programming) — Wikipedia, the free encyclopedia[^],
Anti-pattern — Wikipedia, the free encyclopedia[^].

—SA


这篇关于将base64string转换为图像并显示在datagridview上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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