Tuesday, August 30, 2005

Dynamically Generating Images in ASP .NET

In .NET it is very easy to output a raw image from a web page instead of the default HTML. All you have to do is change the content type and stream the image as binary data to the response:

    protected void Page_Load(object sender, EventArgs e)
        Bitmap bitmap = new Bitmap(320, 240);

        MemoryStream memoryStream = new MemoryStream();
        bitmap.Save(memoryStream, ImageFormat.Png);

        Response.ContentType = "image/png";

The above code will stream an empty image in the PNG format to the caller of this page. The image can either be rendered dynamically or read from a database, the sky is the limit. By using request parameters the image rendering can easily be customized.

The resulting image page can be embedded on other pages on the web site, either statically by referencing it in a HTML image element or dynamically by using the ASP .NET image server control. In the latter case you just assign the URL of the image page to the ImageUrl property of the control.

Monday, August 29, 2005

DataGridView Rich Text Column

A lot of applications need to display formatted text in a table. Out of the box the DataGridView only supports a TextBox column type for which the formatting options are quite limited. As the cells within a DataGridView are no standard user controls it is no trivial task to implement. I implemented a RichTextBox column type based on the following Microsoft knowledge base article:

     Print the Content of a RichTextBox

This article shows how to render the RichTextBox content on a printer device context using the EM_FORMATRANGE message. Instead of the printer I let this code render the content on a device context for a Bitmap:

    Image LimgImage = ...
    Graphics LobjGraphics = Graphics.FromImage(LimgImage);
    IntPtr LobjHdc = LobjGraphics.GetHdc();

This image is then rendered in the Paint event of the cell.

In my application it was furthermore necessary to edit the content in the cells. For this I used the default RichTextBox. It required quite some time to get the sizing right so the image rendered in display mode exactly fitted the content of the RichTextBox used to enter the content, but at the end all the magic numbers involved were :-)

Tuesday, August 23, 2005

Searching Generic Collections

When you find something it is always in the last place you were searching for it. Searching through generic collections using anonymous delegates however has never been as much fun. Consider a list of type Person, now take a look at the following line of code:

List<Person> youngPersons = persons.FindAll(YoungerThan(18));

Now this is what I call self explaining code without being too specific.

The YoungerThan function returns a predicate that will test an item in the collection and returns a Boolean value indicating the truth of the predicate. It is very cool that the predicate delegate can be parameterized, for instance in the YoungerThan implementation:

private Predicate<Person> YoungerThan(int age)
     Predicate<Person> youngerThan = delegate(Person person)
          return person.Age < age;

     return youngerThan;

The function can either be implemented in the class in which you use it or as part of the class to be searched. In the latter case you would probably implement it as a static function.

Monday, August 22, 2005

Autoscrolling Panel Bug

The current implementation of the Panel control contains a bug in the autoscrolling code. Whenever the panel control gets the focus it will scroll the active subcontrol in to view. This bug is acknowledged by Microsoft but, because it is “previously shipped behavior”, it will not be resolved in the next Whidbey release :-(

See also: Bug Details: AutoScroll positon gets losted

Overriding this behavior was not easy as a lot of methods involved in the autoscrolling are not virtual. After some hacking we found a “workaround” for the bug by always scrolling to the current position, which effectively does nothing.

public class MyPanel : Panel
    protected override Point ScrollToControl(Control PobjActiveControl)
        return AutoScrollPosition;

This however not only disables the bug behavior but almost all the autoscrolling behavior too. Therefore the desired autoscrolling behavior, for instance when the user tabs through the panel’s subcontrols, has to be coded manually. Luckily for my project this was already in place althought it only requires a few lines of code to accomplish.

Friday, August 19, 2005

Recustomize Word Document

Removing the customization from a Word document not only removes the application manifest but also the cached data. Althought it is well documented by Microsoft it still bit me.


For a solution it was necessary to change the reference to the customization assembly within the document. I did not want to store any data in the original document as it might contain sensitive information. I therefore created a copy of the document in the temporary folder of the current user. As I did not want to copy the customization assembly to the temporary folder and also did not want to hardcode the application path the only option left was to "recustomize" the document to reference the customization assembly in the original application folder.

/// <summary>
/// This function will recustomize the given word document
/// with the given customization assembly
/// </summary>
/// <param name="PstrDocumentFileName">
/// The name of the Word document
/// </param>
/// <param name="PstrCustomizationAssemblyFileName">
/// The customization assembly
/// </param>
private static void RecustomizeDocument(
    string PstrDocumentFileName,
    string PstrCustomizationAssemblyFileName)
    // Remove the old customization
    if (ServerDocument.IsCustomized(PstrDocumentFileName))

    // Add the new customization
    Assembly LobjCustomizationAssembly =
    string LstrCustomizationAssemblyVersion =

    bool LcbMakePathsRelative = false;
    const string LcstrDeploymentManifestPath = "";

This page is powered by Blogger. Isn't yours?