The text API described in this topic is out of date. If you want to learn about text API represented by the new drawing engine in Graphics Mill, read the Drawing Text article.
Text with effects looks more attractive and eye-catching than a plain caption. This is especially important in web graphics. The article below demonstrates how to apply effects on text.
Let's start with a shadow effect. To create a realistic shadow we should draw plain text on a transparent bitmap and apply the Shadow(Color, Single, Single, Single, Boolean) transform on it. The result is shown at the figure below:
This code demonstrates how to achieve it:
//Create bitmap with transparent background using (var bitmap = new Bitmap(150, 30, PixelFormat.Format32bppArgb, RgbColor.Transparent)) using (var graphics = bitmap.GetGdiPlusGraphics()) { var font = new System.Drawing.Font("Arial", 22, System.Drawing.FontStyle.Bold); var brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); //Draw text graphics.DrawString("Sample text", font, brush, 5, 5); //Apply shadow effect on text bitmap.Transforms.Shadow(RgbColor.Black, 1, 1, 1, false); //Convert to 24 bit images (merge alpha channel with white background) bitmap.Channels.RemoveAlpha(RgbColor.White); bitmap.Save(@"Images\Output\TextWithEffects1.png"); }
Usually it is necessary to draw a separate text label on an existing image. The code example below demonstrates how to create the following figure:
//Create bitmap object which contains image on which we want to draw text using (var bitmap = new Bitmap(@"Images\sampleEf.jpg")) //Create bitmap with transparent background using (var bitmapText = new Bitmap(150, 30, PixelFormat.Format32bppArgb, RgbColor.Transparent)) using (var graphics = bitmapText.GetGdiPlusGraphics()) { //Set text output properties System.Drawing.Font font = new System.Drawing.Font("Arial", 22, System.Drawing.FontStyle.Bold); System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); //Draw text graphics.DrawString("Sample text", font, brush, 10, 5); //Apply shadow effect on text bitmapText.Transforms.Shadow(RgbColor.Black, 1, 1, 1, false); //Draw text with shadow or glow on bitmap bitmap.Draw(bitmapText, 0, 0, bitmapText.Width, bitmapText.Height, CombineMode.Alpha, 1, ResizeInterpolationMode.Low); bitmap.Save(@"Images\Output\TextWithEffects2.png"); }
There are many other text effects; for example, you can use the Spray(Int32) method instead of Shadow(Color, Single, Single, Single, Boolean):
Yet another text effect is a 3D text emulation. This can be easily done by drawing the same text twice—the second time with a small offset, say of one pixel.
The code below shows how to create such an effect.
//Create bitmap using (var bitmap = new Bitmap(150, 30, PixelFormat.Format32bppArgb, RgbColor.Transparent)) using (var graphics = bitmap.GetGdiPlusGraphics()) { //Set text properties var font = new System.Drawing.Font("Arial", 22, System.Drawing.FontStyle.Bold); var brush = new System.Drawing.SolidBrush(System.Drawing.Color.DarkRed); //Draw background text graphics.DrawString("Sample text", font, brush, 6, 6); //Set foreground text properties brush.Color = System.Drawing.Color.Red; //Draw foreground text graphics.DrawString("Sample text", font, brush, 5, 5); bitmap.Save(@"Images\Output\TextWithEffects3.png"); }
So feel free to experiment.