/// <summary> /// Generate mask image of the text strategy. /// </summary> /// <param name="strategy">is text strategy</param> /// <param name="width">is the mask image width</param> /// <param name="height">is the mask image height</param> /// <param name="offset">offsets the text (typically used for shadows)</param> /// <param name="textContext">is text context</param> /// <returns>a valid mask image if successful</returns> public static System.Windows.Media.Imaging.WriteableBitmap GenMask( ITextStrategy strategy, int width, int height, System.Windows.Point offset, TextContext textContext) { if (strategy == null || textContext == null) { return(null); } RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); strategy.DrawString(drawingContext, textContext.fontFamily, textContext.fontStyle, textContext.fontWeight, textContext.nfontSize, textContext.pszText, new System.Windows.Point(textContext.ptDraw.X + offset.X, textContext.ptDraw.Y + offset.Y), textContext.ci); drawingContext.Close(); bmp.Render(drawingVisual); return(new System.Windows.Media.Imaging.WriteableBitmap(bmp)); }
/// <summary> /// Draw outline on image /// </summary> /// <param name="strategy">is text strategy</param> /// <param name="image">is the image to be draw upon</param> /// <param name="offset">offsets the text (typically used for shadows)</param> /// <param name="textContext">is text context</param> /// <returns>true if successful</returns> public static bool DrawTextImage( ITextStrategy strategy, ref System.Windows.Media.Imaging.WriteableBitmap image, System.Windows.Point offset, TextContext textContext, System.Windows.Media.Matrix mat) { if (strategy == null || image == null || textContext == null) { return(false); } RenderTargetBitmap bmp = new RenderTargetBitmap((int)(image.Width), (int)(image.Height), 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); MatrixTransform mat_trans = new MatrixTransform(); mat_trans.Matrix = mat; drawingContext.PushTransform(mat_trans); drawingContext.DrawImage(image, new Rect(0.0, 0.0, image.Width, image.Height)); bool bRet = strategy.DrawString(drawingContext, textContext.fontFamily, textContext.fontStyle, textContext.fontWeight, textContext.nfontSize, textContext.pszText, new System.Windows.Point(textContext.ptDraw.X + offset.X, textContext.ptDraw.Y + offset.Y), textContext.ci); drawingContext.Pop(); drawingContext.Close(); bmp.Render(drawingVisual); image = new System.Windows.Media.Imaging.WriteableBitmap(bmp); return(bRet); }
// Draw Faked Beveled effect public MainWindow() { InitializeComponent(); WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); // Load a font from resource //=============================== FontFamily fontFamily = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#Segoe Print"); context.fontFamily = fontFamily; context.fontStyle = FontStyles.Normal; context.fontWeight = FontWeights.Regular; context.nfontSize = 38; context.pszText = "Love Like Magic"; context.ptDraw = new Point(0, 0); // Draw the main outline //========================================================== ITextStrategy mainOutline = TextDesignerWpf.Canvas.TextOutline(Color.FromRgb(235, 10, 230), Color.FromRgb(235, 10, 230), 4); TextDesignerWpf.Canvas.DrawTextImage(mainOutline, ref canvas, new Point(4, 4), context); // Draw the small bright outline shifted (-2, -2) //========================================================== ITextStrategy mainBright = TextDesignerWpf.Canvas.TextOutline(Color.FromRgb(252, 173, 250), Color.FromRgb(252, 173, 250), 2); TextDesignerWpf.Canvas.DrawTextImage(mainBright, ref canvas, new Point(2, 2), context); // Draw the small dark outline shifted (+2, +2) //========================================================== ITextStrategy mainDark = TextDesignerWpf.Canvas.TextOutline(Color.FromRgb(126, 5, 123), Color.FromRgb(126, 5, 123), 2); TextDesignerWpf.Canvas.DrawTextImage(mainDark, ref canvas, new Point(6, 6), context); // Draw the smallest outline (color same as main outline) //========================================================== ITextStrategy mainInner = TextDesignerWpf.Canvas.TextOutline(Color.FromRgb(235, 10, 230), Color.FromRgb(235, 10, 230), 2); TextDesignerWpf.Canvas.DrawTextImage(mainInner, ref canvas, new Point(4, 4), context); // Finally blit the rendered canvas onto the window by assigning the canvas to the image control image1.Source = canvas; }
// Create dirty text effect public MainWindow() { InitializeComponent(); // Load the dirty image from file BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = new Uri("..\\..\\..\\CommonImages\\dirty-texture.png", UriKind.Relative); src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); WriteableBitmap canvasDirty = new WriteableBitmap(src); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); FontFamily fontFamily = new FontFamily("Arial Black"); context.fontFamily = fontFamily; context.fontWeight = FontWeights.Normal; context.fontStyle = FontStyles.Normal; context.nfontSize = 45; context.pszText = "DIRTY"; context.ptDraw = new Point(5, 90); // Load the texture image from file BitmapImage src2 = new BitmapImage(); src2.BeginInit(); src2.UriSource = new Uri("..\\..\\..\\CommonImages\\texture_blue.jpg", UriKind.Relative); src2.CacheOption = BitmapCacheOption.OnLoad; src2.EndInit(); WriteableBitmap texture = new WriteableBitmap(src2); WriteableBitmap mask2 = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Draw the texture against the red dirty mask onto the 2nd texture TextDesignerWpf.Canvas.ApplyImageToMask(texture, canvasDirty, mask2, MaskColor.Red, false); WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); WriteableBitmap maskShadow = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Draw the gray color against the red dirty mask onto the shadow texture TextDesignerWpf.Canvas.ApplyColorToMask(Color.FromArgb(0xaa, 0xcc, 0xcc, 0xcc), canvasDirty, maskShadow, MaskColor.Red, new Point(3, 3)); WriteableBitmap texture2 = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); TextDesignerWpf.Canvas.ApplyImageToMask(texture, mask2, texture2, MaskColor.Red, false); byte[] pixels = new byte[texture2.PixelHeight * texture2.PixelWidth * texture2.Format.BitsPerPixel / 8]; texture2.CopyPixels(new Int32Rect((int)5, (int)80, (int)(image1.Width - 5), (int)(image1.Height - 80)), pixels, texture2.BackBufferStride, 0); WriteableBitmap texture2Cropped = TextDesignerWpf.Canvas.GenImage((int)(image1.Width-5), (int)(image1.Height-80)); texture2Cropped.WritePixels(new Int32Rect((int)0, (int)0, (int)(image1.Width - 5), (int)(image1.Height - 80)), pixels, texture2.BackBufferStride, 0); // Create image brush for the texture //======================================= ImageBrush textureBrush = new ImageBrush(); textureBrush.ImageSource = texture2Cropped; textureBrush.ViewportUnits = BrushMappingMode.RelativeToBoundingBox; textureBrush.Stretch = Stretch.None; textureBrush.TileMode = TileMode.None; textureBrush.AlignmentX = AlignmentX.Left; textureBrush.AlignmentY = AlignmentY.Top; textureBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox; byte[] pixelsShadow = new byte[maskShadow.PixelHeight * maskShadow.PixelWidth * maskShadow.Format.BitsPerPixel / 8]; maskShadow.CopyPixels(new Int32Rect((int)5+3, (int)80+3, (int)(image1.Width - 5-3), (int)(image1.Height - 80-3)), pixelsShadow, maskShadow.BackBufferStride, 0); WriteableBitmap textureShadowCropped = TextDesignerWpf.Canvas.GenImage((int)(image1.Width - 5-3), (int)(image1.Height - 80-3)); textureShadowCropped.WritePixels(new Int32Rect((int)0, (int)0, (int)(image1.Width - 5-3), (int)(image1.Height - 80-3)), pixelsShadow, maskShadow.BackBufferStride, 0); // Create image brush for the shadow //======================================= ImageBrush shadowBrush = new ImageBrush(); shadowBrush.ImageSource = textureShadowCropped; shadowBrush.ViewportUnits = BrushMappingMode.RelativeToBoundingBox; shadowBrush.Stretch = Stretch.None; shadowBrush.TileMode = TileMode.None; shadowBrush.AlignmentX = AlignmentX.Left; shadowBrush.AlignmentY = AlignmentY.Top; shadowBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox; // Create strategy for the shadow with the shadow brush var strategyShadow = TextDesignerWpf.Canvas.TextNoOutline(shadowBrush); // Draw the shadow image shifted -3, -3 TextDesignerWpf.Canvas.DrawTextImage(strategyShadow, ref canvas, new Point(3, 3), context); // Create strategy for the text body var strategy = TextDesignerWpf.Canvas.TextNoOutline(textureBrush); TextDesignerWpf.Canvas.DrawTextImage(strategy, ref canvas, new Point(0, 0), context); // Then draw the rendered image onto window by assigning it to the image control image1.Source = canvas; }
/// <summary> /// Generate mask image of the text strategy. /// </summary> /// <param name="strategy">is text strategy</param> /// <param name="width">is the mask image width</param> /// <param name="height">is the mask image height</param> /// <param name="offset">offsets the text (typically used for shadows)</param> /// <param name="textContext">is text context</param> /// <returns>a valid mask image if successful</returns> public static System.Windows.Media.Imaging.WriteableBitmap GenMask( ITextStrategy strategy, int width, int height, System.Windows.Point offset, TextContext textContext, System.Windows.Media.Matrix mat) { if (strategy == null || textContext == null) return null; RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); MatrixTransform mat_trans = new MatrixTransform(); mat_trans.Matrix = mat; drawingContext.PushTransform(mat_trans); strategy.DrawString(drawingContext, textContext.fontFamily, textContext.fontStyle, textContext.fontWeight, textContext.nfontSize, textContext.pszText, new System.Windows.Point(textContext.ptDraw.X + offset.X, textContext.ptDraw.Y + offset.Y), textContext.ci); drawingContext.Pop(); drawingContext.Close(); bmp.Render(drawingVisual); return new System.Windows.Media.Imaging.WriteableBitmap(bmp); }
/// <summary> /// Draw outline on image /// </summary> /// <param name="strategy">is text strategy</param> /// <param name="image">is the image to be draw upon</param> /// <param name="offset">offsets the text (typically used for shadows)</param> /// <param name="textContext">is text context</param> /// <returns>true if successful</returns> public static bool DrawTextImage( ITextStrategy strategy, ref System.Windows.Media.Imaging.WriteableBitmap image, System.Windows.Point offset, TextContext textContext, System.Windows.Media.Matrix mat) { if (strategy == null || image == null || textContext == null) return false; RenderTargetBitmap bmp = new RenderTargetBitmap((int)(image.Width), (int)(image.Height), 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); MatrixTransform mat_trans = new MatrixTransform(); mat_trans.Matrix = mat; drawingContext.PushTransform(mat_trans); drawingContext.DrawImage(image, new Rect(0.0, 0.0, image.Width, image.Height)); bool bRet = strategy.DrawString(drawingContext, textContext.fontFamily, textContext.fontStyle, textContext.fontWeight, textContext.nfontSize, textContext.pszText, new System.Windows.Point(textContext.ptDraw.X + offset.X, textContext.ptDraw.Y + offset.Y), textContext.ci); drawingContext.Pop(); drawingContext.Close(); bmp.Render(drawingVisual); image = new System.Windows.Media.Imaging.WriteableBitmap(bmp); return bRet; }
private WriteableBitmap DrawChar(int x_offset, Rect rect, TextContext context) { // Create the outline strategy which is going to shift blit diagonally var strategyOutline = TextDesignerWpf.Canvas.TextOutline(MaskColor.Blue, MaskColor.Blue, 4); WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height), Colors.White, 0); // the single mask outline WriteableBitmap maskOutline = TextDesignerWpf.Canvas.GenMask(strategyOutline, (int)(image1.Width), (int)(image1.Height), new Point(0, 0), context); // the mask to store all the single mask blitted diagonally WriteableBitmap maskOutlineAll = TextDesignerWpf.Canvas.GenImage((int)(image1.Width) + 10, (int)(image1.Height) + 10); RenderTargetBitmap bmp = new RenderTargetBitmap((int)(maskOutlineAll.Width), (int)(maskOutlineAll.Height), 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); // blit diagonally for (int i = 0; i < 8; ++i) drawingContext.DrawImage(maskOutline, new Rect((double)(-i), (double)(-i), maskOutline.Width, maskOutline.Height - 0.0)); drawingContext.Close(); bmp.Render(drawingVisual); maskOutlineAll = new WriteableBitmap(bmp); // Measure the dimension of the big mask in order to generate the correct sized gradient image //============================================================================================= uint top = 0; uint bottom = 0; uint left = 0; uint right = 0; TextDesignerWpf.Canvas.MeasureMaskLength(maskOutlineAll, MaskColor.Blue, ref top, ref left, ref bottom, ref right); right += 2; bottom += 2; // Generate the gradient image for the diagonal outline //======================================================= WriteableBitmap gradImage = TextDesignerWpf.Canvas.GenImage((int)(right - left), (int)(bottom - top)); List<Color> listColors = new List<Color>(); listColors.Add(Colors.Purple); listColors.Add(Colors.MediumPurple); DrawGradient.Draw(ref gradImage, listColors, false); // Because Canvas::ApplyImageToMask requires all image to have same dimensions, // we have to blit our small gradient image onto a temp image as big as the canvas //=================================================================================== WriteableBitmap gradBlitted = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); byte[] pixels2 = new byte[gradImage.PixelHeight * gradImage.PixelWidth * gradImage.Format.BitsPerPixel / 8]; gradImage.CopyPixels(pixels2, gradImage.BackBufferStride, 0); gradBlitted.WritePixels(new Int32Rect((int)left, (int)top, (int)(gradImage.Width), (int)(gradImage.Height)), pixels2, gradImage.BackBufferStride, 0); TextDesignerWpf.Canvas.ApplyImageToMask(gradBlitted, maskOutlineAll, canvas, MaskColor.Blue, false); // Create strategy and mask image for the text body //=================================================== var strategyText = TextDesignerWpf.Canvas.TextNoOutline(MaskColor.Blue); WriteableBitmap maskText = TextDesignerWpf.Canvas.GenMask(strategyText, (int)(image1.Width), (int)(image1.Height), new Point(0, 0), context); // Measure the dimension required for text body using the mask //============================================================= top = 0; bottom = 0; left = 0; right = 0; TextDesignerWpf.Canvas.MeasureMaskLength(maskText, MaskColor.Blue, ref top, ref left, ref bottom, ref right); top -= 2; left -= 2; right += 2; bottom += 2; GradientStopCollection coll = new GradientStopCollection(); GradientStop stop = new GradientStop(Colors.DeepPink, 0.0); coll.Add(stop); stop = new GradientStop(Colors.LightPink, 1.0); coll.Add(stop); // Create the gradient brush for the text body LinearGradientBrush gradTextbrush = new LinearGradientBrush(coll, 90.0); // Create the actual strategy for the text body used for rendering, with the gradient brush var strategyText2 = TextDesignerWpf.Canvas.TextNoOutline(gradTextbrush); // Draw the newly created strategy onto the canvas TextDesignerWpf.Canvas.DrawTextImage(strategyText2, ref canvas, new Point(0, 0), context); return canvas; }
public MainWindow() { InitializeComponent(); //WriteableBitmap main_canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height), Colors.White, 0); RenderTargetBitmap main_canvas = new RenderTargetBitmap((int)(image1.Width), (int)(image1.Height), 96, 96, PixelFormats.Pbgra32); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); FontFamily fontFamily = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#Airbus Special"); context.fontFamily = fontFamily; context.fontStyle = FontStyles.Normal; context.fontWeight = FontWeights.Regular; context.nfontSize = 40; context.pszText = ""; context.ptDraw = new Point(8, 12); string text = "PENTHOUSE"; int x_offset = 0; float y_offset = 0.0f; for (int i = 0; i < text.Length; ++i) { string str = ""; str += text[i]; context.pszText = str; WriteableBitmap canvas = DrawChar(x_offset, new Rect(0.0, 0.0, (double)(image1.Width), (double)(image1.Height)), context); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); TranslateTransform trans_mat = new TranslateTransform(0.0f, y_offset); y_offset += 7.3f; drawingContext.PushTransform(trans_mat); ScaleTransform scale_mat = new ScaleTransform(0.75f, 1.0f); drawingContext.PushTransform(scale_mat); RotateTransform rot_mat = new RotateTransform(-9.8f); drawingContext.PushTransform(rot_mat); drawingContext.DrawImage(canvas, new Rect((double)(x_offset), (double)(0.0), image1.Width , image1.Height - 0.0)); drawingContext.Close(); main_canvas.Render(drawingVisual); if (i == 0) x_offset += 42; else if (i == 2) x_offset += 46; else if (i == 3) x_offset += 43; else if (i == 4 || i == 5 || i == 6) x_offset += 46; else x_offset += 41; } // Finally blit the rendered image onto the window by assigning canvas to the image control image1.Source = main_canvas; }
// Draw the BE HAPPY effect from the BE HAPPY soap opera public MainWindow() { InitializeComponent(); // Create canvas to be rendered WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Create canvas for the green outermost outline WriteableBitmap canvasOuter = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Create canvas for the white inner outline WriteableBitmap canvasInner = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); // Load a font from its resource into private collection, // instead of from system font collection //============================================================= FontFamily fontFamily = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#Alba Regular"); context.fontFamily = fontFamily; context.fontStyle = FontStyles.Normal; context.fontWeight = FontWeights.Regular; context.nfontSize = 48; context.pszText = "bE"; context.ptDraw = new Point(63, 0); RenderTargetBitmap bmp = new RenderTargetBitmap((int)(image1.Width), (int)(image1.Height), 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); // Create the outer strategy var strategyOutline2 = TextDesignerWpf.Canvas.TextOutline(Colors.LightSeaGreen, Colors.LightSeaGreen, 16); // Draw the bE text (outer green outline) TextDesignerWpf.Canvas.DrawTextImage(strategyOutline2, ref canvasOuter, new Point(0, 0), context); context.pszText = "Happy"; context.ptDraw = new Point(8, 48); // Draw the Happy text (outer green outline) TextDesignerWpf.Canvas.DrawTextImage(strategyOutline2, ref canvasOuter, new Point(0, 0), context); // blit the canvasOuter all the way down (5 pixels down) //======================================================== drawingContext.DrawImage(canvasOuter, new Rect(0.0, 0.0, canvasOuter.Width, canvasOuter.Height - 0.0)); drawingContext.DrawImage(canvasOuter, new Rect(0.0, 1.0, canvasOuter.Width, canvasOuter.Height - 1.0)); drawingContext.DrawImage(canvasOuter, new Rect(0.0, 2.0, canvasOuter.Width, canvasOuter.Height - 2.0)); drawingContext.DrawImage(canvasOuter, new Rect(0.0, 3.0, canvasOuter.Width, canvasOuter.Height - 3.0)); drawingContext.DrawImage(canvasOuter, new Rect(0.0, 4.0, canvasOuter.Width, canvasOuter.Height - 4.0)); drawingContext.DrawImage(canvasOuter, new Rect(0.0, 5.0, canvasOuter.Width, canvasOuter.Height - 5.0)); context.pszText = "bE"; context.ptDraw = new Point(63, 0); // Create the inner white strategy var strategyOutline1 = TextDesignerWpf.Canvas.TextOutline(Colors.White, Colors.White, 8); // Draw the bE text (inner white outline) TextDesignerWpf.Canvas.DrawTextImage(strategyOutline1, ref canvasInner, new Point(0, 0), context); context.pszText = "Happy"; context.ptDraw = new Point(8, 48); // Draw the Happy text (inner white outline) TextDesignerWpf.Canvas.DrawTextImage(strategyOutline1, ref canvasInner, new Point(0, 0), context); // blit the canvasInner all the way down (5 pixels down) //======================================================== drawingContext.DrawImage(canvasInner, new Rect(0.0, 0.0, canvasInner.Width, canvasInner.Height - 0.0)); drawingContext.DrawImage(canvasInner, new Rect(0.0, 1.0, canvasInner.Width, canvasInner.Height - 1.0)); drawingContext.DrawImage(canvasInner, new Rect(0.0, 2.0, canvasInner.Width, canvasInner.Height - 2.0)); drawingContext.DrawImage(canvasInner, new Rect(0.0, 3.0, canvasInner.Width, canvasInner.Height - 3.0)); drawingContext.DrawImage(canvasInner, new Rect(0.0, 4.0, canvasInner.Width, canvasInner.Height - 4.0)); drawingContext.DrawImage(canvasInner, new Rect(0.0, 5.0, canvasInner.Width, canvasInner.Height - 5.0)); image1.Source = canvasInner; // Create the strategy for green text body var strategyOutline = TextDesignerWpf.Canvas.TextOutline(Colors.LightSeaGreen, Colors.LightSeaGreen, 1); context.pszText = "bE"; context.ptDraw = new Point(63, 0); // Draw the bE text (text body) TextDesignerWpf.Canvas.DrawTextImage(strategyOutline, ref canvas, new Point(0, 0), context); context.pszText = "Happy"; context.ptDraw = new Point(8, 48); // Draw the Happy text (text body) TextDesignerWpf.Canvas.DrawTextImage(strategyOutline, ref canvas, new Point(0, 0), context); drawingContext.DrawImage(canvas, new Rect(0.0, 0.0, canvas.Width, canvas.Height)); drawingContext.Close(); bmp.Render(drawingVisual); // Finally blit the rendered canvas onto the window by assigning bmp to image control image1.Source = bmp; }
// Create rainbow Text Effect in Aquarion EVOL anime public MainWindow() { InitializeComponent(); // Create the outline strategy which is used later on for measuring // the size of text in order to generate a correct sized gradient image var strategyOutline2 = TextDesignerWpf.Canvas.TextOutline(MaskColor.Blue, MaskColor.Blue, 6); WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); // Load a font from its resource, // instead of from system font collection //============================================================= FontFamily fontFamily = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#Ruzicka Freehand LT Std"); context.fontFamily = fontFamily; context.fontStyle = FontStyles.Normal; context.fontWeight = FontWeights.Regular; context.nfontSize = 46; context.pszText = "I cross over the deep blue void"; context.ptDraw = new Point(0, 0); // Generate the mask image for measuring the size of the text image required //============================================================================ WriteableBitmap maskOutline2 = TextDesignerWpf.Canvas.GenMask(strategyOutline2, (int)(image1.Width), (int)(image1.Height), new Point(0, 0), context); uint top = 0; uint bottom = 0; uint left = 0; uint right = 0; TextDesignerWpf.Canvas.MeasureMaskLength(maskOutline2, MaskColor.Blue, ref top, ref left, ref bottom, ref right); bottom += 2; right += 2; // Generate the gradient image //============================= WriteableBitmap bmpGrad = new WriteableBitmap((int)(right - left), (int)(bottom - top), 96.0, 96.0, PixelFormats.Pbgra32, null); List<Color> list = new List<Color>(); list.Add(Color.FromRgb(255, 0, 0)); list.Add(Color.FromRgb(0, 0, 255)); list.Add(Color.FromRgb(0, 255, 0)); DrawGradient.Draw(ref bmpGrad, list, true); // Because Canvas::ApplyImageToMask requires the all images to have equal dimension, // we need to blit our new gradient image onto a larger image to be same size as canvas image //============================================================================================== WriteableBitmap bmpGrad2 = new WriteableBitmap((int)(image1.Width), (int)(image1.Height), 96.0, 96.0, PixelFormats.Pbgra32, null); byte[] pixels = new byte[bmpGrad.PixelHeight * bmpGrad.PixelWidth * bmpGrad.Format.BitsPerPixel / 8]; bmpGrad.CopyPixels(pixels, bmpGrad.BackBufferStride, 0); bmpGrad2.WritePixels(new Int32Rect((int)left, (int)top, (int)(right - left), (int)(bottom - top)), pixels, bmpGrad.BackBufferStride, 0); // Apply the rainbow text against the blue mask onto the canvas TextDesignerWpf.Canvas.ApplyImageToMask(bmpGrad2, maskOutline2, canvas, MaskColor.Blue, false); // Draw the (white body and black outline) text onto the canvas //============================================================== ITextStrategy strategyOutline1 = TextDesignerWpf.Canvas.TextOutline(Color.FromRgb(255, 255, 255), Color.FromRgb(0, 0, 0), 1); TextDesignerWpf.Canvas.DrawTextImage(strategyOutline1, ref canvas, new Point(0, 0), context); // Finally blit the rendered image onto the window by assigning canvas to the image control image1.Source = canvas; }
// Create the extruded text effect public MainWindow() { InitializeComponent(); // Create the outline strategy which is going to shift blit diagonally var strategyOutline = TextDesignerWpf.Canvas.TextOutline(MaskColor.Blue, MaskColor.Blue, 4); WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height), Colors.White, 0); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); FontFamily fontFamily = new FontFamily("Arial Black"); context.fontFamily = fontFamily; context.fontStyle = FontStyles.Normal; context.fontWeight = FontWeights.Regular; context.nfontSize = 40; context.pszText = "CODING MONKEY"; context.ptDraw = new Point(0, 0); // the single mask outline WriteableBitmap maskOutline = TextDesignerWpf.Canvas.GenMask(strategyOutline, (int)(image1.Width), (int)(image1.Height), new Point(0, 0), context); // the mask to store all the single mask blitted diagonally WriteableBitmap maskOutlineAll = TextDesignerWpf.Canvas.GenImage((int)(image1.Width) + 10, (int)(image1.Height) + 10); RenderTargetBitmap bmp = new RenderTargetBitmap((int)(maskOutlineAll.Width), (int)(maskOutlineAll.Height), 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); // blit diagonally for (int i = 0; i < 7; ++i) drawingContext.DrawImage(maskOutline, new Rect((double)(i), (double)(i), maskOutline.Width, maskOutline.Height - 0.0)); drawingContext.Close(); bmp.Render(drawingVisual); maskOutlineAll = new WriteableBitmap(bmp); // Measure the dimension of the big mask in order to generate the correct sized gradient image //============================================================================================= uint top = 0; uint bottom = 0; uint left = 0; uint right = 0; TextDesignerWpf.Canvas.MeasureMaskLength(maskOutlineAll, MaskColor.Blue, ref top, ref left, ref bottom, ref right); right += 2; bottom += 2; // Generate the gradient image for the diagonal outline //======================================================= WriteableBitmap gradImage = TextDesignerWpf.Canvas.GenImage((int)(right - left), (int)(bottom - top)); List<Color> listColors = new List<Color>(); listColors.Add(Colors.DarkGreen); listColors.Add(Colors.YellowGreen); DrawGradient.Draw(ref gradImage, listColors, false); // Because Canvas::ApplyImageToMask requires all image to have same dimensions, // we have to blit our small gradient image onto a temp image as big as the canvas //=================================================================================== WriteableBitmap gradBlitted = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); byte[] pixels2 = new byte[gradImage.PixelHeight * gradImage.PixelWidth * gradImage.Format.BitsPerPixel / 8]; gradImage.CopyPixels(pixels2, gradImage.BackBufferStride, 0); gradBlitted.WritePixels(new Int32Rect((int)left, (int)top, (int)(gradImage.Width), (int)(gradImage.Height)), pixels2, gradImage.BackBufferStride, 0); TextDesignerWpf.Canvas.ApplyImageToMask(gradBlitted, maskOutlineAll, canvas, MaskColor.Blue, false); // Create strategy and mask image for the text body //=================================================== var strategyText = TextDesignerWpf.Canvas.TextNoOutline(MaskColor.Blue); WriteableBitmap maskText = TextDesignerWpf.Canvas.GenMask(strategyText, (int)(image1.Width), (int)(image1.Height), new Point(0, 0), context); // Measure the dimension required for text body using the mask //============================================================= top = 0; bottom = 0; left = 0; right = 0; TextDesignerWpf.Canvas.MeasureMaskLength(maskText, MaskColor.Blue, ref top, ref left, ref bottom, ref right); top -= 2; left -= 2; right += 2; bottom += 2; GradientStopCollection coll = new GradientStopCollection(); GradientStop stop = new GradientStop(Colors.Orange, 0.0); coll.Add(stop); stop = new GradientStop(Colors.OrangeRed, 1.0); coll.Add(stop); // Create the gradient brush for the text body LinearGradientBrush gradTextbrush = new LinearGradientBrush(coll, 90.0); // Create the actual strategy for the text body used for rendering, with the gradient brush var strategyText2 = TextDesignerWpf.Canvas.TextNoOutline(gradTextbrush); // Draw the newly created strategy onto the canvas TextDesignerWpf.Canvas.DrawTextImage(strategyText2, ref canvas, new Point(0, 0), context); // Finally blit the rendered image onto the window by assigning canvas to the image control image1.Source = canvas; }
// Generating the hollow text effect where the text looks like cut out of canvas void timer_Tick(object sender, EventArgs e) { ++_TimerLoop; if (_TimerLoop > 4) _TimerLoop = 0; // Generating the outline strategy for displaying inside the hollow var strategyOutline = TextDesignerWpf.Canvas.TextGradOutline(Color.FromRgb(255, 255, 255), Color.FromRgb(230, 230, 230), Color.FromRgb(100, 100, 100), 9); // Canvas to render the text. Note the canvas should always be transparent WriteableBitmap canvas = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Text context to store string and font info to be sent as parameter to Canvas methods TextContext context = new TextContext(); FontFamily fontFamily = new FontFamily("Arial Black"); context.fontFamily = fontFamily; context.fontStyle = FontStyles.Normal; context.fontWeight = FontWeights.Bold; context.nfontSize = 56; context.pszText = "CUTOUT"; context.ptDraw = new Point(0, 0); WriteableBitmap hollowImage = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); // Algorithm to shift the shadow outline in and then out continuous int shift = 0; if (_TimerLoop >= 0 && _TimerLoop <= 2) shift = _TimerLoop; else shift = 2 - (_TimerLoop - 2); // Draw the hollow (shadow) outline by shifting accordingly TextDesignerWpf.Canvas.DrawTextImage(strategyOutline, ref hollowImage, new Point(2 + shift, 2 + shift), context); // Generate the green mask for the cutout holes in the text WriteableBitmap maskImage = TextDesignerWpf.Canvas.GenImage((int)(image1.Width), (int)(image1.Height)); var strategyMask = TextDesignerWpf.Canvas.TextOutline(MaskColor.Green, MaskColor.Green, 0); TextDesignerWpf.Canvas.DrawTextImage(strategyMask, ref maskImage, new Point(0, 0), context); // Apply the hollowed image against the green mask on the canvas TextDesignerWpf.Canvas.ApplyImageToMask(hollowImage, maskImage, canvas, MaskColor.Green, false); // Create a black outline only strategy and blit it onto the canvas to cover // the unnatural outline from the gradient shadow //============================================================================= var strategyOutlineOnly = TextDesignerWpf.Canvas.TextOnlyOutline(Color.FromRgb(0, 0, 0), 2, false); TextDesignerWpf.Canvas.DrawTextImage(strategyOutlineOnly, ref canvas, new Point(0, 0), context); // Draw the transparent canvas onto the back buffer //=================================================== RenderTargetBitmap bmp = new RenderTargetBitmap((int)(canvas.Width), (int)(canvas.Height), 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); SolidColorBrush brush = new SolidColorBrush(Colors.Black); drawingContext.DrawRectangle(brush, new Pen(brush, 1.0), new Rect(0.0, 0.0, canvas.Width, canvas.Height)); drawingContext.DrawImage(canvas, new Rect(0.0, 0.0, canvas.Width, canvas.Height)); drawingContext.Close(); bmp.Render(drawingVisual); // Finally blit the rendered image onto the window by assigning to the image control. image1.Source = bmp; // Release all the resources //============================ strategyOutline.Dispose(); strategyMask.Dispose(); strategyOutlineOnly.Dispose(); }