Hi there,
I am having great fun with Pointers or Handlers or whatever you want to
call them in VB.NET. WHat I am attempting to do is create a .NET image by
using the FreeImage library. On the site they clear say,
------------------------
How do I convert a FreeImage image to a HBITMAP ?
FIBITMAP *dib = FreeImage_Load( FIF_PNG, "test.png", PNG_DEFAULT);
HBITMAP bitmap = CreateDIBitmap( hDC, FreeImage_GetIn foHeader(dib),
CBM_INIT, FreeImage_GetBi ts(dib), FreeImage_GetIn fo(dib), DIB_RGB_COLORS) ;
------------------------
Now I know that this is C++, but I thought this shouldn't be hard to
convert, surely? So I have written the following function in replacement to
the above
------------------------
Private Function getFreeImage(By Val iFileName As String) As Image
Try
'create a FreeImage Wrapper and get a handle to the desired image
Dim pFIWImage As New freeImage_Wrapp er()
Dim pIntFIHandle As Integer =
pFIWImage.FreeI mage_Load(freeI mage_Wrapper.Fr eeImage_GetFile Type(iFileName,
0), iFileName, 0)
'Create a new DC that we can use with the CreateDIBitmap API
Dim pBmpBitmap As Bitmap = New
Bitmap(Convert. ToInt32(pFIWIma ge.FreeImage_Ge tWidth(pIntFIHa ndle)),
Convert.ToInt32 (pFIWImage.Free Image_GetHeight (pIntFIHandle)) )
Dim pGraGraphics As Graphics = Graphics.FromIm age(pBmpBitmap)
Dim pIPrHDC As IntPtr = pGraGraphics.Ge tHdc
'Convert the DIB to a DDB using the API CreateDIBitmap
Dim pBIHInfoHeader As BITMAPINFOHEADE R =
pFIWImage.FreeI mage_GetInfoHea der(pIntFIHandl e)
Dim pIPrBits As IntPtr = pFIWImage.FreeI mage_GetBits(pI ntFIHandle)
Dim pBIoInfo As BITMAPINFO = pFIWImage.FreeI mage_GetInfo(pI ntFIHandle)
Dim pIntDDBHandle As Integer = CreateDIBitmap( pIPrHDC.ToInt32 ,
pBIHInfoHeader, CBM_INIT, pIPrBits, pBIoInfo, DIB_RGB_COLORS)
'Now we have a handle to a HBitmap we can create a .NET Image
Dim pImgImage As Image = Image.FromHbitm ap(New IntPtr(pIntDDBH andle))
'Release the DC and unload the image
Call pGraGraphics.Re leaseHdc(pIPrHD C)
Call pFIWImage.FreeI mage_Unload(pIn tFIHandle)
'Return out image
Return (pImgImage)
Catch ex As Exception
MessageBox.Show (ex.ToString)
Return (Nothing)
End Try
End Function
------------------------
Now all I am getting out of this is 1 black pixel, no more, no less.
Why is this? I *think* it may have something to do with the way I am
passing the handle of the HBitmap to the FromHBitmap method, according the
MSDN a handle *is* returned by the API. Unfortunately seeing as my handle
is a 32 bit integer, I can't just pass it straight to the method, so I am
creating a new IntPtr object, which I *presume* creates a pointer to my
value, or doesn't it?
I must be so close to achieving what I want, yet this bit is confusing
me, if I display the value of "New IntPtr(pIntDDBH andle)" in a messagebox I
get the same value each time, so it can't be a pointer, can it? How the
hell can I do this? I have even tried using StretchDIBits to attempt to
blit the DIB onto my bitmap but still no luck :-(
Any help would be greatly appreciated, thanks loads in advance.
Nick.
--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."
Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
I am having great fun with Pointers or Handlers or whatever you want to
call them in VB.NET. WHat I am attempting to do is create a .NET image by
using the FreeImage library. On the site they clear say,
------------------------
How do I convert a FreeImage image to a HBITMAP ?
FIBITMAP *dib = FreeImage_Load( FIF_PNG, "test.png", PNG_DEFAULT);
HBITMAP bitmap = CreateDIBitmap( hDC, FreeImage_GetIn foHeader(dib),
CBM_INIT, FreeImage_GetBi ts(dib), FreeImage_GetIn fo(dib), DIB_RGB_COLORS) ;
------------------------
Now I know that this is C++, but I thought this shouldn't be hard to
convert, surely? So I have written the following function in replacement to
the above
------------------------
Private Function getFreeImage(By Val iFileName As String) As Image
Try
'create a FreeImage Wrapper and get a handle to the desired image
Dim pFIWImage As New freeImage_Wrapp er()
Dim pIntFIHandle As Integer =
pFIWImage.FreeI mage_Load(freeI mage_Wrapper.Fr eeImage_GetFile Type(iFileName,
0), iFileName, 0)
'Create a new DC that we can use with the CreateDIBitmap API
Dim pBmpBitmap As Bitmap = New
Bitmap(Convert. ToInt32(pFIWIma ge.FreeImage_Ge tWidth(pIntFIHa ndle)),
Convert.ToInt32 (pFIWImage.Free Image_GetHeight (pIntFIHandle)) )
Dim pGraGraphics As Graphics = Graphics.FromIm age(pBmpBitmap)
Dim pIPrHDC As IntPtr = pGraGraphics.Ge tHdc
'Convert the DIB to a DDB using the API CreateDIBitmap
Dim pBIHInfoHeader As BITMAPINFOHEADE R =
pFIWImage.FreeI mage_GetInfoHea der(pIntFIHandl e)
Dim pIPrBits As IntPtr = pFIWImage.FreeI mage_GetBits(pI ntFIHandle)
Dim pBIoInfo As BITMAPINFO = pFIWImage.FreeI mage_GetInfo(pI ntFIHandle)
Dim pIntDDBHandle As Integer = CreateDIBitmap( pIPrHDC.ToInt32 ,
pBIHInfoHeader, CBM_INIT, pIPrBits, pBIoInfo, DIB_RGB_COLORS)
'Now we have a handle to a HBitmap we can create a .NET Image
Dim pImgImage As Image = Image.FromHbitm ap(New IntPtr(pIntDDBH andle))
'Release the DC and unload the image
Call pGraGraphics.Re leaseHdc(pIPrHD C)
Call pFIWImage.FreeI mage_Unload(pIn tFIHandle)
'Return out image
Return (pImgImage)
Catch ex As Exception
MessageBox.Show (ex.ToString)
Return (Nothing)
End Try
End Function
------------------------
Now all I am getting out of this is 1 black pixel, no more, no less.
Why is this? I *think* it may have something to do with the way I am
passing the handle of the HBitmap to the FromHBitmap method, according the
MSDN a handle *is* returned by the API. Unfortunately seeing as my handle
is a 32 bit integer, I can't just pass it straight to the method, so I am
creating a new IntPtr object, which I *presume* creates a pointer to my
value, or doesn't it?
I must be so close to achieving what I want, yet this bit is confusing
me, if I display the value of "New IntPtr(pIntDDBH andle)" in a messagebox I
get the same value each time, so it can't be a pointer, can it? How the
hell can I do this? I have even tried using StretchDIBits to attempt to
blit the DIB onto my bitmap but still no luck :-(
Any help would be greatly appreciated, thanks loads in advance.
Nick.
--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."
Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Comment