private byte[] GetBinaryResource(ResourceType type, ResourceValue name) { // Writing a general function to handle resource loading based on // functions FindResource(), LoadResource() etc. is not really // easy because of the returned handle HGLOBAL is a pointer to // resource depending structures that contain additional resource // type depending information and not the resource information // directly! For example see loading an icon resource under // http://www.codeproject.com/Tips/62005/Extracting-A-Particular-Icon-From-an-ICO-Resource.aspx // But the code below should work for custom resources like PNG, // XML etc. byte[] result = new byte[0]; IntPtr hInfo = ResourcesLocator.FindResource(this.hModule, name.Value, type.Value); if (hInfo != IntPtr.Zero) { int size = ResourcesLocator.SizeofResource(this.hModule, hInfo); IntPtr hResource = ResourcesLocator.LoadResource(this.hModule, hInfo); if (hResource != IntPtr.Zero) { result = new byte[size]; IntPtr hLock = ResourcesLocator.LockResource(hResource); if (hLock != IntPtr.Zero) { Marshal.Copy(hLock, result, 0, size); } } } return(result); }
private MemoryStream GetBitmapResource(ResourceValue name) { // Bitmap resources are saved either as DIB or DDB bitmaps and // therefore it is quite ok to use Win-API function LoadBitmap(). // See also http://support.microsoft.com/kb/67883/de. // TODO: Use GetBinaryResource() instead of LoadBitmap(). // Because the bitmap returned by function LoadBitmap() seems // to be loaded with wrong colors. // REMARK: Using LoadImage() instead produces almost the same result! // And therefore it makes no sense to use this function. MemoryStream result = new MemoryStream(); IntPtr hBitmap = ResourcesLocator.LoadBitmap(this.hModule, name.Value); if (hBitmap != IntPtr.Zero) { Bitmap bitmap = Image.FromHbitmap(hBitmap); if (bitmap != null) { bitmap.Save(result, ImageFormat.Bmp); } ResourcesLocator.DeleteObject(hBitmap); result.Position = 0; } return(result); }
public bool Enumerate() { try { if (this.hModule != IntPtr.Zero) { this.cache.Clear(); if (ResourcesLocator.EnumResourcesByType(this.hModule, new EnumResourceTypesDelegate(EnumResourceTypes), this)) { return(this.cache.Keys.Count > 0); } else { Debug.WriteLine("ResourcesLocator::Enumerate() LastError: " + Marshal.GetLastWin32Error()); return(false); } } else { return(false); } } catch (Exception exception) { Debug.WriteLine("ResourcesLocator::Enumerate() " + exception.ToString()); return(false); } }
public bool Load(string filename) { if (String.IsNullOrEmpty(filename)) { return(false); } try { this.filename = filename; if (this.hModule != IntPtr.Zero) { this.Free(); } this.hModule = ResourcesLocator.LoadLibraryEx(this.filename, IntPtr.Zero, ResourcesLocator.LOAD_LIBRARY_AS_DATAFILE); if (this.hModule != IntPtr.Zero) { return(true); } else { Debug.WriteLine("ResourcesLocator::Load() LastError: " + Marshal.GetLastWin32Error()); return(false); } } catch (Exception exception) { Debug.WriteLine("ResourcesLocator::Load() " + exception.ToString()); return(false); } }
public void Free() { if (this.hModule != IntPtr.Zero) { ResourcesLocator.FreeLibrary(this.hModule); this.hModule = IntPtr.Zero; } }
private static bool EnumResourceTypes(IntPtr hModule, IntPtr type, ResourcesLocator parameter) { try { if (ResourcesLocator.EnumResourceNamesByID(parameter.hModule, type, new EnumResourceNamesDelegate(EnumResourceNames), parameter)) { return(true); } else { Debug.WriteLine("ResourcesLocator::EnumResourceTypes() LastError: " + Marshal.GetLastWin32Error()); return(false); } } catch (Exception exception) { Debug.WriteLine("ResourcesLocator::EnumResourceTypes() " + exception.ToString()); return(false); } }
private static extern bool EnumResourceNamesByID(IntPtr hModule, IntPtr type, EnumResourceNamesDelegate callback, ResourcesLocator parameter);
private static extern bool EnumResourcesByType(IntPtr hModule, EnumResourceTypesDelegate callback, ResourcesLocator parameter);
private static bool EnumResourceNames(IntPtr hModule, IntPtr type, IntPtr value, ResourcesLocator parameter) { try { ResourceType helper = new ResourceType(type); if (!parameter.cache.ContainsKey(helper)) { parameter.cache.Add(helper, new List <ResourceValue>()); } parameter.cache[helper].Add(new ResourceValue(value)); // TODO: Handle language depending resources. See example "Creating a Resource List" in // ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/winui/winui/windowsuserinterface/resources/introductiontoresources/usingresources.htm return(true); } catch (Exception exception) { Debug.WriteLine("ResourcesLocator::EnumResourceNames() " + exception.ToString()); return(false); } }