Skip to content

[Question] [XPS] Drawing a Visual is always being rasterized #4466

Open

Description

@wstaelens

XPS: When drawing a Visual on a DrawingContext of a FixedPage, the image is being rasterized and losing the Vector quality.

DrawingContextWrapper cntxt;
Visual visualToDraw;
// ...

var drawing = VisualTreeHelper.GetDrawing(visualToDraw); // <-- vector
System.Windows.Media.ImageSource image = new DrawingImage(drawing); 
cntxt.Canvas.DrawImage(image, drawingBounds.ToRect()); // <-- raster

How to draw a Visual on a DrawingContext as a vector without it being rasterized (XPS)?

(found something related but not really the same: https://stackoverflow.com/questions/6490576/wpf-image-vector-format-export-xps/ )

Activity

  1. miloush commented on May 4, 2021

    @miloush
    Contributor

    Do you have a simple repro? Does adding the visual directly to the FixedPage instead of rendering it on its DrawingContext work for you? (I tried with an ellipse but it looked vector in a DocumentViewer)

  2. wstaelens commented on May 10, 2021

    @wstaelens
    ContributorAuthor

    I don't have a separate solution with a small repro but the code already posted above shows the problem.

    1. First you have to open an XPS document
    2. Get a FixedPage from the document
    3. Pass that FixedPage as a visual to the code below
    4. The code below takes the vector drawing via the VisualTreeHelper.GetDrawing. When we try to draw the Visual on a different DrawingContext of a different FixedPage it gets rasterized.
      When drawing the Visual on the same DrawingContext of the FixedPage it is Vector/good quality.
    DrawingContextWrapper cntxt;
    Visual visualToDraw;
    // ...
    
    var drawing = VisualTreeHelper.GetDrawing(visualToDraw); // <-- vector
    System.Windows.Media.ImageSource image = new DrawingImage(drawing); 
    cntxt.Canvas.DrawImage(image, drawingBounds.ToRect()); // <-- raster
    
  3. added this to the 7.0.0 milestone on Sep 2, 2021
  4. wstaelens commented on Oct 28, 2021

    @wstaelens
    ContributorAuthor

    any updates?

  5. wstaelens commented on Jan 28, 2022

    @wstaelens
    ContributorAuthor

    🥱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions