• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5131
  • Last Modified:

Load Picture Box Image from DataGridView Image Column

Greetings Experts,

I have images saved in an SQL Server 2008 database.  I have the database bound to a DataGridView, with one column defined as a DataGridViewImageColumn.  The records are displaying properly, as well as the image column.  

When the user clicks on the cell that contains the image, I am launching a "Picture Viewer" form that has a PictureBox control on it.  I am not sure how to load the image in the PictureBox from the DataGridViewImageColumn.   I'm guessing that I don't need to go back to the database, since the image is already loaded in the DataGridView cell.

Can you help?

Thank you in advance,

Keith
1
kdwood
Asked:
kdwood
  • 2
  • 2
2 Solutions
 
Bob LearnedCommented:
I would think that if you accessed the cell as a DataGridViewImageCell, then you could get the image from the Value property to display on a PictureBox control.
0
 
Meir RivkinFull stack Software EngineerCommented:
from msdn:
A DataGridViewImageColumn has an associated DataGridViewImageCell in every DataGridViewRow that intersects it.
Each cell is capable of containing an Image or an Icon, depending on the value of the DataGridViewImageCell.ValueIsIcon property.

check http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.image.aspx

so, basically u can get the Image object using the property Image:
0
 
kdwoodAuthor Commented:
Thanks for the replies,

I tried going after the DataGridViewImageCell, but get the following error:

"Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'."

My code:


Private Sub dgMasterPartList_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgMasterPartList.CellMouseClick

   Dim myIMGCell As New DataGridViewImageCell

   myIMGCell = Me.dgMasterPartList.Item("PartImage", e.RowIndex)

   frmPartImageViewer.pbProdImageLG.Image = myIMGCell.Value

End Sub
0
 
Meir RivkinFull stack Software EngineerCommented:
use this function to convert bytearray to image:
static private Image byteArrayToImage(byte[] ba)
		{
			MemoryStream ms= new MemoryStream();
			Image rc= null;
			try
			{
				ms.Write(ba,0,ba.Length);
				rc= new Bitmap(ms);
			}
			finally
			{
				ms.Close();
			}
			return rc;
		}

Open in new window

0
 
kdwoodAuthor Commented:
It worked like a champ sedgqick!   Thanks to both of you for your quick help.

Here is my final code in VB.NET:

Private Sub dgMasterPartList_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgMasterPartList.CellMouseClick

   Dim myIMGCell As New DataGridViewImageCell 

   myIMGCell = Me.dgMasterPartList.Item("PartImage", e.RowIndex)
   frmPartImageViewer.pbProdImageLG.Image = byteArrayToImage(myIMGCell.Value)

End Sub


 Private Shared Function byteArrayToImage(ByVal ba As Byte()) As Image

        Dim ms As New System.IO.MemoryStream()
        Dim rc As Image = Nothing

        Try
            ms.Write(ba, 0, ba.Length)
            rc = New Bitmap(ms)
        Finally
            ms.Close()
        End Try

        Return rc

    End Function

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now