【问题标题】:SVG gradient file renders to Graphics incorrectlySVG 渐变文件不正确地渲染到图形
【发布时间】:2021-03-26 02:06:00
【问题描述】:

我们正在动态生成指定大小的图像文件。共有 3 层:

  • 基础图像层(加载jpeg,将其调整为所需大小,用作位图)
  • 渐变图像层(加载 SVG,调整大小,在基础图像层上渲染)
  • 文本层(在基础图像层上渲染 + SVG 渐变)

问题在于 SVG 层,与原始层相比,它的呈现方式不同:

SVG 图层渲染代码如下

private void RenderGradientLayer(Graphics mainGraphics, ImageDrawSettings settings)
{
    var gradientSvg = SvgDocument.Open(settings.FullGradientFilePath); //d:\path-to-file.svg
    var gradientBitmap = gradientSvg.Draw(settings.TargetSize, settings.TargetSize); //500x500
    mainGraphics.DrawImage(gradientBitmap, 0, 0);
}

图形设置

private static void Init(Graphics graphics)
{
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphics.CompositingQuality = CompositingQuality.HighQuality;
    graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
}

SVG 内容

<svg width="144" height="144" viewBox="0 0 144 144" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="144" height="144" fill="url(#paint0_linear)"/>
<defs>
<linearGradient id="paint0_linear" x1="72" y1="144" x2="72" y2="2.40787e-07" gradientUnits="userSpaceOnUse">
<stop stop-opacity="0.8"/>
<stop offset="1" stop-color="#C4C4C4" stop-opacity="0"/>
</linearGradient>
</defs>
</svg>

是否有任何想法导致 SVG 无法正确呈现?

更新: 找到原因,贴在下面。

【问题讨论】:

  • 一个 SVG 文件是 XML 格式并且是文本。问题可能出在 SVG 文件或机器的图形模式(显卡)中(支持默认设置和图像)。
  • 你的函数gradientSvg.Draw()是做什么的?是来自某个外部图书馆吗?另外,请显示 SVG 文件的源文本。
  • @ccprog Draw() 是添加了 SvgDocument(Assembly Svg, Version=3.1.0.0) SVG 内容的方法

标签: c# svg bitmap render gradient


【解决方案1】:

好的,我找到了原因,它是图形设置:

graphics.CompositingQuality = CompositingQuality.HighQuality;

删除那条线可以正确渲染渐变

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-06
    • 2021-10-24
    • 2012-12-09
    • 2014-06-30
    • 2015-11-19
    • 2021-10-29
    相关资源
    最近更新 更多