Skip to content

Error creating XPS from Canvas #1985

Closed
Closed

Description

@EmaginationStore
  • .NET Core Version: 3.0
  • Windows version: 1903 (18362.356)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No

Description

I have a Textblock with text of "Hello" within a Canvas on the MainWindow. I am running the following code on mouse down of the main window (as a test):

private void MainWindow_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var sfd = new SaveFileDialog { Filter = "XPS files (*.xps)|*.xps" };
            if (sfd.ShowDialog() != true) return;
            var c = Canv;
            var doc = new XpsDocument(sfd.FileName, FileAccess.Write);
            var writer = XpsDocument.CreateXpsDocumentWriter(doc);
            writer.Write(c, new PrintTicket { PageMediaSize = new PageMediaSize(c.ActualWidth, c.ActualHeight) });
            doc.Close();
        }

Actual behavior:
Entries cannot be created while previously created entries are still open.
at System.IO.Compression.ZipArchive.AcquireArchiveStream(ZipArchiveEntry entry)
at System.IO.Compression.ZipArchiveEntry..ctor(ZipArchive archive, String entryName)
at System.IO.Compression.ZipArchiveEntry..ctor(ZipArchive archive, String entryName, CompressionLevel compressionLevel)
at System.IO.Compression.ZipArchive.DoCreateEntry(String entryName, Nullable`1 compressionLevel)
at System.IO.Compression.ZipArchive.CreateEntry(String entryName, CompressionLevel compressionLevel)
at System.IO.Packaging.ZipPackage.CreatePartCore(Uri partUri, String contentType, CompressionOption compressionOption)
at System.IO.Packaging.Package.CreatePart(Uri partUri, String contentType, CompressionOption compressionOption)
at System.Windows.Xps.Packaging.XpsManager.GeneratePart(ContentType contentType, Uri partUri)
at System.Windows.Xps.Packaging.XpsManager.GenerateUniquePart(ContentType contentType)
at System.Windows.Xps.Packaging.XpsFixedDocumentSequenceReaderWriter.AddFixedDocument()
at System.Windows.Xps.Serialization.XpsPackagingPolicy.AcquireXmlWriterForFixedDocument()
at System.Windows.Xps.Serialization.XpsSerializationManager.AcquireXmlWriter(Type writerType)
at System.Windows.Xps.Serialization.ReachHierarchySimulator.SimulateBeginFixedDocument()
at System.Windows.Xps.Serialization.ReachHierarchySimulator.BeginConfirmToXPSStructure(Boolean mode)
at System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
at System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
at System.Windows.Xps.XpsDocumentWriter.Write(Visual visual, PrintTicket printTicket)
at Test.MainWindow.MainWindow_OnMouseDoubleClick(Object sender, MouseButtonEventArgs e) in C:\Users\ashdo\source\repos\Test\Test\MainWindow.xaml.cs:line 27
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()
at Test.App.Main()

Expected behavior: An XPS document to be created of the Canvas

Minimal repro:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" MouseDown="MainWindow_OnMouseDoubleClick">
    <Grid>
        <Canvas Name="Canv">
                    <TextBlock FontSize="200" Foreground="BlueViolet" Text="Hello" >
                    </TextBlock>
        </Canvas>
    </Grid>
</Window>

Activity

  1. added
    Bug
    on Oct 3, 2019
  2. added this to the 3.1 milestone on Oct 3, 2019
  3. added
    rank20
    on Oct 8, 2019
  4. rladuca commented on Oct 14, 2019

    @rladuca
    Member

    The underlying zip implementation does not support the stream being kept open when opening it in write mode.

    This seems to be just another tail end bug of switching zip implementations.

    @EmaginationStore can you try opening with FileAccess.ReadWrite? That allowed me to continue properly. I believe the reason for this is the underlying stream does specific things when in Create mode.

    https://github.com/dotnet/corefx/blob/967eb3c5cb5fd36c8bb56d06f4c840f6a7a5a754/src/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs#L138

    Setting the FileAccess to ReadWrite makes this function in Update mode, which skirts the issue.

    There are changes needed in WPF for this, but you should be able to workaround this in the meantime.

  5. added
    regression
    tenet-compatibility
    on Oct 14, 2019
  6. EmaginationStore commented on Oct 15, 2019

    @EmaginationStore
    Author

    I can confirm that creating the XpsDocument with ReadWrite file mode did allow me to create the XPS document. Thank you

  7. rladuca commented on Oct 18, 2019

    @rladuca
    Member

    After some investigation, we have several assumptions in the WPF XPS document stack that rely on being able to simultaneously add to a ZipArchive. Specifically, the serialization managers will do something akin to this:

    and

    Basically, this sort of split logic keeps the underlying streams open until the entire set of tags are written out. This design persists down into the packaging policies that are used to manage the PackageParts.

    In the past, we have fixed several instances of places where we were not closing these streams eagerly, but this case is a bit more complicated. We'll need to think more carefully about if/how to change the serialization design to conform to the new requirements from System.IO.Compression.

    In the meantime, I think the best fix for 3.1+ would be to implement the workaround here in the code itself. When an XPSDocument is created with FileAccess.Write, we should promote that to FileAccess.ReadWrite so that the ZipArchive ends up in Update mode and can be manipulated as it was in .NET Framework.

    We can revisit the design of the serialization stack for 5.0. I'll file another bug for that when this one is completed.

  8. rladuca commented on Oct 22, 2019

    @rladuca
    Member

    Merged for 5.0 and 3.1

  9. ghost locked as resolved and limited conversation to collaborators on Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Bugrank20regressiontenet-compatibility

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions