views:

988

answers:

3

Hi,

My name is Ed and i need load image from ReportView dinamic.How i can do this? I work windows forms,c# 3.0 and linq to sql, i need load image to my reports dinamic.

Thanks.

A: 

Short answer is that you cannot do this, at least not with the built in report viewer functions.

However, if you are sure you want to do this, you can attempt to dynamically create RDLC files. If you create your RDLC files dynamically, you can dynamically add images to the reports.

You can find some sample code on how to create RDLC files dynamically here.

Jon
This answer simply isn't correct. It can be done, as Peter Tate's answer shows.
Matthew Talbert
Thanks for pointing that out Matthew. I also fixed the original post for you.
Jon
+1  A: 

I'm assuming that you are using the Microsoft Report Viewer Component from C# and you want to add an image to the report dynamically.

This is certainly possible, you need to create a class with a byte[] property that represents the serialized bitmap.

class ReportImage {
    public byte[] Image {get;set;}

    // Other stuff here if you want...
}

Set the property of this object the a 24 bit per pixel serialized version of your Bitmap (i.e. save your bitmap to a MemoryStream, then call MemoryStream.ToArray()). You must use 24 bits per pixel, and the format you save to must be BMP, this seems to be required in the Report Viewer.

You can then bind to the Object Data Source, (see the MSDN documentation for details on binding to Objects, also see the example here). Use the Image item to display your image in the report.

The limitation is that the images in your report must be fixed size. You'll have to resample the images beforehand to fit them in, or, as Jon suggests, dynamically create the RDLC file for the report.

Peter Tate
A: 

The first answer is very helpful (it got me past having little "broken image" boxes on my report), but a little misleading.

It is, strictly speaking, NOT a requirement that the "image" (which is actually a byte array) be a BMP format. In a test project I was able to read jpeg files from disk (i.e. File.ReadAllBytes(filename); ) and add the resulting byte arrays to a byte[] property in a List of "rptrow" (where rptrow is a object that represents all the data for one row in a report table). The images on the report had the MIMEType set to "image/jpeg" and a Source property of "Database". I also noticed that it did not matter what MIMEType I used as long as something was specified, (i.e. not blank).

I was in a hurry, so I didn't even consider checking the statement that it had to be a 24bpp image.

Simplified rptobj:

public class rptobj
{
    public string FileName { get; set; }
    public byte[] Photo { get; set; }

    private List<rptobj> photos;
    public List<rptobj> GetList()
    {
        if (photos == null)
        {
            photos = LoadPhotos();
        }
        return photos;
    }

    private List<rptobj> LoadPhotos()
    {
        var rslt = new List<rptobj>();
        byte[] rawData;
        var path = HttpContext.Current.Server.MapPath(@"~\images");

        DirectoryInfo di = new DirectoryInfo(path);
        FileSystemInfo[] fis = di.GetFileSystemInfos("*.jpg");

        foreach(var fi in fis){
            rawData = File.ReadAllBytes(string.Format(@"{0}\{1}", path, fi.Name ));
            rslt.Add(new rptobj() { Photo = rawData, FileName = fi.Name });
        }
        return rslt;
    }
}
Randy Kreisel