Link to home
Start Free TrialLog in
Avatar of mdbarker813
mdbarker813Flag for United States of America

asked on

I need to retrieve a bitmap from a C DLL from within vb.net


I need a way to retrieve a bitmap from a C DLL from within vb.net.

I need both side of the problem explained. I need the most effective way to get it into a format to move from C to VB.
And once in vb.net I need to put it into a stream for immediate loading into a DirectX texture. I need to fine the most
speed efficient way ASAP.
Avatar of mdbarker813
mdbarker813
Flag of United States of America image

ASKER

I need super quick turn around on this. Please help. Thanks.
Avatar of rsriprac
rsriprac

We need more details of what you are trying exactly and what the C DLL puts out and such.  It will help us help you better.

-Ram
As far as what the C DLL puts out I'm not sure I'm looking for ideas.

I'm adding a function to legacy code on the C DLL side. The C DLL gets a YUV frame from a video feed and converts
it to a RGB Bitmap. I need to be able to return that bitmap from a C call that vb.net can understand.
The Call will be GetFrame() as ? or something to that effect. On the vb.net side it must be able to be stuffed into a
stream.

I need help here. I'm under a time crunch.
Ok, Change of plan here.

All I'm looking for is a way to get a Bitmap from a C/C++ DLL to vb.net and into a stream. I would prefer a pointer to memory but anything is better then what I have now(which is nothing but a headache) .

Writing to disk is not an option.

Hi md,

what about using HBITMAP as the interface between the two languages?

HTH
HTH,

I've been thinking about that however, my lack of C knowledge has hindered my efforts.

Do you know of any sample code that I could look at?

mdb
Hi mdb,

here is some sample code:

From the C Dll return the HBITMAP as follows

extern "C" void PASCAL EXPORT ReturnHBITMAP(HBITMAP* phBitmap)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());
   CCDllApp* pApp = (CCDllApp*) AfxGetApp();
   
   *phBitmap = LoadBitmap( pApp->m_hInstance, MAKEINTRESOURCE( IDB_BITMAP1 ) );

   if (!*phBitmap)
      AfxMessageBox( _T("Couldn't load bitmap in DLL") );
}

The declaration in vb.net is

Private Declare Sub ReturnHBITMAP Lib "CDll.dll" (ByRef hBitmap As IntPtr)


The code for getting the HBITMAP into a System.Drawing.Image object is

      Dim hBitmap As IntPtr
      Dim theImage As System.Drawing.Image

      hBitmap = IntPtr.Zero

      Try
         ReturnHBITMAP(hBitmap)

         If hBitmap.Equals(IntPtr.Zero) Then
            MessageBox.Show("hBitmap is 0")
            Exit Sub
         End If

         theImage = Bitmap.FromHbitmap(hBitmap)

         Me.PictureBox1.Image = theImage

      Catch ex As Exception

         MessageBox.Show(ex.Message)

      End Try

At this time I can't tell you how to convert an System.Drawing.Image to a Stream, but I also don't know what exactly you want to do with it. Maybe I can look at the evening again.

With kind regards
Hi again,

maybe you need something like this for converting the image to a stream object.

      Dim memoryStream As New MemoryStream

This saves the image information to the MemoryStream

      theImage.Save(memoryStream, ImageFormat.Jpeg)

This gets the bytes from the MemoryStream into a byte Array

      Dim rgBytes As Byte()
      ReDim rgBytes(memoryStream.Length)

      memoryStream.Position = 0
      memoryStream.Read(rgBytes, 0, Convert.ToInt32(memoryStream.Length))


HTH
Morning HTH,

That looks promising, let me code this up and see if I can get it to work.

Is there anyway to get it the bitmap straight into a System.IO.Stream vice a
MemoryStream?

Thanks
mdb
HTH,

What version of C/C++ is the code in? VC 6?

mdb
HTH,

I'm having Issues with this C.

mdb
Hi mdb,

so i programmed the sample in short in a visual studio environment. For speeding up the coding i used a regular MFC project, but it should only show the technique for getting a HBITMAP from a C Dll. If you need code for the C Dll post what you need and I can try to write a little function for you.

I can think of a lot of issues you can have with the c Code in the sample because it loads a bitmap from the resources of the Dll. To get the needed result the MFC exported functions need a call to AfxGetStaticModulState() because you have to do this if you call into the dynamic MFC dll. I used AfxGetApp() application pointer for faster getting to the module instance pointer. Another point is that I load a bitmap from the resources of the Dll. I don't think that you will load the bitmap from your resources so you will have to get to your HBITMAP somehow else.

As i said this is only a sample for getting the HBITMAP out of a c dll. How you get to the HBITMAP depends on you, but as I said you can post your goal here and I can try to write a little function for you.

P.S.: my nickname is PsyCoder and HTH means "Hope this helps"

With kind regards
   PsyCoder

Here is the VB.net Code I've got going

    Declare Ansi Function DLLVersion Lib "VideoFeed.dll" () As Integer
    Declare Ansi Sub GetFrame Lib "VideoFeed.dll" (ByRef hBitmap As IntPtr)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim hBitmap As IntPtr
        Dim objImage As Image

        Me.Text = CStr(DLLVersion)

        hBitmap = IntPtr.Zero

        GetFrame(hBitmap)

        If hBitmap.Equals(IntPtr.Zero) Then
            MsgBox("No Image Returned")
            Exit Sub
        End If

        objImage.FromHbitmap(hBitmap)

        picDLLReturn.Image = objImage

    End Sub

Here is the CPP(I'm trying to stay away from MFC)

// VideoFeed.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

//char buffer[10];

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
    return TRUE;
}

int _stdcall DLLVersion()
{
return 1;
}

void _stdcall GetFrame(HBITMAP* phBitmap)
{

*phBitmap = (HBITMAP)LoadImage(NULL,"C:\\Walk1.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);


}

PsyCoder,

This returns a handle but the is no image. Can you help me? I think I have a pointer to a pointer going on here.

It has been a rough week sorry about the name.

mdb
ASKER CERTIFIED SOLUTION
Avatar of PsyCoder
PsyCoder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PsyCoder,

You're Awsome. Thanks

mdb