Posts

Showing posts with the label REFLECTION

Dynamically Loading an Image from an External Assembly

This one came up recently in the C# newsgroup, so I thought I'd take a quick crack at it: private void button1_Click(object sender, EventArgs e) { Assembly asm = Assembly.LoadFrom(System.Environment.CurrentDirectory + @"\AssemblyResources.dll"); Stream strm = asm.GetManifestResourceStream((string)asm.GetManifestResourceNames()[0]); Bitmap b = (Bitmap)Image.FromStream(strm); pictureBox1.Image = b; } What you are doing is: 1) Assuming the "external" assembly is a managed assembly, and it's name is "AssemblyResources.dll", and it resides next to the executable (for ASP.NET you would have to use Server.MapPath or some combination of Request.PhysicalApplicationPath, or the like). We load the assembly into the AppDomain using the LoadFrom method. 2)We extract the resource into a stream using the GetManifestResourceStream method. 3) To make it easier to know the exact names of resources (which can be tricky) I use the GetManifestResourceNames method, whic...