Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I have created a standard dll win32 file with some images by C++. Now I have to load a image (which is with ID) from this dll. I want to use a API function to load that image and after display it on my PictureBox control of Form. But I dont know to choose any function and use it. How do I do this? Hope you help me. Thanks.

What I have tried:

This is my code: I use
LoadBitmapA
function.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Runtime.InteropServices;

namespace ImageDLLTest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern IntPtr LoadBitmapA(IntPtr hinst, int iD);
        [DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibraryA(string Libb);
        [DllImport("kernel32.dll")]
        private static extern IntPtr FreeLibrary(IntPtr hinst);

        public Form1()
        {
            InitializeComponent();
            IntPtr getLib = LoadLibraryA("xy.dll");
            this.BackgroundImage = (System.Drawing.Image)LoadBitmapA(getLib, 1);
            IntPtr freeLib = FreeLibrary(hinst);

            //MessageBox.Show(getLib.ToString());
        }
    }
}


But not work!
Posted
Updated 8-Feb-17 7:20am
v2
Comments
[no name] 8-Feb-17 11:57am    
" I want to use a API function to load", okay so read the documentation for whatever API you have chosen to use.
Andrewpeter 8-Feb-17 13:19pm    
I use LoadBitmapA() to load image from dll, but it's not to work. I update my code above. Hope you help me!

1 solution

You must build some interface with some functionality.

The needed steps are:

1. fetch all needed information from the C++ image into the C# app
2. build a suitable byte buffer in C#
4. call C++ to fill the byte buffer with the image data
4. construct the bitmap

Then you can use this Bitmap constructor to build your bitmap.

To write an interface in your dll you can learn from my article Calling All Stations.

A hack would be that the dll writes a temporary bitmap file which loads C#.
 
Share this answer
 
Comments
Andrewpeter 8-Feb-17 13:27pm    
I updated my code above. Hope you help me to solve completly this problem! Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900