2008年6月2日 星期一

Change Window Form Application Background from Embeded Resource

This is a note about changing the background image of .Net Win Form Application.
Assumption:

  1. the image resources is embedded.
  2. you have no idea about the full name of the background image. (tricky ...)
The following name space is required:
  • using System.Reflection;
  • using System.IO;
  • using System.Drawing;
Here is the code to load the resource from assembly.
  1. Assembly a = Assembly.GetExecutingAssembly();
  2. string[] resNames = a.GetManifestResourceNames();
  3. foreach (string s in resNames)
  4. {
  5. if (s.EndsWith(".png"))
  6. {
  7. Stream imgStream = a.GetManifestResourceStream(s);
  8. this.BackgroundImage = Bitmap.FromStream(imgStream) as Bitmap;
  9. break;
  10. }
  11. }
With using this code the resource can be load dynamically.

Reference
  1. http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75

沒有留言: