In my last post I took a detour from the Data Method topic and explored how to put bitmaps into reports. This post is going I’ll merge the two topics – instead of embedding and image or retrieving it from another source we are going to build a report that creates bitmaps on-the-fly and places them in report designs.
This technique has several uses
- You can generate custom charts & graphics
- You can create images that are context-sensitive (you could for example show the time the report was rendered as a clock, instead of showing it as text in HH:MM format)
- You can make some very nice looking title bars or backgrounds using effects that are not possible from the SSRS reporting tools directly
First, verify that you can create a precision decision that contains an embedded image (you can start with the source code from my previous blog post)
Verify that it renders in preview
Good. Now that we have a working report, let’s get some dynamic bitmap content there.
First create a new Data Method
Here’s what was created …
We are going to change the return type to byte and replace the exception with some simple code that uses things in the System.Drawing namespace. So, first we should add a reference to System.Drawing
In the Solution Explorer, right click on BusinessLogic > References and click Add Reference
Now modify Data Method
public static byte DataMethod1()
using (var bmp = new System.Drawing.Bitmap(600, 200))
var brush_white = System.Drawing.Brushes.White;
using (var g = System.Drawing.Graphics.FromImage(bmp))
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
var f = new System.Drawing.Font("Courier", 36.0f);
g.DrawRectangle(System.Drawing.Pens.Red, 10, 10, 400, 100);
var memstream = new System.IO.MemoryStream();
var bytearray = memstream.ToArray();
All this method does is create a simple bitmap with a red rectangle in it and then returns that bitmap as a byte array.
Build the solution and select the image in the Precision Design and examine its properties
As you can see it refers to the an Embedded image with the id “Flowers”
First, we change the Source property
From Embedded to Database
And then change the Value property from “Flowers”
To an “Expression”. click on the Expression option.
And the Edit Expression dialog will launch
As you can see, it retains the original value of “Flowers”. This is not in valid expression syntax so that is why the red underline appears.
We know we have a datamethod that generates a bitmap so let’s find it. Click on Data Methods in the left-most pane on the bottom
You can now see all the Data Methods in the project. DataMethod1 is the one that creates the bitmap
So select the text in the expression text box
And replace it with an expression that calls DataMethod1
And click OK
And then you’ll see this …
I know. Unsatisfying. When images are generated dynamically via datamethods the design surface doesn’t show a preview of the bitmap.
So, what about preview …
Coo. This works.
An issue you’ll discover when generating bitmaps dynamically is that the preview window doesn’t pick up changes to the bitmap if you simply rewrite the bitmap-generation code or even some changes in the design surface layout even if you click the Refresh button. So to “force” it to pick up the changes I often close the preview window, rebuild the project, and launch the preview window again.
A MORE COMPLICATED DRAWING
I added a new Data Method that creates a more interesting bitmap (the source code for this project includes the code to create this image). This one features some text and three radial gradient fills with transparencies. My intention is to show you how to use this as a header for a report.
First make the header area larger by resizing it …
Now select the edge of the image on the design surface
And drag it into the header area
You’ll see this …
And position it toward the top of the report. (I also changed the size of the image on the design surface but that is not important)
And when you preview …
At this point should you should have a good handle on what can be done with dynamic generation of bitmaps in reports.