如何在Microsoft报表中显示来自字节数组的图像

时间:2021-11-27 21:18:20

I am using a Report file and a ReportViewer control to show a report which loads data dynamically from objects during run-time.

我正在使用Report文件和ReportViewer控件来显示在运行时从对象动态加载数据的报表。

I need to show an image which is stored as a byte array in the object.

我需要显示一个图像,该图像作为字节数组存储在对象中。

The PictureBox's value is currently set to:

PictureBox的值当前设置为:

=First(Fields!ImageData.Value, "dtstItemImage")

And I set the DataSource using:

我使用以下方法设置DataSource:

ImageBindingSource.DataSource = this.item.Image.ImageData;

The code compiles and runs but the image is not displayed in the report.

代码编译并运行,但图像不会显示在报告中。

Is this because the PictureBox needs to be bound to an Image object (and not to a byte array)? Or are there perhaps some properties of the PictureBox which I need to set?

这是因为PictureBox需要绑定到Image对象(而不是字节数组)吗?或者我可能需要设置PictureBox的一些属性?

UPDATE 1

更新1

I've added a border to the PictureBox just to make sure that's its visible and it does show up in the report. It just doesn't contain the image.

我已经为PictureBox添加了一个边框,只是为了确保它是可见的,它确实显示在报告中。它只是不包含图像。

UPDATE 2

更新2

I've fixed a mistake in my code. I've changed:

我在我的代码中修正了一个错误。我改变了:

ImageBindingSource.DataSource = this.item.Image.ImageData;

to:

至:

ImageBindingSource.DataSource = this.item.Image;

as the PictureBox is bound to the ImageData field BUT the DataSource is the Image object.

因为PictureBox绑定到ImageData字段但是DataSource是Image对象。

Now I get a small cross icon instead of nothing which (at least for me) indicates some progress but I don't know where the byte[]-bitmap conversion code needs to be.

现在我得到一个小十字图标而不是什么(至少对我来说)表示一些进展,但我不知道字节[] - 位图转换代码需要在哪里。

2 个解决方案

#1


3  

I managed to solve this by setting the report's Image box Source property to Database (it was previously set to External).

我设法通过将报告的图像框源属性设置为数据库(它以前设置为外部)来解决此问题。

More info about the different available Source values can be found at (MSDN) HowTo: Add an Image (Reporting Services).

有关不同可用源值的详细信息,请参阅(MSDN)HowTo:添加图像(Reporting Services)。

#2


1  

You need to create an image object from the byte array and use that as the source.

您需要从字节数组创建一个图像对象,并将其用作源。

To do this, you can use a helper function like the following

为此,您可以使用如下的辅助函数

public static Image LoadImage(byte[] imageBytes)
{
     Image image = null;
     using (var ms = new MemoryStream(imageBytes))
         image = Image.FromStream(ms);

     return image;
}

Edit

编辑

For WPF, you need to use BitmapSource (MSDN) instead of Image (MSDN)

对于WPF,您需要使用BitmapSource(MSDN)而不是Image(MSDN)

public static BitmapSource LoadImage(Byte[] imageBytes)
{
    var image = new BitmapImage();
    using (var ms = new MemoryStream(binaryData))
    {
        image.BeginInit();
        image.StreamSource = ms;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.EndInit();
    }

    if (image.CanFreeze)
        image.Freeze();

    return image;
}

NB: You can could also do this using a IValueConverter, see this blog post for the source code.

注意:您也可以使用IValueConverter执行此操作,请参阅此博客文章以获取源代码。


and then modify your data binding

然后修改您的数据绑定

ImageBindingSource.DataSource = LoadImage(item.Image.ImageData);

...

...

Make sure that the image (and MemoryStream) is disposed properly when you finished with it, as otherwise you will leak memory.

完成后确保图像(和MemoryStream)正确放置,否则会泄漏内存。

Also, depending on the format of your byte array you may need to do some work. See one of my question/answers for some helpful information.

此外,根据您的字节数组的格式,您可能需要做一些工作。请参阅我的一个问题/答案以获取一些有用的信息。

#1


3  

I managed to solve this by setting the report's Image box Source property to Database (it was previously set to External).

我设法通过将报告的图像框源属性设置为数据库(它以前设置为外部)来解决此问题。

More info about the different available Source values can be found at (MSDN) HowTo: Add an Image (Reporting Services).

有关不同可用源值的详细信息,请参阅(MSDN)HowTo:添加图像(Reporting Services)。

#2


1  

You need to create an image object from the byte array and use that as the source.

您需要从字节数组创建一个图像对象,并将其用作源。

To do this, you can use a helper function like the following

为此,您可以使用如下的辅助函数

public static Image LoadImage(byte[] imageBytes)
{
     Image image = null;
     using (var ms = new MemoryStream(imageBytes))
         image = Image.FromStream(ms);

     return image;
}

Edit

编辑

For WPF, you need to use BitmapSource (MSDN) instead of Image (MSDN)

对于WPF,您需要使用BitmapSource(MSDN)而不是Image(MSDN)

public static BitmapSource LoadImage(Byte[] imageBytes)
{
    var image = new BitmapImage();
    using (var ms = new MemoryStream(binaryData))
    {
        image.BeginInit();
        image.StreamSource = ms;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.EndInit();
    }

    if (image.CanFreeze)
        image.Freeze();

    return image;
}

NB: You can could also do this using a IValueConverter, see this blog post for the source code.

注意:您也可以使用IValueConverter执行此操作,请参阅此博客文章以获取源代码。


and then modify your data binding

然后修改您的数据绑定

ImageBindingSource.DataSource = LoadImage(item.Image.ImageData);

...

...

Make sure that the image (and MemoryStream) is disposed properly when you finished with it, as otherwise you will leak memory.

完成后确保图像(和MemoryStream)正确放置,否则会泄漏内存。

Also, depending on the format of your byte array you may need to do some work. See one of my question/answers for some helpful information.

此外,根据您的字节数组的格式,您可能需要做一些工作。请参阅我的一个问题/答案以获取一些有用的信息。