Solved

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

Posted on 2004-09-02
16
356 Views
Last Modified: 2013-12-03

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
Comment
Question by:mdbarker813
  • 10
  • 5
16 Comments
 

Author Comment

by:mdbarker813
Comment Utility
I need super quick turn around on this. Please help. Thanks.
0
 
LVL 5

Expert Comment

by:rsriprac
Comment Utility
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
 

Author Comment

by:mdbarker813
Comment Utility
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
 

Author Comment

by:mdbarker813
Comment Utility
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
 
LVL 1

Expert Comment

by:PsyCoder
Comment Utility
Hi md,

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

HTH
0
 

Author Comment

by:mdbarker813
Comment Utility
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
 
LVL 1

Expert Comment

by:PsyCoder
Comment Utility
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
 
LVL 1

Expert Comment

by:PsyCoder
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mdbarker813
Comment Utility
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
 

Author Comment

by:mdbarker813
Comment Utility
HTH,

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

mdb
0
 

Author Comment

by:mdbarker813
Comment Utility
HTH,

I'm having Issues with this C.

mdb
0
 
LVL 1

Expert Comment

by:PsyCoder
Comment Utility
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
 

Author Comment

by:mdbarker813
Comment Utility

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
 

Author Comment

by:mdbarker813
Comment Utility
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
 
LVL 1

Accepted Solution

by:
PsyCoder earned 500 total points
Comment Utility
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
 

Author Comment

by:mdbarker813
Comment Utility
PsyCoder,

You're Awsome. Thanks

mdb
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to setup 18 78
mergeTwo  challenge 13 70
Google Chrome color inconsistency 2 38
make top menus bigger font 3 34
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now