I've created small project for studying purpose using C#, to read PNG resource in a .dll assembly.
Firstyly, I created a PNGResource.dll project embedded with "LargeIcon.png" and "SmallIcon.png".
Access Modifier set to "Public".
Next, I created .exe project with a function like:
private static System.Drawing.Image ReadBitmapFromResource(string ResName, Assembly ResAssembly) { string ResFileName; if (ResAssembly.FullName.IndexOf(',') > 0) { ResFileName = string.Format("{0}.Properties.Resources.resources", ResAssembly.FullName.Substring(0, ResAssembly.FullName.IndexOf(','))); } else { ResFileName = string.Format("{0}.Properties.Resources.resources", ResAssembly.FullName); } using (ResourceReader ResReader = new ResourceReader(ResAssembly.GetManifestResourceStream(ResFileName))) { string ResType; byte[] ResByte; ResReader.GetResourceData(ResName, out ResType, out ResByte); if (ResType != null && ResByte != null ? ResType.IndexOf("System.Drawing.Bitmap") == 0 : false) { using (MemoryStream ResStream = new MemoryStream(ResByte)) { return (Image)(new BinaryFormatter().Deserialize(ResStream)); } } } return null; }
|
then in Form's OnLoad:
... System.Reflection.Assembly assembly=System.Reflection.Assembly.LoadFrom("PNGResource.dll");
if (assembly != null) { Image LargeIcon = ReadBitmapFromResource("LargeIcon", assembly); Image SmallIcon = ReadBitmapFromResource("SmallIcon", assembly); } ... |
Those images are successfully displayed, but with a slight incorrectly visual: transparent pixels displayed as white pixels. I've ensured those PNGs have transparent pixels with Photoshop.
Something wrong with my code?