Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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.
0
mdbarker813
Asked:
mdbarker813
  • 10
  • 5
1 Solution
 
mdbarker813Author Commented:
I need super quick turn around on this. Please help. Thanks.
0
 
rsripracCommented:
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
0
 
mdbarker813Author Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mdbarker813Author Commented:
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.

0
 
PsyCoderCommented:
Hi md,

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

HTH
0
 
mdbarker813Author Commented:
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
0
 
PsyCoderCommented:
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
0
 
PsyCoderCommented:
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
0
 
mdbarker813Author Commented:
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
0
 
mdbarker813Author Commented:
HTH,

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

mdb
0
 
mdbarker813Author Commented:
HTH,

I'm having Issues with this C.

mdb
0
 
PsyCoderCommented:
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
0
 
mdbarker813Author Commented:

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


}

0
 
mdbarker813Author Commented:
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
0
 
PsyCoderCommented:
Hi mdb,

the only problem left in your code is the line

>> objImage.FromHbitmap(hBitmap)

The method FromHbitmap is a static function of the class Image and you must use the returned Image object.

so the line must be:

objImage = Image.FromHbitmap(hBitmap)

and you're done!

P.S.: You don't have to be sorry about a little mistake with my name

With kind regards



0
 
mdbarker813Author Commented:
PsyCoder,

You're Awsome. Thanks

mdb
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now