0

I am having a C# WPF desktop app project on .NET6 in Visual Studio.

Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="7.0.0" />
  </ItemGroup>
</Project>

I've installed the package System.Drawing.Common from nuget for this project. The package seems to be in the project dependencies. There is a <PackageReference> to it. This strange error

CS0234
The type or namespace name 'Common' does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)

makes me crazy.

I don't understand if it's some weird conflict with the namespace of the package System.Drawing or something else. Visual Studio as always gives no clues, no solutions. ChatGPT has nothing to say about this except that I should add <PackageReference> to it. Forums don't seem to have anything about this issue. Please could you help me understand. I am new to C#. It is so strange I struggle to simply add a pretty standard package to a very simple project.

Alex
  • 707
  • 1
  • 4
  • 9
  • There is nothing called `Common` in this package. Please show the code where you refer to the `Common` entity. Most likely this entity belongs to a different namespace. – Serg Jul 22 '23 at 00:07
  • As I understand it, `.Common` is an embedded namespace of the package `System.Drawing.Common` (https://www.nuget.org/packages/System.Drawing.Common/). The error above appears when I write `using System.Drawing.Common;`. If I omit this line, it starts to give an error for this code: `BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new System.IO.MemoryStream(Convert.FromBase64String(base64Image)); bitmapImage.EndInit(); MyImage.Source = bitmapImage;` saying that "The name 'bitmapImage' does not exist in the current context" – Alex Jul 22 '23 at 01:06
  • 2
    `BitmapImage` is from `System.Windows.Media.Imaging`. What has GDI+ to do with this? -- If, for some reason, you need GDI+ (not that it appears you do), add `true` to the Project file – Jimi Jul 22 '23 at 01:11
  • @Jimi, thank you for pointing out that BitmapImage is from System.Windows.Media.Imaging. I have `using System.Windows.Media.Imaging;` as well. So it is even more weird the compiler can't see the `BitmapImage` token. Apparently I added `Common` to use `Bitmap` in the following code: `Bitmap bitmap; using(MemoryStream memoryStream=new MemoryStream()){ BmpBitmapEncoder encoder=new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapImage)); encoder.Save(memoryStream); bitmap=new Bitmap(memoryStream);} string filePath=iconPath; bitmap.Save(filePath,ImageFormat.Icon);` – Alex Jul 22 '23 at 10:31
  • I realised that the latter error was a complaint not about the class `BitmapImage` but of the variable (its object) `bitmapImage`, which I created in another method. Totally removing the `using System.Drawing.Common;` helped to compile the project. Thank you. – Alex Jul 22 '23 at 11:11
  • 1
    As mentioned, if you need a System.Drawing.Bitmap, then add `true` to the Project's file (where you also have `true`). After that, you can reference the `System.Drawing` namespace -- If you need to generate ICON files in WPF, you may try [HL.IconPro](https://github.com/HerbertLausmann/HL.IconPro) – Jimi Jul 22 '23 at 11:53

0 Answers0