2

How do you load an image from a URL and then put it into DataGridView's cell (not Column header)? The rows which include the images will be added to the view at runtime based on a search from web service. Cannot find an answer for this specific purpose... Help!

First I tried using PictureBox. When events are received from web service, I will loop thru result to add rows, each of which includes an image.

// 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;

Even though the image loads perfectly, it looks disconnected from the view, i.e. images does not move when I scroll, and when refreshing the view with new data, images just hang around... bad.

So I tried tips from other postings:

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

But I got an error saying "URI formats are not supported."

  • Am I using Image incorrectly?
  • Is there another class that I can use for URL image instead of Image?
  • Or do I have no choice but to create a custom control (which contains a PictureBox) to add to the DataGridView cell?
M W
  • 1,269
  • 5
  • 21
  • 31
  • Try to this link belove maybe it can help you. http://stackoverflow.com/questions/17334843/get-a-image-in-data-gridview-to-picturebox-in-c-sharp/19970816#19970816 – Koeuy Chetra Nov 14 '13 at 06:21

2 Answers2

3

Have a look at this 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;
    }
Community
  • 1
  • 1
Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89
  • 1
    Thanks I think this would lead me to an answer. So to answer my own questions, I should use the DataGridViewImageCell/Column, and load the image dynamically following the instruction. I also looked at the option of creating a custom control inherited from the abstract DataGridViewCell, and got stuck at having to do the manual painting. See this[http://devolutions.net/articles/Dot-net/DataGridViewFAQ/DataGridViewFAQ.htm#_Toc119903415] which explains why I can't (easily) do that. – M W May 26 '12 at 07:53
-3

Here is my solution. It works correctly for me to retrieve an image from DataGridView to load in PictureBox.

Form Event:

 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 Table:

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

SQL Insert image:

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')

Result

Video link: Retrieve an image in DataGridView load to PictureBox in VB.NET