2012-02-13 3 views
1

관리되지 않는 리소스 DLL에서 이미지를로드하려고하는데 dll에서 검색 한 btye 배열을 비트 맵 이미지로 변환 할 때 오류를 지나칠 수 없었습니다.관리되는 코드에서 관리되지 않는 DLL의 비트 맵 파일로드

Test.dll이라는 파일은 Visual Studio에서 볼 때 다음과 같은 구조가 포함
Test.dll에
비트 맵
411
아이콘
1002 [영어 (미국]

때 ID 411 (Bimap 노드)을 두 번 클릭하십시오. 비트 맵 편집기 에서 bmp 파일을 볼 수 있으며 ID 1002 (아이콘 노드)를 두 번 클릭하면 아이콘 편집기에서 differnt 아이콘을 볼 수 있습니다.

그래서 그들은 유효 비트 맵 및 아이콘,하지만 아래에 대한 테스트를 실행할 때 "매개 변수가 유효한 Image.FromStream (..."오류로 예외를 잡으므로 바이트 배열을 이미지로 변환 할 수 없습니다.

누구가 잘못 알고 있는지.

코드

은 다음과 같습니다 : 당신은 BITMAPINFOHEADER에 대한 포인터를 얻고있다

public partial class Form1 : Form 
{ 
    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern IntPtr 
     LoadLibraryEx(string lpFileName, IntPtr hFile, int dwFlags); 

    [DllImport("kernel32.dll")] 
    static extern IntPtr FindResource(IntPtr hModule, int lpName, int 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); 


    const int DATAFILE = 2; 
    const int BITMAP_TYPE = 2; 
    const int ICON_TYPE = 3; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     IntPtr loadLib = LoadLibraryEx("tsjcore.dll", IntPtr.Zero, DATAFILE); 
     IntPtr findRes = FindResource(loadLib, 411, 2); 
     IntPtr loadRes = LoadResource(loadLib, findRes); 
     // Gives the correct size of image as 
     uint size = SizeofResource(loadLib, findRes); 
     byte[] imageArray = new byte[size]; 
     // Loads the imageArray with data when viewed in debug mode. 
     Marshal.Copy(loadRes, imageArray, 0, (int)size); 
     Bitmap bitmap; 
     try 
     { 
      using (MemoryStream memoryStream = new MemoryStream(imageArray)) 
      { 
       bitmap = (Bitmap)Bitmap.FromStream(memoryStream); 
      } 
     } 
     catch (Exception ex) 
     { 
      // displays parameter is not valid Image.FromStream(.... 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
} 

답변

2

, 파일 헤더가 없습니다. 그래서 Image.FromStream() 작동하지 않습니다. 대신 LoadBitmap()을 Pinvoke하고 Image.FromHbitmap()을 사용하십시오.

+0

+1 pinvoke 태그에 트래픽이 많지 않습니다. ..... –

관련 문제