Creating high-resolution images

When creating in-a memory bitmaps using GDI+ the system will assume a bitmap of the standard resolution of 72 DPI.  This is usually fine for images that you would display on screen but is often too low resolution for printing and if you decide to use medium-sized fonts of between ten to sixteen points the text quality is very low and can be pixilated. 

After an image has been created however, you can set the resolution of the image to whatever value you desire and then, when drawing on this image using the Graphics.FromImage method you can specify higher resolution graphics and text by using one of the real-world page unit systems as opposed to pixels. 

The following snippet of code creates a bitmap of 3000 by 2000 pixels and sets its resolution To 600 by 300.  This is not a standard resolution however this example shows quite well how programs that know how to deal with declared image resolutions will indeed take notice of the image parameters and display the picture correctly. 

       //Create a high resolution image...

      Bitmap bm=new Bitmap(3000,2000);

      bm.SetResolution(600,300);

      //Get a Graphics for it

      Graphics g=Graphics.FromImage(bm);

      //set up the graphics.

      g.PageUnit=GraphicsUnit.Point;

      //clear to white.

      g.Clear(Color.White);

      //Create a 14 point font..

      Font fn=new Font("Verdana",14,GraphicsUnit.Point);

      //output text

      g.DrawString(

        "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer dignissim, nisl et imperdiet blandit, dolor dolor molestie lorem, id dictum tortor nisl vel erat. Nullam rutrum eleifend lorem. Nam dolor nibh, fringilla ut, laoreet ut, molestie non, massa. Phasellus eget justo. In dignissim. Nunc nunc purus, sagittis vel, tempor in, ullamcorper non, tortor. Quisque tortor urna, accumsan id, tincidunt eget, cursus ut, turpis. Mauris risus dolor, faucibus vel, posuere non, pharetra id, justo. Nunc pharetra nibh sit amet dolor. Curabitur eu massa vitae felis aliquam mollis. Curabitur in erat at odio scelerisque pulvinar. Suspendisse arcu. Donec eget ipsum. Etiam pulvinar neque et mi. Duis tempus adipiscing lorem. Nulla arcu est, vehicula a, tincidunt sit amet, ornare ut, eros. Aliquam eu est nec neque aliquet dapibus.",

        fn,

        Brushes.Black,

        new RectangleF(0,0,72*5,72*5),

        StringFormat.GenericTypographic);

 

      //save high-res image

 

      bm.Save(@"c:\highrestext.bmp",ImageFormat.Bmp);

 

This code, produces a 23 megabytes image so it's not available to view on this site. You'll need to run the code on your own machine to view it.

When viewed with a reasonably unintelligent programs such as Microsoft Paint that cares only about editing the pixels, the image is obviously distorted and disproportionately large.  When displayed in a program that understands that the image resolution is an important part of the picture, such as Microsoft Word, the picture is displayed at it's intended size and when the page is shown at 100% scale the text upon it is indeed fourteen point type.

The only thing that it's important to remember when creating such bitmaps of is that when you originally construct the image you must remember to calculate the effective size in pixels according to your desired resolution. 

 

Return to the GDI+ FAQ.