감사, 당신과 같이, 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 (안 어셈블리) 인 경우, 대신 상호 운용성을 사용해야합니다 . 다음과 같이 솔루션 here이 있고, 요약 될 수있다 :
[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;
}
없음 오류 처리를 첨가하지 않은,이 하지 생산 준비 코드입니다.
주 : 아이콘이 필요한 경우, 당신은 스트림을 수신 아이콘 생성자 사용할 수 있습니다
using (MemoryStream m = new MemoryStream(resourceManagedBytes)) {
bmp = (Icon)new Icon(m);
}
을 그리고 그에 따라 반환 형식을 변경해야합니다.
이미지가 아니라 아이콘을로드하는 것은 완전히 정상입니다. 대신 Icon.FromHandle()을 사용하십시오. 아이콘 객체를 다시 사용할 수 없다는 확신이들 때 DestroyIcon으로 아이콘을 다시 파괴해야합니다. –