N
Nak
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_GetInfoHeader(dib),
CBM_INIT, FreeImage_GetBits(dib), FreeImage_GetInfo(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(ByVal iFileName As String) As Image
Try
'create a FreeImage Wrapper and get a handle to the desired image
Dim pFIWImage As New freeImage_Wrapper()
Dim pIntFIHandle As Integer =
pFIWImage.FreeImage_Load(freeImage_Wrapper.FreeImage_GetFileType(iFileName,
0), iFileName, 0)
'Create a new DC that we can use with the CreateDIBitmap API
Dim pBmpBitmap As Bitmap = New
Bitmap(Convert.ToInt32(pFIWImage.FreeImage_GetWidth(pIntFIHandle)),
Convert.ToInt32 (pFIWImage.FreeImage_GetHeight(pIntFIHandle)))
Dim pGraGraphics As Graphics = Graphics.FromImage(pBmpBitmap)
Dim pIPrHDC As IntPtr = pGraGraphics.GetHdc
'Convert the DIB to a DDB using the API CreateDIBitmap
Dim pBIHInfoHeader As BITMAPINFOHEADER =
pFIWImage.FreeImage_GetInfoHeader(pIntFIHandle)
Dim pIPrBits As IntPtr = pFIWImage.FreeImage_GetBits(pIntFIHandle)
Dim pBIoInfo As BITMAPINFO = pFIWImage.FreeImage_GetInfo(pIntFIHandle)
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.FromHbitmap(New IntPtr(pIntDDBHandle))
'Release the DC and unload the image
Call pGraGraphics.ReleaseHdc(pIPrHDC)
Call pFIWImage.FreeImage_Unload(pIntFIHandle)
'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(pIntDDBHandle)" 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_GetInfoHeader(dib),
CBM_INIT, FreeImage_GetBits(dib), FreeImage_GetInfo(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(ByVal iFileName As String) As Image
Try
'create a FreeImage Wrapper and get a handle to the desired image
Dim pFIWImage As New freeImage_Wrapper()
Dim pIntFIHandle As Integer =
pFIWImage.FreeImage_Load(freeImage_Wrapper.FreeImage_GetFileType(iFileName,
0), iFileName, 0)
'Create a new DC that we can use with the CreateDIBitmap API
Dim pBmpBitmap As Bitmap = New
Bitmap(Convert.ToInt32(pFIWImage.FreeImage_GetWidth(pIntFIHandle)),
Convert.ToInt32 (pFIWImage.FreeImage_GetHeight(pIntFIHandle)))
Dim pGraGraphics As Graphics = Graphics.FromImage(pBmpBitmap)
Dim pIPrHDC As IntPtr = pGraGraphics.GetHdc
'Convert the DIB to a DDB using the API CreateDIBitmap
Dim pBIHInfoHeader As BITMAPINFOHEADER =
pFIWImage.FreeImage_GetInfoHeader(pIntFIHandle)
Dim pIPrBits As IntPtr = pFIWImage.FreeImage_GetBits(pIntFIHandle)
Dim pBIoInfo As BITMAPINFO = pFIWImage.FreeImage_GetInfo(pIntFIHandle)
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.FromHbitmap(New IntPtr(pIntDDBHandle))
'Release the DC and unload the image
Call pGraGraphics.ReleaseHdc(pIPrHDC)
Call pFIWImage.FreeImage_Unload(pIntFIHandle)
'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(pIntDDBHandle)" 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
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\