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)

image

Verify that it renders in preview

image

Good. Now that we have a working report, let’s get some dynamic bitmap content there.

First create a new Data Method

image

Here’s what was created …

image 

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

image

image

Now modify Data Method

[DataMethod(), AxSessionPermission(SecurityAction.Assert)]
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();
        bmp.Save(memstream, System.Drawing.Imaging.ImageFormat.Png);
        var bytearray = memstream.ToArray();
        return bytearray;

    }

}

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

image

As you can see it refers to the an Embedded image with the id “Flowers”

First, we change the Source property

image

 

From Embedded to  Database

image

And then change the Value property from “Flowers”

image

To an “Expression”. click on the Expression option.

image

And the Edit Expression dialog will launch

image

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.

image

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

image

You can now see all the Data Methods in the project. DataMethod1 is the one that creates the bitmap

image 

So select the text in the expression text box

image

And replace it with an expression that calls DataMethod1

image

And click OK

image

And then you’ll see this …

image

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 …

image

Coo. This works.

IMPORTANT TIP

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.

image

First make the header area larger by resizing it …

image

 

 

 image

Now select the edge of the image on the design surface

image

And drag it into the header area

image

You’ll see this …

image

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)

image

And when you preview …

image

At this point should you should have a good handle on what can be done with dynamic generation of bitmaps in reports.

 

SOURCE CODE

You can get it here: http://cid-19ec39cb500669d8.skydrive.live.com/browse.aspx/Public/Dev/SampleCode/Dynamics/SaveenR-Blog-Post-%7C52009-08-06%7C6