Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ImageSource In .NET Core 3.1?

Tags:

c#

.net-core

wpf

I'm trying to use System.Windows.Media.ImageSource inside of a console application that targets netcoreapp3.1. Looking at the documentation from Microsoft here it looks like it's available for .NET Core 3.0 and 3.1

Applies to NET Core 3.1 3.0

My sample code will run just fine when it's a net472 but as soon as I change back to netcoreapp3.1 it will fail to build.

Demo App

using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageSourceTesting
{
    internal static class Program
    {
        private static void Main()
        {
            ImageSource myImage = new BitmapImage();
            Console.Write(myImage);
        }
    }
}

When attempting to build I see the following:

[CS0234] The type or namespace name 'Media' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

like image 854
Rob P. Avatar asked Jan 26 '26 05:01

Rob P.


1 Answers

System.Windows.Media.ImageSource is a part of WPF, so if you created console application, then you have to add WPF support in project using UseWPF tag like in example:

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

    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <UseWPF>true</UseWPF>
    </PropertyGroup>

   </Project>
like image 181
Lukasz Szczygielek Avatar answered Jan 27 '26 19:01

Lukasz Szczygielek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!