ImageTransparency.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos
{
// 该示例演示了 GcDocs.Pdf 渲染的能力
// 使用指定透明度(不透明度)的图像。
public class ImageTransparency
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
var rc = Common.Util.AddNote(
"GcPdfGraphics.DrawImage() 方法允许渲染具有指定不透明度的图像。" +
"",
page);
var tl = g.CreateTextLayout();
tl.DefaultFormat.Font = StandardFonts.Times;
tl.DefaultFormat.FontSize = 12;
tl.MaxWidth = page.Size.Width;
tl.MaxHeight = page.Size.Height;
tl.MarginAll = 36;
tl.MarginTop += rc.Bottom;
tl.Append(Common.Util.LoremIpsum(2));
tl.PerformLayout(true);
g.DrawTextLayout(tl, PointF.Empty);
using (var image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "puffins.jpg")))
{
var imageRc = new RectangleF(tl.MarginLeft, tl.MarginTop, 144, 144);
// 不透明度0.2:
g.DrawImage(image, imageRc, null, ImageAlign.ScaleImage, 0.2f);
imageRc.Offset(imageRc.Width + 36, 0);
// 不透明度0.5:
g.DrawImage(image, imageRc, null, ImageAlign.ScaleImage, 0.5f);
imageRc.Offset(imageRc.Width + 36, 0);
// 不透明度 1(默认):
g.DrawImage(image, imageRc, null, ImageAlign.ScaleImage);
// 注意:我们必须在处理文档中使用的图像之前保存文档:
doc.Save(stream);
}
return doc.Pages.Count;
}
}
}