Question Image from DB issues

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

A DB Table is brought into a DataTable.

If the value of the long binary field in null it should return a default image, but that is not happening

Here is the PictureBox control

VB.NET:
Dim Default_LogoPic As New PictureBox
            With Default_LogoPic
                .Size = New Point(70, 70)
                .Location = New Point(320, 60)
                If Form1.AnnualData.Columns.Contains("Logo_Flag") Then
                    Dim vImage() As Byte = NullImage(Row("Logo_Image"))
                    Dim vStream As New MemoryStream(vImage)
                    .Image = Image.FromStream(vStream)
                End If
            End With
            StationaryTab.Controls.Add(Default_LogoCB)

.. and here is the null value function


VB.NET:
Public Function NullImage(ByVal obj As Object) As Byte()
        Dim Img As Image = My.Resources.Cross
        Dim imgStream As MemoryStream = New MemoryStream()
        Img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png)
        imgStream.Close()
        Dim byteArray As Byte() = imgStream.ToArray()
        imgStream.Dispose()
        If Not obj.Equals(DBNull.Value) Then
            Dim Str As String = Convert.ToString(obj)
            Dim encoding As New System.Text.ASCIIEncoding()
            byteArray = encoding.GetBytes(Str)
        End If
        Return byteArray
    End Function

So far only got round to testing with nullvalues - no errors, but no default image loading either! Any ideas?

Thanks
 
Surely the very first thing you should be doing is checking for NULL. If the field is NULL then you simply get the default Image; no need for any MemoryStream at all.
VB.NET:
If row.IsNull("Logo_Image") Then
    .Image = My.Resources.Cross
Else
    Dim data As Byte() = DirectCast(row("Logo_Image"), Byte())

    'Get Image via MemoryStream.
End
 
jmcilhinney - thanks for your reply.

The idea was took keep the default image in one function - and it would appear it DOES work! I managed to load the same control name twice instead of the one I needed

VB.NET:
Dim Default_LogoPic As New PictureBox


VB.NET:
StationaryTab.Controls.Add(Default_LogoCB)

Thank you again for taking the time to reply!
 
Back
Top