如何从datagridview加载图片框中的图像 [英] How to load image in picture box from datagridview

查看:47
本文介绍了如何从datagridview加载图片框中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误无法从System.String转换为System.Byte []代码如下:



I am getting error "Cannot convert from System.String to System.Byte[]" on below code:

private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {

           byte[] imgbyte = (byte[])dgvProduct.CurrentRow.Cells[11].Value;
           GetPhoto(imgbyte);

       }





我的尝试:





What I have tried:

private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {

          byte[] imgbyte = (byte[])dgvProduct.CurrentRow.Cells[11].Value;
          GetPhoto(imgbyte);

      }







private void GetPhoto(byte[] imgbyte)
       {
           Image newImage;
           using (MemoryStream ms = new MemoryStream(imgbyte, 0, imgbyte.Length))
           {
               ms.Write(imgbyte, 0, imgbyte.Length);

               newImage = Image.FromStream(ms, true);
               pbItem.Image = newImage;

           }
       }

推荐答案

首先使用调试器并查看你试图转换的字符串究竟是什么:很可能它不是你可以直接用作图像数据的字节流:它可能是文件的路径,它可能是Base64翻译的图像数据,它可能完全是错误的单元格 - 我们不知道,现在你也不知道!



所以使用调试器看看凝视和锻炼实际上是什么内容:在行上放置一个断点

Start by using the debugger and looking at exactly what is in the string you are trying to convert: the chances are that it isn't a stream of bytes that you can directly use as image data: it may be a path to a file, it may be Base64 translated image data, it may be the wrong cell entirely - we don't know, and at the moment neither do you!

So use the debugger to look at the staring and work out what the content actually is: put a breakpoint on the line
byte[] imgbyte = (byte[])dgvProduct.CurrentRow.Cells[11].Value;

并检查单元格中的内容。



我们不能为您做到这一点 - 我们没有访问您的数据......

And examine what is in the cell.

We can't do that for you - we don't have any access to your data...


这篇关于如何从datagridview加载图片框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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