[]
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:
Draw an image with some shapes and text.
// 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));
}
Draw the image on a transparent background with an offset for the shadow.
// Draw the image to the transparent background with an offset for shadow.
using (var g = bmp.CreateGraphics(Color.Transparent))
{
Draw(g, 20, 50);
}
Extract the alpha channel from GcBitmap to a GrayscaleBitmap.
// Extract the alpha channel from GcBitmap to a GrayscaleBitmap.
using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
Apply some blur to the GrayscaleBitmap using the ApplyGaussianBlur method.
// Apply some blur to GrayscaleBitmap.
gs.ApplyGaussianBlur(9);
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.
// Convert the transparency mask from GrayscaleBitmap to GcBitmap. Apply an additional opacity factor.
gs.ToShadowBitmap(bmp, Color.CadetBlue, 0.4f);
Substitute the transparent background with an opaque background color.
// Substitute the transparent background with an opaque background color.
bmp.ConvertToOpaque(Color.AliceBlue);
Draw the main image onto the shadow image and save the image.
// Draw the main image.
using (var g = bmp.CreateGraphics())
{
Draw(g, 0f, 0f);
}
// Save the image with a shadow.
bmp.SaveAsPng("WithSahdow.png");