Document Solutions for Imaging
Features / Work with Graphics / Add Shadow
In This Topic
    Add Shadow
    In This Topic

    DsImaging provides ApplyGaussianBlur and ToShadowBitmap methods in GrayscaleBitmap class. These methods make the process of drawing an image with a shadow easier and more straightforward. The ApplyGaussianBlur method accepts the borderColor, radius, and borderMode arguments. The ToShadowBitmap method simplifies moving a transparency mask from GrayscaleBitmap to a GcBitmap, and has the ability to pass the shadowColor and opacity factor. Also, the ToShadowBitmap always treats the GrayscaleBitmap as a transparency mask, even if it was created from any color channel (not necessarily the alpha channel).

    To add a shadow to an image:

    1. Draw an image with some shapes and text.


      C#
      Copy Code
      // Initialize GcBitmap.
      using var bmp = new GcBitmap(800, 600, false);
      
      // Draw an image.
      using (var g = bmp.CreateGraphics(Color.AliceBlue))
      {
          Draw(g, 0, 0);
      }
      
      // Save the image without a shadow.
      bmp.SaveAsPng("WithoutSahdow.png");
      
      static void Draw(GcGraphics g, float offsetX, float offsetY)
      {
          // Define the transformation matrix.
          var baseT = Matrix3x2.CreateTranslation(offsetX, offsetY);
          g.Transform = baseT;
          
          // Draw an ellipse.
          g.DrawEllipse(new RectangleF(100, 100, 300, 200),
              new Pen(Color.Orange, 20f));
      
          // Draw a line.
          g.DrawLine(new PointF(50, 400), new PointF(500, 50),
              new Pen(Color.RoyalBlue, 20f)
              {
                  LineCap = PenLineCap.Round
              });
      
          // Draw strings.
          g.DrawString("Shadow",
              new TextFormat
              {
                  FontName = "Segoe UI",
                  FontSize = 40,
                  ForeColor = Color.MistyRose,
                  StrokePen = new Pen(Color.DarkRed, 1f)
              },
              new PointF(200, 150));
          g.Transform = Matrix3x2.CreateRotation((float)(Math.PI / 6)) *
              (Matrix3x2.CreateTranslation(50, 250) * baseT);
          g.DrawString("The shadow is added to both text and shapes.",
              new TextFormat
              {
                  FontName = "Times New Roman",
                  FontSize = 18,
                  ForeColor = Color.CornflowerBlue
              },
              new PointF(0, 0));
      
          // Draw a rectangle.
          g.DrawRectangle(new RectangleF(-15, -10, 470, 50),
              new Pen(Color.Salmon, 1f));
      }
      
    2. Draw the image on a transparent background with an offset for the shadow.


      C#
      Copy Code
      // Draw the image to the transparent background with an offset for shadow.
      using (var g = bmp.CreateGraphics(Color.Transparent))
      {
          Draw(g, 20, 50);
      }
      
    3. Extract the alpha channel from GcBitmap to a GrayscaleBitmap.

      C#
      Copy Code
      // Extract the alpha channel from GcBitmap to a GrayscaleBitmap.
      using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
      
    4. Apply some blur to the GrayscaleBitmap using the ApplyGaussianBlur method.

      C#
      Copy Code
      // Apply some blur to GrayscaleBitmap.
      gs.ApplyGaussianBlur(9);
      
    5. Convert the transparency mask from GrayscaleBitmap to GcBitmap, filling the opaque pixels with the shadow color. Draw the transparency mask into the same bitmap using the ToShadowBitmap; there is no need to create another GcBitmap instance.


      C#
      Copy Code
      // Convert the transparency mask from GrayscaleBitmap to GcBitmap. Apply an additional opacity factor.
      gs.ToShadowBitmap(bmp, Color.CadetBlue, 0.4f);
      
    6. Substitute the transparent background with an opaque background color.


      C#
      Copy Code
      // Substitute the transparent background with an opaque background color.
      bmp.ConvertToOpaque(Color.AliceBlue);
      
    7. Draw the main image onto the shadow image and save the image.


      C#
      Copy Code
      // Draw the main image.
      using (var g = bmp.CreateGraphics())
      {
          Draw(g, 0f, 0f);
      }
      
      // Save the image with a shadow.
      bmp.SaveAsPng("WithSahdow.png");