Documents for Imaging .NET Edition
Features / Create Image
In This Topic
    Create Image
    In This Topic

    An image is a visual representation of information that can be created using a combination of graphics and text. GcImaging allows you to create image(s) programmatically using such graphics, and text. It allows you to create and save images in various image formats such as, JPEG, PNG, BMP, TIFF, and GIF.

    GcImaging provides two main classes, namely GcBitmap and GcBitmapGraphics, that can be used to create image(s). The GcBitmap class represents an uncompressed in-memory bitmap in 32-bit ARGB format. This class provides CreateGraphics method to create graphics for GcBitmap. The CreateGraphics method creates an instance of GcBitmapGraphics class, which lets you draw shapes, graphics, and text to an image. On the other hand, the GcBitmapGraphics class derives from the GcGraphics class and implements a drawing surface for GcBitmap.

    Create Image

    To create an image:

    1. Initialize the GcBitmap class.
    2. Create a drawing surface to draw shapes and render text for an image using CreateGraphics method of the GcBitmap class which returns an instance of the GcBitmapGraphics class.
    3. Draw rounded rectangles and connecting lines in the image using DrawRoundRect and DrawLine methods of the GcBitmapGraphics class respectively.
    4. Apply the background color to the rectangles using FillRoundRect method of the GcBitmapGraphics class.
    5. Initialize the TextFormat class to define the style used to render text in the image.
    6. Add text to the rectangles using DrawString method of the GcBitmapGraphics class.
      C#
      Copy Code
      public void CreateImage(int pixelWidth = 550, int pixelHeight = 350,
          bool opaque = true, float dpiX = 96, float dpiY = 96)
      {        
          //Initialize GcBitmap with the expected height/width
          var bmp = new GcBitmap(pixelWidth, pixelHeight, true, dpiX, dpiY);
      
          //Create graphics for GcBitmap
          using (var g = bmp.CreateGraphics(Color.LightBlue))
          {
              // Rounded rectangle's radii:
              float rx = 36, ry = 24;
      
              //Define text format used to render text in shapes
              var tf = new TextFormat()
              {
                  Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                  FontSize = 18
              };
      
              // Using dedicated methods to draw and fill round rectangles:
              var rec1 = new RectangleF(30, 110, 150, 100);
              g.FillRoundRect(rec1, rx, ry, Color.PaleGreen);
              g.DrawRoundRect(rec1, rx, ry, Color.Blue, 4);
      
              //Draw string within the rectangle
              g.DrawString("Image", tf, rec1, TextAlignment.Center,
                  ParagraphAlignment.Center, false);
      
              var rec2 = new RectangleF(300, 30, 150, 100);
              g.FillRoundRect(rec2, rx, ry, Color.PaleGreen);
              g.DrawRoundRect(rec2, rx, ry, Color.Blue, 4);
      
              //Draw string within the rectangle
              g.DrawString("Text", tf, rec2, TextAlignment.Center,
                  ParagraphAlignment.Center, false);
      
              var rec3 = new RectangleF(300, 230, 220, 100);
              g.FillRoundRect(rec3, rx, ry, Color.PaleGreen);
              g.DrawRoundRect(rec3, rx, ry, Color.Blue, 4);
      
              //Draw string within the rectangle
              g.DrawString("Graphics", tf, rec3, TextAlignment.Center,
                  ParagraphAlignment.Center, false);
      
              //Draw lines between the rectangles
              g.DrawLine(183, 160, 299, 80, Color.Red, 5, DashStyle.Solid);
              g.DrawLine(183, 160, 299, 280, Color.Red, 5, DashStyle.Solid);                      
          }       
          
          //Save GcBitmap to jpeg format
          bmp.SaveAsJpeg("GcImaging.jpeg");
      }
      
    Back to Top

    Create Thumbnail

    GcImaging allows you to create thumbnails of images using Resize method of the GcBitmap class. The Resize method takes InterpolationMode as a parameter to generate the transformed image which is stored as a GcBitmap instance. The interpolation parameter can be set using the InterpolationMode enumeration which specifies the algorithm used to scale images. This affects the way an image stretches or shrinks.

    Original Image Thumbnail
    Image of a marble statue Thumbnail of the image of a marble statue

    To create a thumbnail of an image:

    1. Load an image in a GcBitmap instance.
    2. Determine the height and width for the thumbnail.
    3. Invoke the Resize method of GcBitmap class with thumbnail height, width, and interpolation mode as its parameters.
      C#
      Copy Code
      public void CreateThumbnail(string origImagePath, string thumbImagePath, int thumbWidth, int thumbHeight)
      {
          using (var origBmp = new GcBitmap(origImagePath, null))
          using (var thumbBmp = new GcBitmap(thumbWidth, thumbHeight, true))
          {
              thumbBmp.Clear(Color.White);
              float k = Math.Min((float)thumbWidth / origBmp.PixelWidth, (float)thumbHeight / origBmp.PixelHeight);
              var interpolationMode = k < 0.5f ? InterpolationMode.Downscale : InterpolationMode.Cubic;
              int bmpWidth = (int)(k * origBmp.PixelWidth + 0.5f);
              int bmpHeight = (int)(k * origBmp.PixelHeight + 0.5f);
              using (var bmp = origBmp.Resize(bmpWidth, bmpHeight, interpolationMode))
              {
                  thumbBmp.BitBlt(bmp, (thumbWidth - bmpWidth) / 2, thumbHeight - bmpHeight);
              }
      
              thumbBmp.SaveAsJpeg(thumbImagePath);
          }
      
      }
      

    Back to Top

    For more information about creating images using GcImaging, see GcImaging sample browser.