Need Help with Image conversion..

dualshock03

Well-known member
Joined
Jan 28, 2007
Messages
105
Programming Experience
1-3
i have a dtagridview with an image column with a corresponding content click event that enables my picture box to display the image from the image item in datagrdiview.. then delete the retrieved by pressing with button..

check my attachment for viewing...

this is my code for the datagridview content click event:

VB.NET:
Private Sub _DELETEGRID_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _DELETEGRID.CellContentClick
        Try
            lblname.Text = _DELETEGRID.Item(1, e.RowIndex).Value
            lblid.Text = _DELETEGRID.Item(0, e.RowIndex).Value
            lblfiled.Text = _DELETEGRID.Item(3, e.RowIndex).Value
            lblpic.Image = Image.FromStream(_DELETEGRID.Item(4, e.RowIndex).Value) ' fixing yet
        Catch click_err As Exception
            MsgBox(click_err.Message)
            Exit Try
        End Try

However im ecountering this error = ("Unable to cast object of type 'System.Byte[]' to type 'System.IO.Stream'.") due to wrong conversion type of the image.. i dont what to do.. my brain's rolling inside my head waaahh... please help fix this.. i know its something to do with filestream... am i right??


as for deleting the selected record from the datagridview with my button, how to do code for this?
 

Attachments

  • image1.GIF
    image1.GIF
    44.8 KB · Views: 39
The Value object is already an Image object.
 
so if its already an object how do im going to pass it to my picturebox? any code?

does it need to be declared using memorystream?
 
You may want to try this (from the top of my head):

VB.NET:
        Dim imagestream As New MemoryStream 
        Dim bytes  As Byte() = CType(_DELETEGRID.Item(4, e.RowIndex).Value, Byte())  
        imagestream.Write(bytes, 0, bytes.Length) 
        Dim myImage As Image = Image.FromStream(imagestream) 
        'display  the image. 
        lblpic.Image = myImage
 
so if its already an object how do im going to pass it to my picturebox? any code?
This works for unbound grid:
VB.NET:
Me.PictureBox1.Image = CType(Me.Table1DataGridView.Item("imgDataGridViewImageColumn", 0).Value, Image)
For bound grid the image cell represents a data column that must be type Byte array, in which case you have to use a MemoryStream and Image.FromStream.

For measures I just tested the FromStream call when cell value was an Image, and indeed the error is 'can't cast from Bitmap to Stream', so we can deduct that you have a bound grid and have to use the latter method.
as for deleting the selected record from the datagridview with my button, how to do code for this?
Use your BindingSource:
VB.NET:
Me.Table1BindingSource.RemoveCurrent()
 

Latest posts

Back
Top