Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad image from resource in .net requires unused references
    primarykey
    data
    text
    <p>short story: Resources, at runtime, use all the references of the project, even if they are not required to run.</p> <p>Long story: I'm writing a .net dll that is used inside Autocad. It's a collection of .net forms. The project of course references some autocad dlls. To help the testing, I decoupled the forms code from the autocad code. So, if I compile the proj as dll, I can use it with autocad; if I compile it as exe the forms load (using mock objects as data). Everything is fine, except when I add an image to my form using the resources:</p> <pre><code>Image img = global::myproj.Properties.Resources.myimage; </code></pre> <p>This leads, at runtime, to this error:</p> <pre><code>System.IO.FileNotFoundException was unhandled Message="Could not load file or assembly 'acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." </code></pre> <p>That dll can't be loaded because we're not in autocad; but it's not required in the "exe" form of the project. In fact, if I remove the image (which obviously has nothing to do with autocad) everything works. It's like the "Resource" part of the project, at runtime, references all the dlls contained in the project... even if they're not used.</p> <p>How can I add an image to my project, without removing the autocad dlls? I'd like the images to be part of the project (that is, inside the dll, not somewhere on disk).</p> <p>BTW: autocad dlls are just an example. I guess I would have the same problem with any other dlls that require the project to be "hosted" in some app.</p> <p>Offending line:</p> <pre><code>pictureBox9.Image = global::MyProj.Properties.Resources.tdb2; </code></pre> <p>Full stack trace:</p> <pre><code>System.IO.FileNotFoundException was unhandled Message="Could not load file or assembly 'acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." Source="mscorlib" FileName="acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null" FusionLog="=== Pre-bind state information ===\r\nLOG: User = user\r\nLOG: DisplayName = acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null\n (Fully-specified)\r\nLOG: Appbase = file:&lt;full path&gt;/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : MyProj, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: No application configuration file found.\r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\config\\machine.config.\r\nLOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).\r\nLOG: Attempting download of new URL file:&lt;full path&gt;/acmgd.DLL.\r\nLOG: Attempting download of new URL file:&lt;full path&gt;/acmgd/acmgd.DLL.\r\nLOG: Attempting download of new URL file:&lt;full path&gt;/acmgd.EXE.\r\nLOG: Attempting download of new URL file:&lt;full path&gt;/acmgd/acmgd.EXE.\r\n" StackTrace: at System.ModuleHandle.ResolveMethod(Int32 methodToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount) at System.ModuleHandle.ResolveMethodHandle(Int32 methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.CustomAttributeData..ctor(Module scope, CustomAttributeRecord caRecord) at System.Reflection.CustomAttributeData.GetCustomAttributes(Module module, Int32 tkTarget) at System.Reflection.CustomAttributeData.GetCustomAttributes(Assembly target) at System.Resources.ResourceManager.GetNeutralResourcesLanguage(Assembly a, UltimateResourceFallbackLocation&amp; fallbackLocation) at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream) at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture) at MyProj.Properties.Resources.get_tdb2() in &lt;full path&gt;Resources.Designer.cs:line 93 at MyProj.MyForm.InitializeComponent() in &lt;full path&gt;\MyForm.Designer.cs:line 524 at MyProj.MyForm..ctor() in &lt;full path&gt;\MyForm.cs:line 28 at MyProj.MyForm.tabPage_Enter(Object sender, EventArgs e) in &lt;full path&gt;\MyForm.cs:line 121 at System.Windows.Forms.Control.OnEnter(EventArgs e) at System.Windows.Forms.TabPage.OnEnter(EventArgs e) at System.Windows.Forms.TabPage.FireEnter(EventArgs e) at System.Windows.Forms.TabControl.OnSelected(TabControlEventArgs e) at System.Windows.Forms.TabControl.WmSelChange() at System.Windows.Forms.TabControl.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message&amp; m) at System.Windows.Forms.Control.WmNotify(Message&amp; m) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message&amp; m) at System.Windows.Forms.Control.DefWndProc(Message&amp; m) at System.Windows.Forms.Control.WmMouseDown(Message&amp; m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.TabControl.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at PidXp.MyForm.Main() in &lt;full path&gt;\MyForm.cs:line 23 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() </code></pre> <p>(some paths/names replaced)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload