Skip to content

[BUG] SKDocument.CreateXps() incorrectly scales <image> elements from SVG, while CreatePdf() renders correctly #3400

@waibuku

Description

@waibuku

Description

When converting the same SVG content to XPS and PDF using SkiaSharp, elements (embedded images) are rendered with incorrect dimensions in XPS output, while they appear correct in PDF output. All other SVG elements (text, barcodes, rectangles, shapes) render correctly in both formats.

SkiaSharp Version: [3.119.1]
Svg.Skia Version: [3.2.1]
Platform: Windows 10/11
Target Framework: .NET 9.0
Language: C#

Steps to Reproduce
Load an SVG document containing elements with Base64-encoded images
Parse the SVG using SKSvg.Load()
Generate XPS using SKDocument.CreateXps()
Generate PDF using SKDocument.CreatePdf() with the same SVG
Compare the image dimensions in XPS vs PDF output

Code

using SkiaSharp;
using Svg.Skia;
using System.IO;
using System.Text;

// Load SVG
var svgContent = File.ReadAllText("sample.svg");
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(svgContent));
var skSvg = new SKSvg();
var picture = skSvg.Load(ms);
var bounds = picture.CullRect;

const float DPI = 72f;
const float SVG_DPI = 96f;
const float contentScale = DPI / SVG_DPI; // 0.75

var pageWidth = bounds.Width * contentScale;
var pageHeight = bounds.Height * contentScale;

// Generate PDF (works correctly)
using (var pdfStream = File.OpenWrite("output.pdf"))
{
using var document = SKDocument.CreatePdf(pdfStream);
using var canvas = document.BeginPage(pageWidth, pageHeight);
canvas.Clear(SKColors.White);
canvas.Scale(contentScale);
canvas.DrawPicture(picture);
document.EndPage();
document.Close();
}

// Generate XPS (images are incorrectly scaled)
using (var xpsStream = File.OpenWrite("output.xps"))
{
using var document = SKDocument.CreateXps(xpsStream);
using var canvas = document.BeginPage(pageWidth, pageHeight);
canvas.Clear(SKColors.White);
canvas.Scale(contentScale);
canvas.DrawPicture(picture);
document.EndPage();
document.Close();
}

Sample SVG Structure:

Expected Behavior

Images in XPS output should have the same dimensions and appearance as in PDF output. All SVG elements should render consistently across both formats.

This issue only affects elements within SVG
The problem appears to be in how SKDocument.CreateXps() handles image rendering from SKPicture
The same SKPicture object produces correct output with CreatePdf() and CreateImage()
This severely impacts label printing applications that require XPS output for Windows print pipeline

Actual Behavior

PDF Output: ✅ Images render correctly with proper dimensions
XPS Output: ❌ Images are significantly smaller (approximately 30-40% of correct size)
Other Elements: ✅ Text, barcodes, shapes render correctly in both PDF and XPS

Version of SkiaSharp

3.116.0 (Current)

Last Known Good Version of SkiaSharp

2.88.9 (Previous)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Windows

Platform / Operating System Version

No response

Devices

No response

Relevant Screenshots

No response

Relevant Log Output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/SkiaSharpIssues that relate to the C# binding of SkiaSharp.backend/PDFbackend/Xpsos/Windows-ClassicIssues running on Microsoft Windows using Win32 APIs (Windows.Forms or WPF)tenet/reliabilityIssues relating to crashes or unexpected behaviorstype/bug

    Type

    No type

    Projects

    Status

    New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions