How to show image from URL in datagridview cell?

前端 未结 2 1448
清歌不尽
清歌不尽 2021-01-27 03:28

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

相关标签:
2条回答
  • 2021-01-27 03:53

    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

    0 讨论(0)
  • 2021-01-27 04:11

    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;
        }
    
    0 讨论(0)
提交回复
热议问题