//// This code is part of Document Solutions for Imaging demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Drawing;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Imaging;usingGCTEXT = GrapeCity.Documents.Text; namespaceDsImagingWeb.Demos{// This sample shows how a color spectrum with fall offs from fully saturated// colors to white and black can be easily created using linear gradient brushes// and GcBitmap.Renderer.TransparencyMaskBitmap.publicclassColorSpectrum {publicGcBitmapGenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null) {// Color spectrum gradient brush:var spec = newLinearGradientBrush(Color.Red, Color.Red);var step = 1f / 6f; spec.GradientStops = newGradientStop[] {newGradientStop(Color.Yellow, step),newGradientStop(Color.Green, step * 2),newGradientStop(Color.Cyan, step * 3),newGradientStop(Color.Blue, step * 4),newGradientStop(Color.Magenta, step * 5), }; // Create the spectrum in a separate temp bitmap:var spectrumSz = newSize(pixelSize.Width, (int)(pixelSize.Height * 0.75f));// Falloff brush and mask:usingvar tbmp = newGcBitmap(spectrumSz.Width, spectrumSz.Height, true);usingvar tgfx = tbmp.CreateGraphics();// Black is opaque, White transparent:var falloff = newLinearGradientBrush(Color.Black, PointF.Empty, Color.Black, newPointF(0, 1));// Additional gray stops provide a more gradual and pleasing appearance: falloff.GradientStops = newGradientStop[] {newGradientStop(Color.LightGray, 0.35f),newGradientStop(Color.White, 0.5f),newGradientStop(Color.LightGray, 0.65f) };// Fill the mask with the gradient that will define the transparency: tgfx.FillRectangle(newRectangleF(0, 0, tbmp.Width, tbmp.Height), falloff);// Convert mask to grayscale for use as the transparency mask:usingvar mask = tbmp.ToGrayscaleBitmap();// Reuse the original mask bitmap;// Fill it with a gradient from white at the top to black at the bottom:var grad = newLinearGradientBrush(Color.White, PointF.Empty, Color.Black, newPointF(0, 1)); tgfx.FillRectangle(newRectangleF(0, 0, spectrumSz.Width, spectrumSz.Height), grad); // Add transparency mask and draw the gradient: tbmp.Renderer.TransparencyMaskBitmap = mask; tgfx.FillRectangle(newRectangleF(PointF.Empty, spectrumSz), spec); // Copy spectrum to the target bitmap:var bmp = newGcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi); bmp.BitBlt(tbmp, 0, 0); // Add some explanatory texts below the spectrum:var g = bmp.CreateGraphics();var rc = newRectangleF(0, spectrumSz.Height, spectrumSz.Width, bmp.PixelHeight - spectrumSz.Height);var tf = newTextFormat() {Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMonoBold.ttf")),FontSize = 14, }; g.FillRectangle(rc, Color.LightGray); g.DrawString("The color spectrum is drawn using a horizontal linear gradient brush with 7 stops:\n" +"Red - Yellow - Green - Cyan - Blue - Magenta - Red.\n" +"The background is filled with a vertical gradient from white to black. " +"A TransparencyMaskBitmap with a vertical gradient that changes from 0 " +"(opaque) to 1 (transparent) back to 0 (opaque) provides falloffs " +"from spectrum colors in the center up to white and down to black backgrounds.", tf, rc, TextAlignment.Center, ParagraphAlignment.Center); return bmp; } }}