WPF图像矢量格式导出(XPS?)

j_m*_*aly 5 wpf png export image xps

我们的工具允许导出到PNG,非常好用.现在,我想将导出添加到某种矢量格式.我尝试了XPS,但结果并不令人满意.

看一下比较http://www.jakubmaly.cz/xps-vs-png.png.左边的图片来自XPS导出,PNG导出右边的图片,XPS图片在XPS Viewer中打开并且缩放100%时明显模糊.

有没有我缺少的设置或为什么会这样?

谢谢,Jakub.

可以在此处找到示例xps输出:http://www.jakubmaly.cz/files/a.xps.这是执行XPS导出的代码:

if (!boundingRectangle.HasValue)
{
    boundingRectangle = new Rect(0, 0, frameworkElement.ActualWidth, frameworkElement.ActualHeight);
}

// Save current canvas transorm
Transform transform = frameworkElement.LayoutTransform;
// Temporarily reset the layout transform before saving
frameworkElement.LayoutTransform = null;


// Get the size of the canvas
Size size = new Size(boundingRectangle.Value.Width, boundingRectangle.Value.Height);
// Measure and arrange elements
frameworkElement.Measure(size);
frameworkElement.Arrange(new Rect(size));

// Open new package
System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(filename, FileMode.Create);
// Create new xps document based on the package opened
XpsDocument doc = new XpsDocument(package);
// Create an instance of XpsDocumentWriter for the document
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
// Write the canvas (as Visual) to the document
writer.Write(frameworkElement);
// Close document
doc.Close();
// Close package
package.Close();

// Restore previously saved layout
frameworkElement.LayoutTransform = transform;
Run Code Online (Sandbox Code Playgroud)

Nés*_* A. 2

我也遇到过类似的问题。当通过固定文档传递到 XPS 时,我的图像非常模糊。解决方案是将图像直接写入XPS...

/// <summary>
/// Saves the supplied visual Source, within the specified Bounds, as XPS in the specified File-Name.
/// Returns error message or null when succeeded.
/// </summary>
public static string SaveVisualAsXPS(Visual Source, Size Bounds, string FileName)
{
    string ErrorMessage = null;

    try
    {
        using (var Container = Package.Open(FileName, FileMode.Create))
        {
            using (var TargetDocument = new XpsDocument(Container, CompressionOption.Maximum))
            {
                var Writer = XpsDocument.CreateXpsDocumentWriter(TargetDocument);
                var Ticket = GetPrintTicketFromPrinter();
                if (Ticket == null)
                    return "No printer is defined.";

                Ticket.PageMediaSize = new PageMediaSize(Bounds.Width, Bounds.Height);
                var SourceVisual = Source;
                Writer.Write(SourceVisual, Ticket);
            }
        }
    }
    catch (Exception Problem)
    {
        ErrorMessage = "Cannot export document to XPS.\nProblem: " + Problem.Message;
    }

    return ErrorMessage;
}
Run Code Online (Sandbox Code Playgroud)

提供具有精确宽度和高度的打印票可以避免缩放(这就是我想要的)。从以下示例中获取该函数: http://msdn.microsoft.com/en-us/library/system.printing.printticket.aspx