SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

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

Open waibuku opened this issue 2 months ago • 1 comments

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: <svg width="70mm" height="50mm" viewBox="0 0 265 189" xmlns="http://www.w3.org/2000/svg"> <image href="data:image/jpeg;base64,/9j/4AAQSkZJRg..." x="10.388" y="22.387" width="39.99997292" height="20.00001292" preserveAspectRatio="xMidYMid meet"/>

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

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

waibuku avatar Oct 31 '25 06:10 waibuku

Triage Summary

Labels will be applied to identify the affected areas (PDF and XPS rendering) and the specific outcome (SkiaSharp functions and reliability issues).

This issue is not identified as a regression based on the provided information, as it seems to describe ongoing rendering challenges rather than a breakdown of previously functional behavior.

Additional remarks:

  • The issue specifically highlights discrepancies between PDF and XPS rendering in a Windows environment.
Detailed Summary and Actions

Summary of the triage:

  • The issue affects rendering in both PDF and XPS formats.
  • The SKDocument.CreateXps() function from SkiaSharp is the focal point of the problem, related to image rendering from SVGs.
  • The problems are noted to occur specifically on Windows 10/11 and revolve around image scaling issues.

Summary of the actions that will be performed:

Action Item Description
Apply Label backend/PDF The issue involves rendering images to PDF format, which behaves correctly, distinguishing it from XPS.
Apply Label backend/Xps The issue specifically affects the XPS rendering, causing incorrect display of images.
Apply Label area/SkiaSharp This pertains to the SkiaSharp function SKDocument.CreateXps() dealing with embedded SVG images.
Apply Label os/Windows-Classic This relates to rendering discrepancies occurring on Windows 10/11 platforms.
Apply Label tenet/reliability The issue involves unexpected behaviors in image scaling and reliability in rendering processes.

This entire triage process was automated by AI and mistakes may have been made. Please let us know so we can continue to improve.

github-actions[bot] avatar Oct 31 '25 06:10 github-actions[bot]