【发布时间】:2015-03-26 15:14:14
【问题描述】:
我的目标是创建一个具有多种颜色的对角渐变作为椭圆的背景。可以使用下面的代码完成水平或垂直渐变。我基本上用渐变阴影绘制矩形并从中剪出一个椭圆。
这是我的水平渐变:
水平渐变的代码:
var cb = writer.DirectContent;
var boundingBox = new Rectangle(200, 200, 700, 350);
//draw a path, for example an ellipse
cb.Ellipse(boundingBox.Left, boundingBox.Bottom, boundingBox.Right, boundingBox.Top);
cb.Clip();//set clipping mask
cb.NewPath();
//gradient 1: yellow to gray
var gradientRect1 = new Rectangle(200, 200, 400, 350);
PdfShading shading = PdfShading.SimpleAxial(writer, gradientRect1.Left, gradientRect1.Bottom + (gradientRect1.Height / 2), gradientRect1.Right, gradientRect1.Bottom + (gradientRect1.Height / 2), BaseColor.YELLOW, BaseColor.DARK_GRAY);
var pattern = new PdfShadingPattern(shading);
gradientRect1.BackgroundColor = new ShadingColor(pattern);
cb.Rectangle(gradientRect1);
cb.Fill();
//gradient 2, gray to red
var gradientRect2 = new Rectangle(400, 200, 700, 350);
PdfShading shading2 = PdfShading.SimpleAxial(writer, gradientRect2.Left, gradientRect2.Bottom + (gradientRect2.Height / 2), gradientRect2.Right, gradientRect2.Bottom + (gradientRect2.Height / 2), BaseColor.DARK_GRAY, BaseColor.RED);
var pattern2 = new PdfShadingPattern(shading2);
gradientRect2.BackgroundColor = new ShadingColor(pattern2);
cb.Rectangle(gradientRect2);
cb.Fill();
有谁知道是否可以旋转渐变以显示对角渐变?感谢您的帮助!
【问题讨论】:
-
您使用的着色轴坐标表示水平轴。您是否尝试过使用非水平轴?
-
是的,可以指定对角坐标,但这仅适用于一种颜色过渡。
标签: c# itextsharp linear-gradients