Document Solutions for Imaging
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. DsImaging 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, SVG, ICO, GIF and WebP.

    DsImaging 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("Image.jpeg");
      }
      
    Back to Top

    Create SVG Image using Code

    To create an SVG image using code:

    1. Create a new SVG document by creating an instance of GcSvgDocument.
    2. Create an instance of SvgPathBuilder class. This class provides methods to execute the path commands.
    3. Define the path to draw the outline of shape to be drawn on SVG using methods such as AddMoveTo and AddCurveTo.
    4. Add these elements into root collection of 'svg' element using the Add method.
    5. Provide the SvgPathData using ToPathData method of the SvgPathBuilder class which represents sequence of instructions for drawing the path.
    6. Define other properties of each path such as, Fill, Stroke etc.
    7. Save the document as SVG by using Save method of the GcSvgDocument class.
    8. To save the SVG as image, use the DrawSvg method of the GcBitmapGraphics class.
    C#
    Copy Code
    public static GcSvgDocument DrawCarrot()
    {
        // Create a new SVG document
        var doc = new GcSvgDocument();
        var svg = doc.RootSvg;
        svg.ViewBox = new SvgViewBox(0, 0, 313.666f, 164.519f);
    
        //Create an instance of SvgPathBuilder class. 
        var pb = new SvgPathBuilder();
    
        //Define the path
        pb.AddMoveTo(false, 29.649f, 9.683f);
        pb.AddCurveTo(true, -2.389f, -0.468f, -4.797f, 2.57f, -6.137f, 5.697f);
        pb.AddCurveTo(true, 2.075f, -2.255f, 3.596f, -1.051f, 4.915f, -0.675f);
        pb.AddCurveTo(true, -2.122f, 2.795f, -4f, 5.877f, -7.746f, 5.568f);
        pb.AddCurveTo(true, 2.384f, -6.014f, 2.963f, -12.977f, 0.394f, -17.78f);
        pb.AddCurveTo(true, -1.296f, 2.591f, -1.854f, 6.054f, -5.204f, 7.395f);
        pb.AddCurveTo(true, 3.575f, 2.455f, 0.986f, 7.637f, 1.208f, 11.437f);
        pb.AddCurveTo(false, 11.967f, 21.17f, 6.428f, 16.391f, 9.058f, 10.67f);
        pb.AddCurveTo(true, -3.922f, 8.312f, -2.715f, 19.745f, 4.363f, 22.224f);
        pb.AddCurveTo(true, -3.86f, 4.265f, -2.204f, 10.343f, 0.209f, 13.781f);
        pb.AddCurveTo(true, -0.96f, 1.808f, -1.83f, 2.546f, -3.774f, 3.195f);
        pb.AddCurveTo(true, 3.376f, 1.628f, 6.612f, 4.866f, 11.326f, 3.366f);
        pb.AddCurveTo(true, -1.005f, 2.345f, -12.389f, 9.499f, -15.16f, 10.35f);
        pb.AddCurveTo(true, 3.216f, 0.267f, 14.492f, -2.308f, 16.903f, -5.349f);
        pb.AddCurveTo(true, -1.583f, 2.84f, 1.431f, 2.28f, 2.86f, 4.56f);
        pb.AddCurveTo(true, 1.877f, -3.088f, 3.978f, -2.374f, 5.677f, -3.311f);
        pb.AddCurveTo(true, -0.406f, 4.826f, -2.12f, 9.27f, -5.447f, 13.582f);
        pb.AddCurveTo(true, 2.834f, -4.894f, 6.922f, -5.367f, 10.474f, -5.879f);
        pb.AddCurveTo(true, -0.893f, 4.245f, -3.146f, 8.646f, -7.077f, 10.479f);
        pb.AddCurveTo(true, 5.359f, 0.445f, 11.123f, -3.934f, 13.509f, -9.944f);
        pb.AddCurveTo(true, 12.688f, 3.209f, 28.763f, -1.932f, 39.894f, 7.084f);
        pb.AddCurveTo(true, 1.024f, 0.625f, 1.761f, -4.98f, 1.023f, -5.852f);
        pb.AddCurveTo(false, 72.823f, 55.357f, 69.273f, 68.83f, 52.651f, 54.498f);
        pb.AddCurveTo(true, -0.492f, -0.584f, 1.563f, -5.81f, 1f, -8.825f);
        pb.AddCurveTo(true, -1.048f, -3.596f, -3.799f, -6.249f, -7.594f, -6.027f);
        pb.AddCurveTo(true, -2.191f, 0.361f, -5.448f, 0.631f, -7.84f, 0.159f);
        pb.AddCurveTo(true, 2.923f, -5.961f, 9.848f, -4.849f, 12.28f, -11.396f);
        pb.AddCurveTo(true, -4.759f, 2.039f, -7.864f, -2.808f, -12.329f, -1.018f);
        pb.AddCurveTo(true, 1.63f, -3.377f, 4.557f, -2.863f, 6.786f, -3.755f);
        pb.AddCurveTo(true, -3.817f, -2.746f, -9.295f, -5.091f, -14.56f, -0.129f);
        pb.AddCurveTo(false, 33.228f, 18.615f, 32.064f, 13.119f, 29.649f, 9.683f);
    
        //Add elements into Children collection of SVG 
        svg.Children.Add(new SvgPathElement()
        {
            FillRule = SvgFillRule.EvenOdd,
            Fill = new SvgPaint(Color.FromArgb(0x43, 0x95, 0x39)),
            PathData = pb.ToPathData(),
        });
    
        pb.Reset();
        pb.AddMoveTo(false, 29.649f, 9.683f);
        pb.AddCurveTo(true, -2.389f, -0.468f, -4.797f, 2.57f, -6.137f, 5.697f);
        pb.AddCurveTo(true, 2.075f, -2.255f, 3.596f, -1.051f, 4.915f, -0.675f);
        pb.AddCurveTo(true, -2.122f, 2.795f, -4f, 5.877f, -7.746f, 5.568f);
        pb.AddCurveTo(true, 2.384f, -6.014f, 2.963f, -12.977f, 0.394f, -17.78f);
        pb.AddCurveTo(true, -1.296f, 2.591f, -1.854f, 6.054f, -5.204f, 7.395f);
        pb.AddCurveTo(true, 3.575f, 2.455f, 0.986f, 7.637f, 1.208f, 11.437f);
        pb.AddCurveTo(false, 11.967f, 21.17f, 6.428f, 16.391f, 9.058f, 10.67f);
        pb.AddCurveTo(true, -3.922f, 8.312f, -2.715f, 19.745f, 4.363f, 22.224f);
        pb.AddCurveTo(true, -3.86f, 4.265f, -2.204f, 10.343f, 0.209f, 13.781f);
        pb.AddCurveTo(true, -0.96f, 1.808f, -1.83f, 2.546f, -3.774f, 3.195f);
        pb.AddCurveTo(true, 3.376f, 1.628f, 6.612f, 4.866f, 11.326f, 3.366f);
        pb.AddCurveTo(true, -1.005f, 2.345f, -12.389f, 9.499f, -15.16f, 10.35f);
        pb.AddCurveTo(true, 3.216f, 0.267f, 14.492f, -2.308f, 16.903f, -5.349f);
        pb.AddCurveTo(true, -1.583f, 2.84f, 1.431f, 2.28f, 2.86f, 4.56f);
        pb.AddCurveTo(true, 1.877f, -3.088f, 3.978f, -2.374f, 5.677f, -3.311f);
        pb.AddCurveTo(true, -0.406f, 4.826f, -2.12f, 9.27f, -5.447f, 13.582f);
        pb.AddCurveTo(true, 2.834f, -4.894f, 6.922f, -5.367f, 10.474f, -5.879f);
        pb.AddCurveTo(true, -0.893f, 4.245f, -3.146f, 8.646f, -7.077f, 10.479f);
        pb.AddCurveTo(true, 5.359f, 0.445f, 11.123f, -3.934f, 13.509f, -9.944f);
        pb.AddCurveTo(true, 12.688f, 3.209f, 28.763f, -1.932f, 39.894f, 7.084f);
        pb.AddCurveTo(true, 1.024f, 0.625f, 1.761f, -4.98f, 1.023f, -5.852f);
        pb.AddCurveTo(false, 72.823f, 55.357f, 69.273f, 68.83f, 52.651f, 54.498f);
        pb.AddCurveTo(true, -0.492f, -0.584f, 1.563f, -5.81f, 1f, -8.825f);
        pb.AddCurveTo(true, -1.048f, -3.596f, -3.799f, -6.249f, -7.594f, -6.027f);
        pb.AddCurveTo(true, -2.191f, 0.361f, -5.448f, 0.631f, -7.84f, 0.159f);
        pb.AddCurveTo(true, 2.923f, -5.961f, 9.848f, -4.849f, 12.28f, -11.396f);
        pb.AddCurveTo(true, -4.759f, 2.039f, -7.864f, -2.808f, -12.329f, -1.018f);
        pb.AddCurveTo(true, 1.63f, -3.377f, 4.557f, -2.863f, 6.786f, -3.755f);
        pb.AddCurveTo(true, -3.817f, -2.746f, -9.295f, -5.091f, -14.56f, -0.129f);
        pb.AddCurveTo(false, 33.228f, 18.615f, 32.064f, 13.119f, 29.649f, 9.683f);
        pb.AddClosePath();
        //Add elements into Children collection of SVG 
        svg.Children.Add(new SvgPathElement()
        {
            Fill = SvgPaint.None,
            Stroke = new SvgPaint(Color.Black),
            StrokeWidth = new SvgLength(2.292f),
            StrokeMiterLimit = 14.3f,
            PathData = pb.ToPathData(),
        });
    
        pb.Reset();
        pb.AddMoveTo(false, 85.989f, 101.047f);
        pb.AddCurveTo(true, 0f, 0f, 3.202f, 3.67f, 8.536f, 4.673f);
        pb.AddCurveTo(true, 7.828f, 1.472f, 17.269f, 0.936f, 17.269f, 0.936f);
        pb.AddCurveTo(true, 0f, 0f, 2.546f, 5.166f, 10.787f, 7.338f);
        pb.AddCurveTo(true, 8.248f, 2.168f, 17.802f, 0.484f, 17.802f, 0.484f);
        pb.AddCurveTo(true, 0f, 0f, 8.781f, 1.722f, 19.654f, 8.074f);
        pb.AddCurveTo(true, 10.871f, 6.353f, 20.142f, 2.163f, 20.142f, 2.163f);
        pb.AddCurveTo(true, 0f, 0f, 1.722f, 3.118f, 14.11f, 9.102f);
        pb.AddCurveTo(true, 12.39f, 5.982f, 14.152f, 2.658f, 28.387f, 4.339f);
        pb.AddCurveTo(true, 14.232f, 1.672f, 19.36f, 5.568f, 30.108f, 7.449f);
        pb.AddCurveTo(true, 10.747f, 1.886f, 25.801f, 5.607f, 25.801f, 5.607f);
        pb.AddCurveTo(true, 0f, 0f, 4.925f, 0.409f, 12.313f, 6.967f);
        pb.AddCurveTo(true, 7.381f, 6.564f, 18.453f, 4.506f, 18.453f, 4.506f);
        pb.AddCurveTo(true, 0f, 0f, -10.869f, -6.352f, -15.467f, -10.702f);
        pb.AddCurveTo(true, -4.594f, -4.342f, -16.901f, -11.309f, -24.984f, -15.448f);
        pb.AddCurveTo(true, -8.079f, -4.14f, -18.215f, -7.46f, -30.233f, -11.924f);
        pb.AddCurveTo(true, -12.018f, -4.468f, -6.934f, -6.029f, -23.632f, -13.855f);
        pb.AddCurveTo(true, -16.695f, -7.822f, -13.662f, -8.565f, -28.347f, -10.776f);
        pb.AddCurveTo(true, -14.686f, -2.208f, -6.444f, -11.933f, -23.917f, -16.356f);
        pb.AddCurveTo(true, -17.479f, -4.423f, -11.037f, -4.382f, -26.016f, -9.093f);
        pb.AddCurveTo(true, -14.97f, -4.715f, -10.638f, -10.104f, -26.665f, -13.116f);
        pb.AddCurveTo(true, -14.149f, -2.66f, -21.318f, 0.468f, -27.722f, 11.581f);
        pb.AddCurveTo(false, 73.104f, 89.075f, 85.989f, 101.047f, 85.989f, 101.047f);
        // Add elements into Children collection of SVG 
        svg.Children.Add(new SvgPathElement()
        {
            FillRule = SvgFillRule.EvenOdd,
            Fill = new SvgPaint(Color.FromArgb(0xFF, 0xC2, 0x22)),
            PathData = pb.ToPathData(),
        });
    
        pb.Reset();
        pb.AddMoveTo(false, 221.771f, 126.738f);
        pb.AddCurveTo(true, 0f, 0f, 1.874f, -4.211f, 4.215f, -6.087f);
        pb.AddCurveTo(true, 2.347f, -1.868f, 2.812f, -2.339f, 2.812f, -2.339f);
        pb.AddMoveTo(false, 147.11f, 105.122f);
        pb.AddCurveTo(true, 0f, 0f, 0.882f, -11.047f, 6.765f, -15.793f);
        pb.AddCurveTo(true, 5.879f, -4.745f, 10.882f, -5.568f, 10.882f, -5.568f);
        pb.AddMoveTo(false, 125.391f, 86.008f);
        pb.AddCurveTo(true, 0f, 0f, 2.797f, -6.289f, 6.291f, -9.081f);
        pb.AddCurveTo(true, 3.495f, -2.791f, 4.194f, -3.49f, 4.194f, -3.49f);
        pb.AddMoveTo(false, 181.153f, 124.8f);
        pb.AddCurveTo(true, 0f, 0f, -1.206f, -4.014f, -0.709f, -6.671f);
        pb.AddCurveTo(true, 0.493f, -2.66f, 0.539f, -3.256f, 0.539f, -3.256f);
        pb.AddMoveTo(false, 111.704f, 107.641f);
        pb.AddCurveTo(true, 0f, 0f, -1.935f, -6.604f, -1.076f, -10.991f);
        pb.AddCurveTo(true, 0.862f, -4.389f, 0.942f, -5.376f, 0.942f, -5.376f);
        pb.AddMoveTo(false, 85.989f, 101.047f);
        pb.AddCurveTo(true, 0f, 0f, 3.202f, 3.67f, 8.536f, 4.673f);
        pb.AddCurveTo(true, 7.828f, 1.472f, 17.269f, 0.936f, 17.269f, 0.936f);
        pb.AddCurveTo(true, 0f, 0f, 2.546f, 5.166f, 10.787f, 7.338f);
        pb.AddCurveTo(true, 8.248f, 2.168f, 17.802f, 0.484f, 17.802f, 0.484f);
        pb.AddCurveTo(true, 0f, 0f, 8.781f, 1.722f, 19.654f, 8.074f);
        pb.AddCurveTo(true, 10.871f, 6.353f, 20.142f, 2.163f, 20.142f, 2.163f);
        pb.AddCurveTo(true, 0f, 0f, 1.722f, 3.118f, 14.11f, 9.102f);
        pb.AddCurveTo(true, 12.39f, 5.982f, 14.152f, 2.658f, 28.387f, 4.339f);
        pb.AddCurveTo(true, 14.232f, 1.672f, 19.36f, 5.568f, 30.108f, 7.449f);
        pb.AddCurveTo(true, 10.747f, 1.886f, 25.801f, 5.607f, 25.801f, 5.607f);
        pb.AddCurveTo(true, 0f, 0f, 4.925f, 0.409f, 12.313f, 6.967f);
        pb.AddCurveTo(true, 7.381f, 6.564f, 18.453f, 4.506f, 18.453f, 4.506f);
        pb.AddCurveTo(true, 0f, 0f, -10.869f, -6.352f, -15.467f, -10.702f);
        pb.AddCurveTo(true, -4.594f, -4.342f, -16.901f, -11.309f, -24.984f, -15.448f);
        pb.AddCurveTo(true, -8.079f, -4.14f, -18.215f, -7.46f, -30.233f, -11.924f);
        pb.AddCurveTo(true, -12.018f, -4.468f, -6.934f, -6.029f, -23.632f, -13.855f);
        pb.AddCurveTo(true, -16.695f, -7.822f, -13.662f, -8.565f, -28.347f, -10.776f);
        pb.AddCurveTo(true, -14.686f, -2.208f, -6.444f, -11.933f, -23.917f, -16.356f);
        pb.AddCurveTo(true, -17.479f, -4.423f, -11.037f, -4.382f, -26.016f, -9.093f);
        pb.AddCurveTo(true, -14.97f, -4.715f, -10.638f, -10.104f, -26.665f, -13.116f);
        pb.AddCurveTo(true, -14.149f, -2.66f, -21.318f, 0.468f, -27.722f, 11.581f);
        pb.AddCurveTo(false, 73.104f, 89.075f, 85.989f, 101.047f, 85.989f, 101.047f);
        pb.AddClosePath();
    
        //Add elements into Children collection of SVG 
        svg.Children.Add(new SvgPathElement()
        {
            Fill = SvgPaint.None,
            Stroke = new SvgPaint(Color.Black),
            StrokeWidth = new SvgLength(3.056f),
            StrokeMiterLimit = 11.5f,
            PathData = pb.ToPathData(),
        });
        //Save the document as svg
        doc.Save("demo.svg");
        return doc;
    }
    public static void CreateAndRenderSvgToImage()
    {
        int factor = 2;
        using (var bmp = new GcBitmap(320 * factor, 170 * factor, true, 96f * factor, 96f * factor)) ;
        using (var gr = bmp.CreateGraphics(Color.White))
        {
            gr.DrawSvg(DrawCarrot(), PointF.Empty);
        }
    
        // Save the SVG as image
        bmp.SaveAsPng("carrot.png");
        Console.WriteLine("CreateAndRenderSvgToImage");
    }
    

    Create Thumbnail

    DsImaging 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);
          }
      
      }
      

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

    Note: For rendering large or complex text and graphics, you can use Skia library. For more information about the library and its usage, see Render using Skia Library