How to add WPF support to .NET 5 Winforms project

Viewed 1772

I'm converting an old .NET Framework 4 WinForms application to .NET 5. In this case, the easiest way was to re-create the project, which mostly worked. However, I have a method to decode a JPG image from a memory stream:

            // Look for JFIF header
            MemoryStream memStream = new MemoryStream( rawData );
            JpegBitmapDecoder decoder = new JpegBitmapDecoder( memStream,
                BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default );
            BitmapSource bmpSource = decoder.Frames[0];
            Bitmap bitmap = new Bitmap( bmpSource.PixelWidth, bmpSource.PixelHeight,
                PixelFormat.Format24bppRgb );

This fails because VS 2019 complains that "using System.Windows.Media.Imaging" causes an error, and JpegBitmapDecoder shows with a red squiggly. If I hover over JpegBitmapDecoder, I'm given the option for "using System.Windows.Media.Imaging; from PresentationCode".

However, choosing that causes VS 2019 to be stuck on "Execute Suggested Action", trying to add the using.

The project is targeted for .NET 5, and I've added the Microsoft.Windows.Compatibility NuGet package, so what am I doing wrong? Or, how to I access the JpegBitmapDecoder?

2 Answers

You write that you're using Visual Studio 2019 (e.g. version 16.7.6). However, as far as I know, .NET 5 projects are supported only in Visual Studio 2019 Preview (i.e. version 16.8.0 Preview).

Note: .NET 5 projects are supported only in version 16.8.0 of Visual Studio 2019 and later (I only just realized that the "Preview" had made it to released status).

When I try to reproduce your problem in Visual Studio 2019 Preview, I get a slightly different issue: when I select the "using System.Windows.Media.Imaging; from PresentationCode" menu item, a progress dialog shows up:

"using System.Windows.Media.Imaging; (from PresentationCore)" progress dialog

This dialog seems to go on forever. Visual Studio never seems to actually complete the action.

So that part of the feature seems broken. However, fortunately you can do the same thing manually that Visual Studio should have done for you. Simply select the project, and then choose "Edit Project File" from the "Project" menu, and add <UseWPF>true</UseWPF> to the main project <PropertyGroup> element.

When you're done, the project file might look something like this:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

At that point, the entry "Microsoft.WindowsDesktop.App.WindowsForms" under the "Frameworks" item in the project's "Dependencies" WPF libraries should change to "Microsoft.WindowsDesktop.App" and will include both the Winforms and WPF libraries that you need.

You can go the other way as well, adding Winforms support to an existing WPF project, simply by adding the <UseWindowsForms/> element instead.

Of course, this all begs the question why you don't just use the GDI+ image handling types. Winforms can load JPEG files just as well as WPF, so why you want the dependency on WPF is unclear. But there are other reasons to mix and match too. Whatever the reason you want to combine Winforms and WPF in a .NET 5 project, the above will do it.

I found the answer. Apparently, I have to add a UseWPF item in the project file, since PresentationCore is really a WPF library. Doing so fixed the problem.

Related