//
// 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.Collections.Generic;
using System.Linq;
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 sample demonstrates the effects of different blend modes
// when drawing on a GcGraphics. Note that the current blend mode
// set on an instance of GcGraphics (including GcBitmapGraphics)
// affects not only images but all drawing operations.
// See also BlendImages_0 and BlendingModes samples.
public class BlendText
{
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] _ = null)
{
var ispectr = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-500x500.png"));
const int margin = 36;
const int NCOLS = 4;
var w = (int)((pixelSize.Width - margin * 2) / NCOLS);
var h = w / 2;
int row = 0, col = 0;
float bottomy = 0;
var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
using (var g = bmp.CreateGraphics(Color.White))
{
// Text layout for captions:
var tl = g.CreateTextLayout();
tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "courbd.ttf"));
tl.DefaultFormat.FontSize = 22;
tl.ParagraphAlignment = ParagraphAlignment.Center;
tl.TextAlignment = TextAlignment.Center;
tl.MaxWidth = w;
tl.MaxHeight = h;
tl.MarginTop = (h - h / 1.4f) / 2;
tl.DefaultFormat.ForeColor = Color.Black;
var tfWhite = new TextFormat(tl.DefaultFormat) { ForeColor = Color.White };
// Render all blend modes in a grid:
var modes = Enum.GetValues(typeof(BlendMode));
foreach (var mode in modes)
{
var blendMode = (BlendMode)mode;
if (!g.IsBlendModeSupported(blendMode))
continue;
int x = margin + w * col;
int y = margin + h * row;
var r = new RectangleF(x, y, w, h);
// Draw spectrum image normally:
g.BlendMode = BlendMode.Normal;
g.DrawImage(ispectr, r, null, ImageAlign.StretchImage);
// Draw blend mode name using the current blend mode:
tl.Clear();
tl.AppendLine($"B: {blendMode}");
tl.Append($"W: {blendMode}", tfWhite);
tl.PerformLayout(true);
var rc = tl.ContentRectangle;
rc.Offset(x, y);
rc.Inflate(4, 2);
// Current blend mode:
g.BlendMode = blendMode;
g.DrawTextLayout(tl, new PointF(x, y));
// Draw spectrum image again using BlendMode.Difference
// to produce (mostly) colorful text on black background:
g.BlendMode = BlendMode.Difference;
g.DrawImage(ispectr, r, null, ImageAlign.StretchImage);
// Draw a rectangle to mark the current area:
g.BlendMode = BlendMode.Normal;
g.DrawRectangle(r, Color.Gray);
bottomy = r.Bottom;
if (++col == NCOLS)
{
col = 0;
++row;
}
}
// For reference, draw the backdrop:
tl.Clear();
tl.MarginAll = 0;
tl.MaxWidth = bmp.PixelWidth;
tl.MaxHeight = null;
tl.DefaultFormat.FontSize = 14;
tl.TextAlignment = TextAlignment.Leading;
tl.ParagraphAlignment = ParagraphAlignment.Near;
tl.Append("The spectrum image used as the backdrop for the texts above:");
g.DrawTextLayout(tl, new PointF(margin, bottomy + margin));
g.DrawImage(ispectr, new RectangleF(margin, bottomy + margin + tl.ContentHeight + 24, w, w), null, ImageAlign.StretchImage);
}
return bmp;
}
}
}