Dynamically show image

13 posts, 0 answers
  1. rh
    rh avatar
    193 posts
    Member since:
    Jan 2007

    Posted 24 Aug 2007 Link to this post

    I have a report where I need to dynamically show a different image on a row-by-row basis based on the data in that row. Is there an example on how to do this? If not, if someone has figured this out can you post an example?
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 24 Aug 2007 Link to this post

    Hi rh,

    You have to attach to the DetailSection's ItemDataBound event. Inside it, you can obtain a reference to the current row through the DataItem property and set the appropriate image. We have prepared a little sample that demonstrates how to do that. Please take a look at it and if you have further questions, feel free to contact us again. Here is an excerpt of its source code:

    1     public partial class DynamicPictureReport : Report 
    2     { 
    3         public DynamicPictureReport() 
    4         { 
    5             InitializeComponent(); 
    6  
    7             DataTable dummyData = new DataTable(); 
    8             dummyData.Columns.Add("ID"); 
    9             for(int i = 0; i < 3; i++) 
    10             { 
    11                 dummyData.Rows.Add(i); 
    12             } 
    13             this.DataSource = dummyData; 
    14         } 
    15  
    16         private void detail_ItemDataBound(object sender, System.EventArgs e) 
    17         { 
    18             Telerik.Reporting.Processing.DetailSection procDetail = sender as Telerik.Reporting.Processing.DetailSection; 
    19             Telerik.Reporting.Processing.PictureBox procPictureBox = procDetail.Items["pictureBox"as Telerik.Reporting.Processing.PictureBox; 
    20             DataRowView row = procDetail.DataItem as DataRowView; 
    21             string currentId = row["ID"as string
    22             Bitmap image = new Bitmap(100, 100); 
    23             Graphics dummy = Graphics.FromImage(image); 
    24             Font font = new Font("Arial", 18); 
    25             dummy.DrawString("I am " + currentId, font, Brushes.Red, new RectangleF(0, 0, 100, 100)); 
    26             procPictureBox.ImageData = image; 
    27         } 
    28     } 
    29  


    Regards,
    Rossen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Michael Helm
    Michael Helm avatar
    7 posts
    Member since:
    Jan 2007

    Posted 30 Aug 2007 Link to this post

    Hi,
    I exactly tryed your example because I simply dont bring any images to the screen. Using your example and some others that should work I always get this error message.

    I use the current version and try to display the report within the Web Viewer.

    Greetings,
    Alexander Brütt

     

    Serverfehler in der Anwendung /eyeView.

    Der Wert darf nicht NULL sein.
    Parametername: encoder

    Beschreibung: Unbehandelte Ausnahme beim Ausführen der aktuellen Webanforderung. Überprüfen Sie die Stapelüberwachung, um weitere Informationen über diesen Fehler anzuzeigen und festzustellen, wo der Fehler im Code verursacht wurde.

    Ausnahmedetails: System.ArgumentNullException: Der Wert darf nicht NULL sein.
    Parametername: encoder

    Quellfehler:

    Beim Ausführen der aktuellen Webanforderung wurde einen unbehandelte Ausnahme generiert. Informationen über den Ursprung und die Position der Ausnahme können mit der Ausnahmestapelüberwachung angezeigt werden.

    Stapelüberwachung:

    [ArgumentNullException: Der Wert darf nicht NULL sein.
    Parametername: encoder]
       System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +386727
       System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
       Telerik.Reporting.HtmlRendering.ImageRenderer.SaveImageInStream(HtmlRenderingContext context, Image img, String uniqueImageID, String mimeType) +80
       Telerik.Reporting.HtmlRendering.ImageRenderer.Render(ReportItemBase item, HtmlRenderingContext context) +85
       Telerik.Reporting.HtmlRendering.HtmlItemRender.Telerik.Reporting.HtmlRendering.IReportItemRender.Render(ReportItemBase item, RenderingContext context) +67
       Telerik.Reporting.HtmlRendering.RenderingContext.Render(ReportItemBase item) +47
       Telerik.Reporting.HtmlRendering.Matrix.Render(HtmlRenderingContext context) +1159
       Telerik.Reporting.HtmlRendering.ReportSectionBaseRenderer.Render(ReportItemBase item, HtmlRenderingContext context) +75
       Telerik.Reporting.HtmlRendering.HtmlItemRender.Telerik.Reporting.HtmlRendering.IReportItemRender.Render(ReportItemBase item, RenderingContext context) +67
       Telerik.Reporting.HtmlRendering.RenderingContext.Render(ReportItemBase item) +47
       Telerik.Reporting.HtmlRendering.HtmlPage.RenderBodyInternal(HtmlWriter bodyWriter) +598
       Telerik.Reporting.HtmlRendering.HtmlPage.Render(HtmlTextWriter writer, HtmlRenderingContext renderingContext) +152
       Telerik.Reporting.HtmlRendering.HtmlReport.Render(HtmlTextWriter writer) +353
       Telerik.Reporting.HtmlRendering.HtmlRenderingExtension.Telerik.Reporting.Processing.IRenderingExtension.Render(Report report, Hashtable renderingContext, Hashtable deviceInfo, CreateStream createStreamCallback, EvaluateHeaderFooterExpressions evalHeaderFooterCallback) +260
       Telerik.Reporting.Processing.ReportProcessor.Render(String format, Report reportDefinition, Hashtable deviceInfo, CreateStream createStramCallback) +136
       Telerik.ReportViewer.WebForms.ServerReport.Render(HttpResponse response, String format, Int32 pageNumber, Boolean refresh) +784
       Telerik.ReportViewer.WebForms.ReportPageOperation.PerformOperation(NameValueCollection urlQuery, HttpResponse response) +72
       Telerik.ReportViewer.WebForms.HttpHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext context) +118
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +303
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
    


    Versionsinformationen: Microsoft .NET Framework-Version:2.0.50727.832; ASP.NET-Version:2.0.50727.832
  4. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 30 Aug 2007 Link to this post

    Hi Alexander,

    The issue can be solved by using the following code snippet:
                
    using (Bitmap image = new Bitmap(100, 100)) 
                { 
                    using (Graphics dummy = Graphics.FromImage(image)) 
                    { 
                        Font font = new Font("Arial", 18); 
                        dummy.DrawString("I am " + currentId, font, Brushes.Red, new RectangleF(0, 0, 100, 100)); 
                    } 
     
                    MemoryStream ms = new MemoryStream(); 
                    image.Save(ms, ImageFormat.Bmp); 
     
                    procPictureBox.ImageData = Image.FromStream(ms); 
                } 


    The problem is that when the Bitmap is created from code its RawFormat is MemoryBmp which can't be saved in a stream. You first have to change the image format to a non memory one and just then pass the image to the picture box.


    Kind regards,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Michael Helm
    Michael Helm avatar
    7 posts
    Member since:
    Jan 2007

    Posted 31 Aug 2007 Link to this post

    Thank you,
    that workaround works fine for me!
    Regards,
    Alexander
  6. manu
    manu avatar
    4 posts
    Member since:
    Dec 2007

    Posted 28 Apr 2008 Link to this post

    Hi,
    I have the same problem, Instead of showing image in detail section, I have to show image in group header section. I am showing report for different agency
    (If I have 4 agency it will show one combine report separated by group header  as well as group footer section). Image on group header section needs to change as agency changed. Can you please provide the sample code ASAP.
    I also needs to email report as a attachement for agencies respective email Id that comes from database (need to email only that agency report part). Is it possible what i want to achieve in telerik report??


    Looking forward.....
  7. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 28 Apr 2008 Link to this post

    Hello Harendra,

    You can achieve the requested functionality in many different ways. To show the image you will need a PictureBox report item placed in the group header section. It can be bound to a data field (e.g. PictureBox1.Value = "=Fields.AgencyImage" or PictureBox1.Value = "=Fields.AgencyImageUrl") which either contains the image data or the location of the image. Another possible way is to load the image in the ItemDataBound event of the PictureBox depending on some specific value read from the current DataItem. Which way you will choose depends on your specific project requirements. You can see sample implementations from the Demos installed with Telerik Reporting and from this thread.

    To see how to send a report by e-mail you can refer to the following forum thread: Send a report by e-mail.

    Hope this helps.

    Best wishes,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. manu
    manu avatar
    4 posts
    Member since:
    Dec 2007

    Posted 29 Apr 2008 Link to this post

    Thankx  for reply..
    but my problem is still not solved. I try the solution given by you, in my case i don't have image instead i have url for image. below is the error..

    System.InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.     at InvoiceReportAll.groupHeaderSection1_ItemDataBound(Object sender, EventArgs e) in C:\SVNProjects\TMS\App_Code\InvoiceReportAll.vb:line 591     at Telerik.Reporting.ReportItemBase.RaiseEvent(Object key, Object sender, EventArgs e)     at Telerik.Reporting.ReportItemBase.RaiseItemDataBound(Object sender, EventArgs e)     at Telerik.Reporting.Processing.ReportItemBase.OnItemProcessed()     at Telerik.Reporting.Processing.Report.ProcessGroup(DataRowView dataItem, Int32 level)     at Telerik.Reporting.Processing.Report.ProcessData(DataView dataView, Int32 level, ProcessDataHandler dataHandler)     at Telerik.Reporting.Processing.Report.ProcessData(DataView dataView, Int32 level)     at Telerik.Reporting.Processing.Report.ProcessReportContent()     at Telerik.Reporting.Processing.Report.ProcessItem()     at Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Report reportDefinition)     at Telerik.Reporting.Processing.ReportProcessor.Render(String format, Report reportDefinition, Hashtable deviceInfo, CreateStream createStramCallback)     at Telerik.ReportViewer.WebForms.ServerReport.Render(HttpResponse response, String format, Int32 pageNumber, Boolean refresh)     at Telerik.ReportViewer.WebForms.ReportPageOperation.PerformOperation(NameValueCollection urlQuery, HttpResponse response)     at Telerik.ReportViewer.WebForms.HttpHandler.System.Web.IHttpHandler.Proc


    my code is.....

    Public Sub ReportBind(ByVal InvoiceId As String, ByVal BatchId As String, ByVal AccountId As Int16)
            Me.CurrSum = 0
            Dim objP As New PClsInvoice
            Dim objInv As New ClsInvoice
            Dim dtbl As DataTable
            objP.InvoiceId = InvoiceId
            objP.AccountId = AccountId
            dtbl = objInv.PrintInvoiceAll(objP, BatchId)  // returns data table  contain report data
            Dim row As DataRow
            Dim Imageurl As New DataColumn("Imageurl")
            Imageurl.DataType = System.Type.GetType("System.Byte[]")
            dtbl.Columns.Add(Imageurl)
            dtbl.AcceptChanges()
            Dim en As New System.Text.ASCIIEncoding
            For Each row In dtbl.Rows
                row.BeginEdit()
                row("Imageurl") = en.GetBytes(HttpContext.Current.Session("image" & row("AgencyId")))
                row.AcceptChanges()
            Next
            Me.DataSource = dtbl
            picAgencyLogo.Value = "=Imageurl"
               End Sub

    I call report bind method from .aspx page.

    If I use  " picAgencyLogo.Value = "=Imageurl"" with report sub constructor ... I cause error.. "Imageurl is not defined for this context".

    thankx in advance

  9. Ivan
    Admin
    Ivan avatar
    519 posts

    Posted 29 Apr 2008 Link to this post

    Hi manu,

    The exception is raised from the groupHeaderSection1_ItemDataBound subroutine. In it you are trying to get an Image object while the value is of type byte array(byte[]).

    I suppose that the following line is also wrong:

        Imageurl.DataType = System.Type.GetType("System.Byte[]")

    If you are setting an Url for the PictureBox value it is most probably string, not byte array.

    Kind regards,
    Ivan
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  10. Madalin
    Madalin avatar
    1 posts
    Member since:
    Mar 2011

    Posted 07 Mar 2011 Link to this post

    Hi,

    I have an web application (ASP.NET with WCF service and silverlight clients) hosted on Windows Azure. I need to generate reports with images loaded dynamically from an URL (e.g. hostname/Image.aspx?imgID=....).  I do this using Telerik.Reporting and rendering the report as PDF.
    On my local development development machine everything works great and all images appear in the generated report.

    However, when moving the application on production environment, the reports gets generated but each report picture box shows the following error instead of the image:

    An error has occured while processing PictureBox 'pictureBox1':

    The underlying connection was closed: Could not establish trust relationship

    for the SSL/TLS secure channel.


    The web application runs on https with a self-signed certificate. If I try to load in browser same image url as in report, the image gets displayed ok.

    Thank you,
    Madalin Plastoi
  11. Kevin
    Kevin avatar
    2 posts
    Member since:
    Apr 2011

    Posted 03 Aug 2011 Link to this post

    We had the same problem and were able to fix it by adding "127.0.0.1 www.sitename.com" to the host file of the remote server.
  12. Vehbi
    Vehbi avatar
    1 posts
    Member since:
    Feb 2014

    Posted 07 Jan 2015 Link to this post

    An error has occurred while processing PictureBox 'pictureBox1': Could not find a part of the path System.Byte[]. ------------- InnerException ------------- Could not find file . This is happening when I am reading image saved in database as varbinary or image data type. Any solution?
  13. KS
    KS avatar
    5 posts
    Member since:
    Dec 2012

    Posted 12 Jan 2015 in reply to Vehbi Link to this post

    Hi,

    Why it is searching for  a physical path after the image is taken from database? Post the code here.

    -KS
Back to Top