[Sample of Feb 22nd] Using Direct2D for Server-Side Rendering with ASP.NET - Microsoft All-In-One Code Framework - Site Home - MSDN Blogs
Microsoft All-In-One Code Framework - Developers' Pain Killer

[Sample of Feb 22nd] Using Direct2D for Server-Side Rendering with ASP.NET

Rate This
  • Comments 1

 

Homepage image
RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSD2DServerSideRendering-2d099ab6 

Today’s code sample demonstrates how to use Direct2D to render images in ASP.NET web applications.  Some server applications need to render images and send back the generated bitmaps to users connected through web clients.  GDI+ and System.Drawing are not supported in a service or web application.  Direct2D is the appropriate technology for rendering images to a bitmap on disk in a service context.  Direct2D is completely supported in the context of a service.   If your server side application e.g. ASP.NET web app or Windows Service app needs to generate or render images, this code sample will reduce your coding effort and avoid the inappropriate use of GDI+ and System.Drawing classes.

The sample was provided by Microsoft Senior Escalation Engineer Greg Binkerd.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

Some server applications need to render images and send back the generated bitmaps to users connected through web clients.  GDI+ and System.Drawing are not supported in a service or web application.  Direct2D is the appropriate technology for rendering images to a bitmap on disk in a service context.  Direct2D is completely supported in the context of a service.  This is a C# ASP.NET sample which demonstrates how to render a Direct2D scene to an image file on disk.  It demonstrates how to create a Direct2D bitmap based on image data in memory, draw Direct2D objects, such as an Ellipse and a Rectangle, and it also demonstrates how to use DirectWrite to render text.  The sample uses the Direct2D and DirectWrite assemblies from the Windows API Code Pack for Microsoft .NET Framework.  These provide developers access to Direct2D and DirectWrite from managed code.

 

Run-time requirements

In order to gain the support of Direct2D, the sample must be run on these platforms:

Windows 7 or Windows Vista with Service Pack 2 (SP2) and Platform Update for Windows Vista
Windows Server 2008 R2 or Windows Server 2008 with Service Pack 2 (SP2) and Platform Update for Windows Server 2008 

 

Running the Sample

This code demonstrates how to use Direct2D to render 2D objects and text to an image file on disk.  You will need to grant permission to the "Network Service" user account for the folder on the server that you wish to save the image file to.  You can set that location in this portion of the code:

wicBitmap.SaveToFile(wicFactory, ContainerFormats.Png, "c:\\SampleFiles\\Images\\test.png");

Execute the sample and then press the “Create Image File” button.  You will now see your Direct2D scene rendered to the PNG file on disk (at the server where the code is executed):

image

 

Using the Code

Server side applications often need to render images and send back the generated bitmaps to users connected through web clients. The problem is that GDI+ and System.Drawing are not supported in a service or web application.  Direct2D is supported but there are few samples which demonstrate how to use Direct2D from C# and also create a bitmap file on disk of a Direct2D scene.  My hope is that many web developers will be able to use this sample to convert their (problematic) System.Drawing code to Direct2D.  One example would be a shipping label that is generated server side and then sent to the client for use.  You can call DrawText() as follows to render text on the shipping label:

wicRenderTarget.DrawText("This is a test", textFormat_Value, renderText_Value, blackSolidColorBrush);

Look at the CreateBitmap() method to see how I create a Direct2D bitmap based on image data in memory.  This creates a Bitmap which you can render to your Direct2D scene, such as a barcode, for example.  You can draw this barcode Bitmap on the shipping label with the following code:

RectF DrawBitmap_Rect; 
DrawBitmap_Rect = new RectF(10, 275, 180, 350); 
wicRenderTarget.DrawBitmap(d2dBitmapBarcode, 1.0f, BitmapInterpolationMode.NearestNeighbor, DrawBitmap_Rect);

Once you have finished rendering your Direct2D objects to memory, then you can call the following method to save the image to a file on disk:

wicBitmap.SaveToFile(wicFactory, ContainerFormats.Png, "c:\\SampleFiles\\Images\\test.png");
 

More Information

For more information on DirectWrite, see the MSDN DirectWrite documentation:   http://msdn.microsoft.com/en-us/library/windows/desktop/dd368038(v=vs.85).aspx

For more information on DirectWrite, see the MSDN DirectWrite documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368038(v=vs.85).aspx

For more information on the Windows API Code Pack for Microsoft .NET Framework, see: http://archive.msdn.microsoft.com/WindowsAPICodePack

Leave a Comment
  • Please add 6 and 7 and type the answer here:
  • Post
  • All sounds great except the graphics hardware is not available to services on Win2K8 due to session 0 isolation. This is a real drawback to using Windows for server side rendering.

Page 1 of 1 (1 items)