LOAD IMAGES DYNAMICALLY INTO CRYSTAL REPORTS USING VS2005

If you want to insert images from the file system (eg, C:\images folder) into crystal reports here is a simple process of doing that.

Add a dataset  to your project. Add a datatable to the dataset. Then add a row of data type system.byte().

dataset-photo

Add a new crystal report using the dataset created as its datasource. You are done now.

Add a new form to your project with a crystal report viewer and a button

The method below handles the button click event

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ofd As New OpenFileDialog()

        ofd.Multiselect = False

        ofd.Filter = “Images|*.jpg;*gif;*.bmp”

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

            Me.createReports(Me.savepic(ofd.FileName))

        Else

            MessageBox.Show(“Please select a Picture”)

        End If

    End Sub

 

Public Function savepic(ByVal fsource As String) As Byte()

        Dim arrImage() As Byte = Nothing

        If File.Exists(fsource) = False Then

            Return arrImage

        End If

        Dim img As Image = Image.FromFile(fsource)

        Dim ms As New IO.MemoryStream

        img.Save(ms, img.RawFormat)

        arrImage = ms.GetBuffer

        ms.Close()

        Return arrImage

    End Function

 

    Private Sub createReports(ByVal image() As Byte)

        Me.DataSet1.reports.Clear()

        Dim nrow As DataRow = DataSet1.reports.NewreportsRow

        nrow(0) = image

        DataSet1.reports.AddreportsRow(nrow)

        Dim rpt1 As New rptImages     ‘ crystal report object

        rpt1.SetDataSource(DataSet1)            ‘ set the datasource of crystalreport object, dataset 1 was the one created

        CrystalReportViewer1.ReportSource = rpt1

    End Sub

 

Sample picture when the application is run

Sample Picture of the Application
Sample Picture of the Application
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s