Shadow.cs
C# VB
//// This code is part of Document Solutions for Imaging demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System;using System.IO;using System.Drawing;using System.Numerics;using GrapeCity.Documents.Drawing;using GrapeCity.Documents.Text;using GrapeCity.Documents.Imaging;using GCTEXT = GrapeCity.Documents.Text;using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsImagingWeb.Demos{ // This example shows how to create a semi-transparent blurred shadow // of a text and graphics image, offset by a specified amount. // To achieve this the code employs the ApplyGaussianBlur and ToShadowBitmap // methods of the GrayscaleBitmap class. public class Shadow { private GCTEXT.Font _font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf"));
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null) { // Create a transparent bitmap and draw the shadow image on it (offset left/down by 30/50): var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, false); using (var g = bmp.CreateGraphics(Color.Transparent)) { Draw(g, 30, 50); }
// Extract the alpha channel from GcBitmap to a GrayscaleBitmap: using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
// Blur the GrayscaleBitmap to create a believable looking shadow: gs.ApplyGaussianBlur(9);
// Convert the transparency mask from GrayscaleBitmap to GcBitmap, // filling the opaque pixels with the shadow color (CadetBlue), // also making the resulting 'shadow' slightly transparent (0.6f): gs.ToShadowBitmap(bmp, Color.CadetBlue, 0.6f);
// Replace the transparent background with an opaque background color: bmp.ConvertToOpaque(Color.LightGoldenrodYellow);
// Finally draw the original image without offset on top: using (var g = bmp.CreateGraphics()) { Draw(g, 0, 0); }
// Done return bmp; }
private void Draw(GcGraphics g, float offsetX, float offsetY) { var baseT = Matrix3x2.CreateTranslation(offsetX, offsetY); g.Transform = baseT; g.DrawEllipse(new RectangleF(100, 100, 300, 200), new GCDRAW.Pen(Color.Orange, 20)); g.DrawLine(new PointF(50, 400), new PointF(500, 50), new GCDRAW.Pen(Color.RoyalBlue, 20) { LineCap = PenLineCap.Round }); g.DrawString("Howl's Moving Castle", new TextFormat { Font = _font, FontSize = 40, ForeColor = Color.MistyRose, StrokePen = new GCDRAW.Pen(Color.DarkRed, 1) }, new PointF(200, 150)); g.Transform = Matrix3x2.CreateRotation((float)(Math.PI / 6)) * (Matrix3x2.CreateTranslation(50, 250) * baseT); g.DrawString("The quick brown fox jumps over the lazy dog.", new TextFormat { Font = _font, FontSize = 18, ForeColor = Color.CornflowerBlue }, new PointF(0, 0)); g.DrawRectangle(new RectangleF(-15, -10, 470, 50), new GCDRAW.Pen(Color.Salmon, 1));
g.Transform = baseT;
// Draw a window with four colored semi-transparent panes: var wnd = new RectangleF(520, 420, 400, 500); var winHalf = new SizeF(wnd.Width / 2, wnd.Height / 2); var frame = Color.Brown; var glassTL = Color.FromArgb(unchecked((int)0x70FF4600)); var glassTR = Color.FromArgb(unchecked((int)0x70A5FF00)); var glassBL = Color.FromArgb(unchecked((int)0x70007BFF)); var glassBR = Color.FromArgb(unchecked((int)0x70FFCD00));
g.FillRectangle(new RectangleF(wnd.Location, winHalf), glassTL); g.FillRectangle(new RectangleF(new PointF(wnd.X + wnd.Width / 2, wnd.Y), winHalf), glassTR); g.FillRectangle(new RectangleF(new PointF(wnd.X, wnd.Y + wnd.Height / 2), winHalf), glassBL); g.FillRectangle(new RectangleF(new PointF(wnd.X + wnd.Width / 2, wnd.Y + wnd.Height / 2), winHalf), glassBR);
g.DrawRectangle(wnd, new GCDRAW.Pen(frame, 30)); g.DrawLine(wnd.Left, wnd.Top + wnd.Height / 2, wnd.Right, wnd.Top + wnd.Height / 2, frame, 20); g.DrawLine(wnd.Left + wnd.Width / 2, wnd.Top, wnd.Left + wnd.Width / 2, wnd.Bottom, frame, 20);
g.Transform = Matrix3x2.Identity; } }}