We help IT Professionals succeed at work.
Get Started

How do I dynamically load an image into Crystal Report using vb.net?

1,290 Views
Last Modified: 2012-05-07
I have been searching the web for an answer to my issue.  I have found many articles and forums, which I have used to get where I am, that discuss how to dynamically load images into a Crystal Report using VB.Net.  I have followed the examples to the letter and yet I am unable to get my images onto the report.  I have seen several peoples comments saying that it worked perfectly and that they were able to get it to work.  However, when I run my code and it gets to the point where it tries to read the bytes of the file into the row I get an exception error that states "Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.Couldn't store <System.Byte[]> in img Column.  Expected type is Byte."  I am not sure what I am doing wrong, but it does not seem to want to load.  Any assistance would be GREATLY appreciated.
Public Function ImgTable(ByVal ImageFile As String) As DataTable
        ImgTable = New DataTable
        Dim row As DataRow
 
        ImgTable.TableName = "Images"
        ImgTable.Columns.Add("img", GetType(System.Byte))
 
        FileCopy(ImageFile, "C:\test.tif")     '<-- I am only copying the image locally because of a different error
        Dim fs As New IO.FileStream("C:\test.tif", IO.FileMode.Open)
        Dim br As New IO.BinaryReader(fs)
 
        row = ImgTable.NewRow
        row(0) = br.ReadBytes(br.BaseStream.Length)   '<-- This is where I get the error
        ImgTable.Rows.Add(row)
 
        br = Nothing
        fs.Close()
        fs = Nothing
    End Function

Open in new window

Comment
Watch Question
Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE