« WELCOME | Main | IronPython: Stealing Colors with System.Drawing.Bitmap (and yes Visio) »
Monday
May032010

Creating and Saving Bitmaps to Files via C#: GDI+ and Direct2D compared

For all you fans of System.Drawing, here’s a quick example that illustrates how the same task can be done in Direct2D. The examples I found online weren’t very clear, but now placing the code side-by-side, I think you’ll see that it doesn’t seen so alien.

FYI: To compile this sample requires the WindowsAPICodePack: http://code.msdn.microsoft.com/WindowsAPICodePack

CODE COMPARISON

Shown next to each other, below the green regions are section of code that are very similar in structure. The yellow areas highlight those sections that you will find new when moving to Direct2D.

The key points in this sample:

  • Many more namespaces involved
  • 3 “factories” to create
  • Before the drawing begins the BeginDraw() method is called and afterwards EndDraw() method is called

image

 

As you get into more complicated samples, I’m sure you’ll find more differences. But for now, this is a quick way of understanding how a common scenario works across both technologies.

THE FULL SOURCE CODE

using DX = Microsoft.WindowsAPICodePack.DirectX;
using DX_Direct2D1 = Microsoft.WindowsAPICodePack.DirectX.Direct2D1;
using DX_DirectWrite = Microsoft.WindowsAPICodePack.DirectX.DirectWrite;
using DX_WIC = Microsoft.WindowsAPICodePack.DirectX.WindowsImagingComponent;
using SD = System.Drawing;

namespace direct2d
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateBitmapDirect2D();
            CreateBitmapGDIPlus();
        }

        public static void CreateBitmapDirect2D()
        {
            uint bmp_width = 100;
            uint bmp_height = 200;
            var font_family_name = "Calibri";
            var font_size = 25f;
            string text = "HELLO";
            var color_red = new DX_Direct2D1.ColorF(250f, 0f, 0f, 128f);
            var color_blue = new DX_Direct2D1.ColorF(0f, 0f, 255f, 128f);
            var text_rect = new DX_Direct2D1.RectF(0, 0, 100, 200);
            var file_fmt = DX_WIC.ContainerFormats.Png;
            var pix_fmt = DX_WIC.PixelFormats.Pf32bppBGR;

            using (var img_factory = new DX_WIC.ImagingFactory())
            using (var d2d_factory = DX_Direct2D1.D2DFactory.CreateFactory())
            using (var dw_factory = DX_DirectWrite.DWriteFactory.CreateFactory(DX_DirectWrite.DWriteFactoryType.Shared))
            {
                var bmp_cacheopt = DX_WIC.BitmapCreateCacheOption.CacheOnLoad;
                var render_target_props = new DX_Direct2D1.RenderTargetProperties();

                using (var bmp = img_factory.CreateWICBitmap(bmp_width, bmp_height, pix_fmt, bmp_cacheopt))
                using (var bmp_target = d2d_factory.CreateWicBitmapRenderTarget(bmp, render_target_props))
                {
                    bmp_target.BeginDraw();
                    bmp_target.Clear(new DX_Direct2D1.ColorF(0f, 0f, 0f, 0f));

                    using (var redbrush = bmp_target.CreateSolidColorBrush(color_red))
                    using (var bluebrush = bmp_target.CreateSolidColorBrush(color_blue))
                    using (var textformat = dw_factory.CreateTextFormat(font_family_name, font_size))
                    {
                        bmp_target.DrawText(text, textformat, text_rect, redbrush);
                        bmp_target.DrawLine(new DX_Direct2D1.Point2F(0, 0), new DX_Direct2D1.Point2F(100, 100), bluebrush, 3.0f);
                        bmp_target.DrawRectangle(new DX_Direct2D1.RectF(50, 10, 80, 40), redbrush, 1.0f);
                    }

                    bmp_target.EndDraw();
                    bmp.SaveToFile(img_factory, file_fmt, "D:\\demo_direct2d.png");
                }
            }
        }



        public static void CreateBitmapGDIPlus()
        {
            int bmp_width = 100;
            int bmp_height = 200;
            var font_family_name = "Calibri";
            var font_size = 25f;
            string text = "HELLO";
            var color_red = SD.Color.FromArgb(255, 250, 0, 0);
            var color_blue = SD.Color.FromArgb(255, 0, 0, 255);
            var file_fmt = SD.Imaging.ImageFormat.Png;


            using (var bmp = new SD.Bitmap(bmp_width, bmp_height))
            using (var graphics = SD.Graphics.FromImage(bmp))
            {
                graphics.Clear(SD.Color.FromArgb(255, 0, 0, 0));
                using (var redbrush = new SD.SolidBrush(color_red))
                using (var bluepen = new SD.Pen(color_blue, 3.0f))
                using (var redpen = new SD.Pen(color_red, 1.0f))
                using (var font = new SD.Font(font_family_name, font_size))
                {
                    graphics.DrawString(text, font, redbrush, new SD.Rectangle(0, 0, 100, 200));
                    graphics.DrawLine(bluepen, new SD.Point(0, 0), new SD.Point(100, 100));
                    graphics.DrawRectangle(redpen, new SD.Rectangle(50, 10, 80, 40));

                }

                bmp.Save("D:\\demo_gdiplus.png", file_fmt);
            }
        }
    }
}

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>