从托管代码中的非托管 DLL 加载位图文件

发布于 2025-01-05 05:01:07 字数 2099 浏览 1 评论 0原文

我正在尝试从非托管资源 dll 加载图像,但在将从 dll 检索到的 btye 数组转换为位图图像时无法解决错误。

在 Visual Studio 中查看时,test.dll 文件包含以下结构:
测试.dll
位图
+411
图标
+1002[英语(美国])

,当我双击 ID 411(Bimap 节点)时,我可以在位图编辑器中看到 bmp 文件 当我双击 ID 1002(图标节点)时,我可以在图标编辑器中看到不同的图标。

所以我确信它们是有效的位图和图标,但是当我运行下面的测试时,它无法将字节数组转换为图像,因为它捕获了“参数无效 Image.FromStream(...” 错误的异常。

有谁知道 :

代码如下

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());
        }
    }
}

I'm trying to load a image from a unmanaged resource dll and have not been able to get past a error when converting the btye array retrieved from the dll to a bitmap image.

The test.dll file contains the following structure when viewed in visual studio:
test.dll
Bitmap
+411
Icon
+1002[English (United States]

and when I double click the ID 411 (Bimap node) I can see the bmp file in the bitmap editor
and when I double click the ID 1002 (Icon node) I can see the differnt icons in the icon editor.

So im certain that they are valid bitmap and icons, but when I run the test for below it cannot convert byte array to image as it catches the exception with "parameter is not valid Image.FromStream(..." error.

Does anyone know what it wrong.

The code is below:

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());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城之時有危險 2025-01-12 05:01:07

您得到一个指向 BITMAPINFOHEADER 的指针,但文件头丢失。所以 Image.FromStream() 无法工作。相反,请 Pinvoke LoadBitmap() 并使用 Image.FromHbitmap()。

You are getting a pointer to the BITMAPINFOHEADER, the file header is missing. So Image.FromStream() cannot work. Pinvoke LoadBitmap() instead and use Image.FromHbitmap().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文