【问题标题】:How to show image from URL in datagridview cell?如何在 datagridview 单元格中显示来自 URL 的图像?
【发布时间】:2012-05-25 18:41:04
【问题描述】:

如何从 URL 加载图像,然后将其放入 DataGridView 的单元格(而不是列标题)?包含图像的行将在运行时基于来自 Web 服务的搜索添加到视图中。找不到此特定目的的答案...帮助!

首先我尝试使用 PictureBox。当从 Web 服务接收到事件时,我将通过结果循环添加行,每行都包含一个图像。

// Add image
System.Windows.Forms.PictureBox picEventImage = new System.Windows.Forms.PictureBox();
picEventImage.Image = global::MyEventfulQuery.Properties.Resources.defaultImage;
picEventImage.ImageLocation = Event.ImageUrl;
this.dgvEventsView.Controls.Add(picEventImage);
picEventImage.Location = this.dgvEventsView.GetCellDisplayRectangle(1, i, true).Location;

即使图像加载完美,但它看起来与视图断开连接,即当我滚动时图像不会移动,而当我用新数据刷新视图时,图像只是在周围徘徊......不好。

所以我尝试了其他帖子的提示:

Image image = Image.FromFile(Event.ImageUrl);
DataGridViewImageCell imageCell = new DataGridViewImageCell();
imageCell.Value = image;
this.dgvEventsView[1, i] = imageCell;

但我收到一条错误消息 “不支持 URI 格式。”

  • 我是否错误地使用了图片?
  • 是否有另一个类可以用于 URL 图像而不是 Image?
  • 还是我别无选择,只能创建一个自定义控件(其中包含一个 PictureBox)以添加到 DataGridView 单元格?

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

看看这个 SO Post https://stackoverflow.com/a/1906625/763026

 foreach (DataRow row in t.Rows)
    {
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(row["uri"].ToString());
                    myRequest.Method = "GET";
                    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
                    myResponse.Close();

                    row["Img"] = bmp;
    }

【讨论】:

  • 谢谢,我想这会让我找到答案。所以要回答我自己的问题,我应该使用 DataGridViewImageCell/Column,并按照说明动态加载图像。我还查看了创建从抽象 DataGridViewCell 继承的自定义控件的选项,并且不得不手动绘制。看到这个[devolutions.net/articles/Dot-net/DataGridViewFAQ/… 这解释了为什么我不能(轻松)这样做。
【解决方案2】:

这是我的解决方案。它可以正常工作,我可以从 DataGridView 中检索图像以加载到 PictureBox 中。

表单事件:

 Private con As New SqlConnection("YourConnectionString")
    Private com As SqlCommand
Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
        con.Open()
        com = New SqlCommand("SELECT MyPhoto FROM tbGalary WHERE ID=" & DGV.Rows(e.RowIndex).Cells(0).Value, con)
        Dim ms As New MemoryStream(CType(com.ExecuteScalar, Byte()))
        txtPicture.Image = Image.FromStream(ms)
        txtPicture.SizeMode = PictureBoxSizeMode.StretchImage
        com.Dispose()
        con.Close()
End Sub

SQL 表:

CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

SQL 插入图片:

INSERT INTO tbGalary VALUES('1','D:\image1.jpg')
INSERT INTO tbGalary VALUES('2','D:\image2.jpg')
INSERT INTO tbGalary VALUES('3','D:\image3.jpg')
INSERT INTO tbGalary VALUES('4','D:\image4.jpg')
INSERT INTO tbGalary VALUES('5','D:\image5.jpg')

结果

视频链接:Retrieve an image in DataGridView load to PictureBox in VB.NET

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2020-06-25
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    相关资源
    最近更新 更多