Home > database >  Must add reference to WindowsBase.dll to use RenderTargetBitmap.Render method?
Must add reference to WindowsBase.dll to use RenderTargetBitmap.Render method?

Time:12-10

I'm currently working on a WPF application that uses a plotting library called Live Charts for WPF. I want to save a PNG of my graph, which is described on their github page Save Plot Example also discussed at this stackoverflow question here. The problem is adding a reference to the windows base assembly.

`

private void SaveToPng(FrameworkElement visual, string filename)
{
    var encoder = new PngBitmapEncoder();
    EncodeVisual(visual, filename, encoder);
}

private static void EncodeVisual(FrameworkElement visual, string fileName, BitmapEncoder encoder)
{
    var bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
    //bitmap.Render();
    bitmap.Render(visual);
    var frame = BitmapFrame.Create(bitmap);
    encoder.Frames.Add(frame);
    using (var stream = File.Create(fileName)) encoder.Save(stream);
}

`

I have tried to add this dll as a reference which is located at C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0, but when I do this it does not work. It is actually implicitly included in the project, so that may be why I can add it explicitly like I did with the PresentationCore.dll and PresentationFramework.dll which were also required and solved a few errors. The error states: the type 'System.Windows.Freezable' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. This is shown by hovering over the call to bitmap.Render(visual), and when hovering over encoder.Save(stream) as shown in the picture and in the above code. I believe resolving this dependency would fix the problem, but I cannot figure out how. Thank you. Picture of Project

CodePudding user response:

Discovered what I believe is the answer to the problem. I was creating the SaveToPNG and EncodeVisual methods inside of a class library which would be used in the WPF project. The class library is unable to accept WindowsBase as an explicit reference, but the WPF project itself can. It will take some restructuring of the code to implement this in the WPF project itself, but it seems to be the way to resolve this problem.

CodePudding user response:

I am using .NET 6. Here is the contents of my csproj file in my class library:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup />
  <ItemGroup>
    <Compile Update="Core Library\Port.cs">
      <SubType>Component</SubType>
    </Compile>
  </ItemGroup>
</Project>

I did not see the attributes that you spoke of, and autocomplete could not reference them in this file. Inside of the IDE (Rider) I was able to target net6.0-windows in the properties menu of the project. I then reinserted the code into the class library, but the same error still occurred. I agree with you that it shouldn't be necessary to reference WindowsBase, which isn't necessary when placing the code in the wpf project itself, but in the class library I also had to reference PresentationCore and PresentationFramework, which did take care of multiple errors.

  • Related