Skip to content

System.AccessViolationException thrown by PrintDocument.Print when platform is X86 #76538

@gcaughey

Description

@gcaughey

Description

PrintDocument.Print works fine for x86 builds with .NET 6.0.9 but throws exception in .NET 7 RC1. This only happens if the platform is x86.

Reproduction Steps

Create new WPF desktop application.

Change csproj so it looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

Add a button to MainWindow and change MainWindow.xaml.cs so it looks like this:

using System.Drawing;
using System.Drawing.Printing;
using System.Windows;


namespace TestPrintNet7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string text = "Hello world";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (PrintDocument doc = new PrintDocument())
            {
                doc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                doc.Print();   // exception here!!!
                doc.PrintPage -= doc_PrintPage;
            }
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            using (Font font = new Font("Consolas", 9, System.Drawing.FontStyle.Regular))
            {
                e.Graphics.DrawString(text, font, System.Drawing.Brushes.Black, 0, 0);
            }
        }
    }
}

Expected behavior

Text should be printed to file selected by Microsoft Print to PDF printer.

Actual behavior

Exception thrown on the line
doc.Print();

Regression?

Works fine in .NET 6.

Known Workarounds

none

Configuration


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
	  <UseWindowsForms>true</UseWindowsForms>
	  <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

The problem only happens on .NET 7 with x86 platform target.

Tested on Windows build 25211.1001. The machine architecture is x64.

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions