Dll が .NET アセンブリの場合、次のAssembly.GetManifestResourceStream
ようにを呼び出すことができます。
public static Bitmap getBitmapFromAssemblyPath(string assemblyPath, string resourceId) {
Assembly assemb = Assembly.LoadFrom(assemblyPath);
Stream stream = assemb.GetManifestResourceStream(resourceId);
Bitmap bmp = new Bitmap(stream);
return bmp;
}
ネイティブ dll (アセンブリではない) の場合は、代わりに Interop を使用する必要があります。ここに解決策があり、次のように要約できます。
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);
[DllImport("kernel32.dll")]
static extern IntPtr FindResource(IntPtr hModule, int lpID, string lpType);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);
[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeLibrary(IntPtr hModule);
static const int LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
public static Bitmap getImageFromNativeDll(string dllPath, string resourceName, int resourceId) {
Bitmap bmp = null;
IntPtr dllHandle = LoadLibraryEx(dllPath, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
IntPtr resourceHandle = FindResource(dllHandle, resourceId, resourceName);
uint resourceSize = SizeofResource(dllHandle, resourceHandle);
IntPtr resourceUnmanagedBytesPtr = LoadResource(dllHandle, resourceHandle);
byte[] resourceManagedBytes = new byte[resourceSize];
Marshal.Copy(resourceUnmanagedBytesPtr, resourceManagedBytes, 0, (int)resourceSize);
using (MemoryStream m = new MemoryStream(resourceManagedBytes)) {
bmp = (Bitmap)Bitmap.FromStream(m);
}
FreeLibrary(dllHandle);
return bmp;
}
エラー処理は追加されていません。これは本番環境向けのコードではありません。
注: アイコンが必要な場合は、ストリームを受け取る Icon コンストラクターを使用できます。
using (MemoryStream m = new MemoryStream(resourceManagedBytes)) {
bmp = (Icon)new Icon(m);
}
それに応じて戻り値の型を変更する必要があります。