显示不同行的相同图像 [英] Same image displaying for different rows

查看:73
本文介绍了显示不同行的相同图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要做的是在已经从base64String转换的datagridview上显示图像。我的问题是它为每一行显示相同的图像,即使base64String为每个图像都不同。我的代码显示我已经首先声明了ImageColumn,然后将Base64String转换为datagridview的每一行中的imageBytes。我缺少什么?



我的尝试:



< pre lang =c#> DataGridViewImageColumn ic = new DataGridViewImageColumn();
ic.HeaderText = Img;
ic.Name = cImg;
ic.Width = 100 ;
ic.DisplayIndex = 5 ;
dgvProducts.Columns.Add(ic);


foreach (DataGridViewRow row in dgvProducts.Rows)
{
byte [] imageBytes =
Convert.FromBase64String(row.Cells [ 图片]。Value.ToString());
使用(MemoryStream ms = new MemoryStream(imageBytes, 0 ,imageBytes.Length))
{
foreach (DataGridViewRow rows in dgvProducts.Rows)
{
ic.Image = Image.FromStream(ms, true );
}

}

如果(ic.Image!= null
{
row.Height = ic.Width;
}

解决方案

问题是双重的:首先,正如CHill60指出的那样,你有一个冗余的内循环:

  foreach (DataGridViewRow row  in  dgvProducts.Rows )
{
byte [] imageBytes = Convert.FromBase64String(row.Cells [ 图片]。Value.ToString());
使用(MemoryStream ms = new MemoryStream(imageBytes, 0 ,imageBytes.Length))
{
foreach (DataGridViewRow rows in dgvProducts.Rows)
{
ic.Image = Image.FromStream(ms, true );
}

内部foreach甚至不使用循环变量 - 所以它所做的只是平方你创建的图像数量。



另一个问题是你总是把创建的图像放在同一个地方:

 ic.Image = Image.FromStream(ms,); 

但由于 ic 的值只能在两个循环之外设置,而且你总是设置整个列的图像,你将总是得到你在列的每个单元格中加载的最后一个图像!

试试这个:

  foreach (DataGridViewRow行 dgvProducts.Rows中)
{
byte [] imageBytes = Convert.FromBase64String(row.Cells [ Picture]。Value.ToString ());
MemoryStream ms = new MemoryStream(imageBytes, 0 ,imageBytes.Length))
row.Cells [ cImg]。Value = Image.FromStream(ms, true );



请注意,您无法在循环内处理流。文档非常清楚,流必须在图像的生命周期内存在: Image.FromStream方法(Stream,Boolean)(System.Drawing) [ ^ ]

备注
您必须保持流的生命周期为图片。





忘记了Value属性! :O [/ edit]


Hi,
What im trying to do is display an image on a datagridview that has been converted from a base64String.My problem is it is displaying the same image for each row even though the base64String for each image is different. My code below shows that i have declared the ImageColumn first, then converted the Base64String into imageBytes in each row of the datagridview. What am i missing?

What I have tried:

DataGridViewImageColumn ic = new DataGridViewImageColumn();
ic.HeaderText = "Img";
ic.Name = "cImg";
ic.Width = 100;
ic.DisplayIndex = 5;
dgvProducts.Columns.Add(ic);


foreach (DataGridViewRow row in dgvProducts.Rows)
{
  byte[] imageBytes = 
Convert.FromBase64String(row.Cells["Picture"].Value.ToString());
 using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
 {
     foreach (DataGridViewRow rows in dgvProducts.Rows)
     {
       ic.Image = Image.FromStream(ms, true);
     }
                    
 }
            
     if(ic.Image != null)
     {
        row.Height = ic.Width;
      }

解决方案

The problem is twofold: firstly as CHill60 has pointed out, you have a redundant inner loop:

foreach (DataGridViewRow row in dgvProducts.Rows)
{
     byte[] imageBytes = Convert.FromBase64String(row.Cells["Picture"].Value.ToString());
     using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
     {
         foreach (DataGridViewRow rows in dgvProducts.Rows)
         {
             ic.Image = Image.FromStream(ms, true);
         }

The inner foreach doesn't even use the loop variable - so all it does is square the number of images you create.

The other problem is that you always put the created image in the same place:

ic.Image = Image.FromStream(ms, true);

but since the value of ic is only ever set outside both loops, and you always set the image for the whole column you will always get the last image you load in every cell of the column!
Try this:

foreach (DataGridViewRow row in dgvProducts.Rows)
    {
    byte[] imageBytes = Convert.FromBase64String(row.Cells["Picture"].Value.ToString());
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
    row.Cells["cImg"].Value = Image.FromStream(ms, true);


Note that you can't Dispose the stream inside the loop. The documentation is very clear that the stream must exist for the lifetime of the image: Image.FromStream Method (Stream, Boolean) (System.Drawing)[^]

Remarks
You must keep the stream open for the lifetime of the Image.



[edit]Forgot the Value property! :O [/edit]


这篇关于显示不同行的相同图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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