load an image based on a provided filepath in the report at runtime.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: load an image based on a provided filepath in the report at runtime.

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    21

    load an image based on a provided filepath in the report at runtime.

    Hi

    I have to program in C# such that I have to add a button to the form , and create a crystal report, by getting source
    from the xml schema file(.xsd file) with two fields namely name,image..... name is string datatype and image is of
    datatype base64Binary. Now i have to add a crystal report viewer to the form and whenthe program is run, we
    should be able to load any image ,ie, by clicking the button add any image from the disk and that image should be displayed
    in the report in image column......


    so wht i have to do is
    -create a dataset that supports images
    -create a report that support this dataset
    -load data in the dataset
    -use the dataset as datasource for the report and show the report in the viewer

    Then i found a code in Vb...
    please change for me in c# and explain how to do...

    i will paste the coding part
    Code:
    Private Sub btnLoadImage_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnLoadImage.Click

    Dim ofdImage As New OpenFileDialog

    If ofdImage.ShowDialog = DialogResult.OK Then

    Try

    'Create a new instance of the dataset
    Dim dsImgRpt As New dsImageReport

    'Create a new instance of the report
    Dim m_imagereport As New rptImage

    'Create a new row in the dataset
    Dim dr As dsImageReport.ImageRow = _
    dsImgRpt.Image.NewImageRow

    'Set the various elements of the row
    dr.Name = ofdImage.FileName
    'fill this field with a byte array
    dr.Photo = GetImageData(ofdImage.FileName)

    'Add the new row to the dataset
    dsImgRpt.Image.Rows.Add(dr)

    'Use the dataset as datasource for the report
    m_imagereport.SetDataSource(dsImgRpt)

    'Show the report in the reportviewer
    Me.crystalReportViewer1.ReportSource = m_imagereport

    Catch ex As Exception

    MessageBox.Show("Something went wrong: " & ex.Message)

    End Try

    End If

    End Sub

    Private Function GetImageData(ByVal fileName As String) As Byte()

    'Method to load an image from disk and return it as a bytestream
    Dim fs As System.IO.FileStream = _
    New System.IO.FileStream(fileName, _
    System.IO.FileMode.Open, System.IO.FileAccess.Read)
    Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs)
    Return (br.ReadBytes(Convert.ToInt32(br.BaseStream.Length)))

    End Function


    please help me to do this..

    d h o l

  2. #2
    Join Date
    Feb 2008
    Posts
    2

    Re: Load an image based on a provided filepath in the report at runtime.

    Hello,

    Below is the converted code in C# and you shall see the comments along with the code which explains the function the code would be performing:
    Code:
    private void btnLoadImage_Click(object sender, System.EventArgs e) 
    { 
        OpenFileDialog ofdImage = new OpenFileDialog();  
        if (ofdImage.ShowDialog == DialogResult.OK) { 
            
            try { 
                //Create a new instance of the dataset 
                dsImageReport dsImgRpt = new dsImageReport(); 
                //Create a new instance of the report 
                rptImage m_imagereport = new rptImage(); 
                //Create a new row in the dataset 
                dsImageReport.ImageRow dr = dsImgRpt.Image.NewImageRow; 
                //Set the various elements of the row 
                dr.Name = ofdImage.FileName; 
                //fill this field with a byte array 
                dr.Photo = GetImageData(ofdImage.FileName); 
                //Add the new row to the dataset 
                dsImgRpt.Image.Rows.Add(dr); 
                //Use the dataset as datasource for the report 
                m_imagereport.SetDataSource(dsImgRpt); 
                //Show the report in the reportviewer 
                this.crystalReportViewer1.ReportSource = m_imagereport; 
            } 
            catch (Exception ex) { 
                MessageBox.Show("Something went wrong: " + ex.Message); 
            } 
        } 
    } 
    
    private byte[] GetImageData(string fileName) 
    {     
        //Method to load an image from disk and return it as a bytestream 
        System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
        System.IO.BinaryReader br = new System.IO.BinaryReader(fs); 
        return (br.ReadBytes(Convert.ToInt32(br.BaseStream.Length))); 
    }
    Regards,
    Dave

    Dave Traister
    Software Engineer
    ComponentOne LLC
    Last edited by Hack; 02-20-2008 at 07:30 AM. Reason: Added Code Tags

Similar Threads

  1. Replies: 0
    Last Post: 03-21-2001, 05:46 PM
  2. How do you load an image from ram?
    By Arty Clark in forum VB Classic
    Replies: 2
    Last Post: 08-24-2000, 11:24 AM
  3. force image to load from server--not cache
    By Garrett Smith in forum Enterprise
    Replies: 0
    Last Post: 07-31-2000, 04:00 PM
  4. Load an API Image list from a Strip Bitmap in a resource file
    By Scott Stanaland in forum VB Classic
    Replies: 8
    Last Post: 05-31-2000, 12:07 PM
  5. How do you load an image from ram?
    By Arty Clark in forum VB Classic
    Replies: 0
    Last Post: 03-26-2000, 04:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center