将base64string转换为图像并显示在datagridview上

编程入门 行业动态 更新时间:2024-10-25 22:29:23
本文介绍了将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) [ ^ ]. 您的主要问题是对周围的所有内容进行硬编码:图片",列".我希望这只是为了说明.但是,如果您在真实的应用程序中执行此操作,则无法获得可维护性良好的代码.请参阅: 魔术字符串-维基百科,免费的百科全书 [ ^ ], 魔术数(编程)—维基百科,免费的百科全书 [ ^ ], 反模式-维基百科,免费的百科全书 [ ^ ].
—SA
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上

本文发布于:2023-11-26 19:32:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   图像   base64string   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!