[A]System.Drawing.Bitmap cannot be cast to [B]System.Drawing.Bitmap

I'm trying to port my custom visualizer from LINQPad 5 to LINQPad 6 and I get the following exception when I try to access one of the images in the Resources file that I have:

[A]System.Drawing.Bitmap cannot be cast to [B]System.Drawing.Bitmap. Type A originates from 'System.Drawing.Common, Version=, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' in the context 'Default' at location 'C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\5.0.0\System.Drawing.Common.dll'. Type B originates from 'System.Drawing.Common, Version=, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' in the context 'Default' at location 'C:\Users\Giorgi\AppData\Local\Temp\LINQPad6\_iybtcson\shadow-13\System.Drawing.Common.dll'.

at System.Runtime.CompilerServices.CastHelpers.ChkCast_Helper(Void* toTypeHnd, Object obj) at QueryPlanVisualizer.LinqPad6.SqlServerResources.get_qp_icons() in D:\src\queryplanvisualizer\src\QueryPlanVisualizer.LinqPad6\SqlServerResources.Designer.cs:line 108 at QueryPlanVisualizer.LinqPad6.QueryPlanVisualizer.DumpPlanInternal[T](IQueryable`1 queryable, Boolean dumpData, Boolean addNewPanel) in D:\src\queryplanvisualizer\src\QueryPlanVisualizer.LinqPad6\QueryPlanVisualizer.cs:line 80 at QueryPlanVisualizer.LinqPad6.QueryPlanVisualizer.DumpPlan[T](IQueryable`1 queryable, Boolean dumpData) in D:\src\queryplanvisualizer\src\QueryPlanVisualizer.LinqPad6\QueryPlanVisualizer.cs:line 17

Any suggestions?


  • edited February 26

    I "solved" the issue by embedding the image as "Embedded Resource" but now I get this exception when trying to set an image for button:

    Method not found: 'Void System.Windows.Forms.ButtonBase.set_Image(System.Drawing.Image)'.

  • It sounds like your visualizer project is targeting .NET Framework instead of .NET 5 or .NET Core 3. Because LINQPad 6 targets .NET 5 / .NET Core 3, your visualizer must do the same.

    If you want to have a single project that supports both LINQPad 5 (.NET Framework) and LINQPad 6 (.NET 5 / .NET Core 3), you can multi-target. Here's an example of a .csproj file that multi-targets .NET Framework 4.6 and .NET Core 3.1. It will generate two output folders (bin\net46 and bin\netcoreapp3.1):

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
        <PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
        <ItemGroup Condition="'$(TargetFramework)' == 'net46'">
            <Reference Include="System.Xaml">
            <Reference Include="WindowsBase" />
            <Reference Include="PresentationCore" />
            <Reference Include="PresentationFramework" />

    Notice the conditional item groups. Any references to framework libraries such as System.Drawing.dll should exist only in the net46 item group, and not in the netcoreapp group.

  • edited February 27

    My visualizer targets .net core but the error happens only when using a typed context from my own assembly that has a dependency on Microsoft.EntityFrameworkCore.SqlServer

  • This happens in LINQPad without referencing the visualizer but using a typed context that doesn't reference anything apart from Microsoft.EntityFrameworkCore.SqlServer

Sign In or Register to comment.