Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not load file or assembly System.Drawing or one of its dependencies" error on .Net 2.0, VS2010 and Windows 8

I am getting a FileNotFoundException on a Windows Forms Application project, with the following message:

Could not load file or assembly 'System.Drawing, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 
The system cannot find the file specified.

To replicate the problem:

  • Select New, Project, choose .Net Framework 2.0 as the target and pick Windows Forms Application as the project type.
  • On the properties of the form created by default, select a value for the Icon property. Any .ico file will do. This will embed the file on the resx file.
  • Compile and run the application.

When I do this, the program stops on the line this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); with the following exception:

System.IO.FileNotFoundException was unhandled
  Message=Could not load file or assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

I’m getting this on Visual Studio 2010 SP1, recently installed on Windows 8 Developer Preview. If I change the project properties to target .Net Framework 4, the error goes away.

On the Form1.resx file, I can see that the version of the System.Drawing assembly is explicitly stated as 2.0:

<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

Any ideas?

like image 535
Leonardo Avatar asked Feb 08 '12 09:02

Leonardo


3 Answers

This is a bug. I have seen it too. It happens because your .resx file is pointing to 4.0.0.0 version of System.Drawing where one does not exist. To overcome this problem i usually edit the .resx in notepad to change 4.0.0.0 to 2.0.0.0. The bug is introduced by following the exact steps that you have outlined.

like image 53
Sudarshan chandan Avatar answered Sep 28 '22 11:09

Sudarshan chandan


I've found a possible solution, please try this:

Open the resx File in the Designer and set the accessmodifier from public to no code generation.

Edit: there's a workaround, but very annoying though.

  1. Open Form in Designer and make needed GUI changes. Close designer and save
  2. Compile project and receive RESX compile error (only forms with Imagelist should have this problem)
  3. Double-click resx compile error to open resx file.
  4. Scroll to top of imagestream.
  5. Edit the top line of the Image stream: AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w TO AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w
  6. Close and save resx file and recompile.

**NOTE: the only difference are the characters at end "j00LjAuMC4w' to "j0yLjAuMC4w" This needs to be done EVERY TIME you open the form in Designer mode.

Microsoft says they are going to fix it in the next VS version...

Source: http://connect.microsoft.com/VisualStudio/feedback/details/532584/error-when-compiling-resx-file-seems-related-to-beta2-bug-5252020

like image 39
vulkanino Avatar answered Sep 28 '22 11:09

vulkanino


This problem can occur if .net 4.5 preview resgen is used to create the resource files.

I have the same problem on my laptop(Windows 7, VS2010 Premium, VS11 Developer Preview). I got this problem with a simple forms project when i say 'localizable=true' on a form. In my case are no image data involved. The project is set to .net 3.5

private void InitializeComponent()
{
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
    this.SuspendLayout();
    // 
    // Form1
    // 
    resources.ApplyResources(this, "$this"); //exception Could not load file or assembly 'System.Drawing, Version=4.0.0.0, 

If i then copy this project to another machine(Windows 7, VS2010 Premium) and try to debug it, the error remains. The error goes away if i clean the solution(not the project)(or delete bin/obj by hand) If i then copy this solution back to the my laptop, the error is gone, but i cant see the form again in design view 'Error message: at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)'

The reason for all this seems to be the .net version in the *.Designer.cs files.

  1. Runtime Version: 4.0.30319.239 on the computer where it works,
  2. Runtime Version: 4.0.30319.17020 on the laptop where i get the exception.

Can anyone tell me where i can configure which resgen version is used when dealing with .net 3.5 projects?

like image 41
Manuel Avatar answered Sep 28 '22 12:09

Manuel