How to show image from byte array in Microsoft report

I use a report file and a ReportViewer control to display a report that dynamically loads data from objects at runtime.

I need to show an image that is stored in a byte array in an object.

PictureBox is currently set to:

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

And I installed DataSource using:

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

The code compiles and runs, but the image does not appear in the report .

Is it because the PictureBox needs to be bound to the Image object (and not to the byte array)? Or maybe there are some PictureBox properties that I need to set?

UPDATE 1

I added a border to the PictureBox to make sure its visibility and that it is displayed in the report. It just does not contain an image.

UPDATE 2

I fixed a bug in my code. I changed:

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

in

 ImageBindingSource.DataSource = this.item.Image; 

since the PictureBox is bound to the ImageData field, but the DataSource is an Image object.

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 [] conversion code of the bitmap should be.

+6
source share
2 answers

I was able to solve this problem by setting the Image Source parameter to the value of the Database report (it was previously set to External ).

See the (MSDN) for more information on the various available initial values . HowTo: Add Image (Reporting Services) .

+3
source

You need to create an image object from an array of bytes and use it as a source.

For this you can use a helper function like

 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 )

 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 also do this with IValueConverter , see this blog post for source code .


and then change the data binding

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

...

Make sure that the image (and the MemoryStream ) is correctly set when you are done with it, otherwise you will leak the memory.

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

+1
source

Source: https://habr.com/ru/post/924853/


All Articles