C
Christopher Kurtis Koeber
Dear all,
I would like to convert a system.drawing.image or bitmap to a FreeImage
bitmap. I am using the code below. NOTE: the problem I am having doesn't
seem to be with the FreeImage Library itself bit rather with the Windows API
calls. Am I doing something wrong on that end? This is the code that I am
using below, but the result is a black image and not the image that I put
in. Any ideas? Thank you very much for your time!
Sincerely,
Christopher Koeber
PS: The FreeImage Class is part of the freeimage project. You can visit
http://freeimage.sourceforge.net for more info.
Protected MainImage As New FreeImage
Private Declare Function GetDIBits Lib "gdi32.dll" (ByVal aHDC As Int32,
ByVal hBitmap As IntPtr, ByVal nStartScan As Int32, ByVal nNumScans As
Int32, ByVal lpBits As IntPtr, ByRef lpBI As BITMAPINFO, ByVal wUsage As
Int32) As Int32
Private Declare Function GetObject Lib "gdi32.dll" Alias "GetObjectA" (ByVal
hObject As IntPtr, ByVal nCount As Int32, ByRef lpObject As GDIBITMAP) As
Int32
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As
Int32
Private Const DIB_RGB_COLORS As Integer = 0
Private DevContext As Integer
<StructLayout(LayoutKind.Sequential)> _
Private Structure GDIBITMAP
Public bmType As Int32
Public bmWidth As Int32
Public bmHeight As Int32
Public bmWidthBytes As Int32
Public bmPlanes As Int16
Public bmBitsPixel As Int16
Public bmBits As Int32
End Structure
Public Function ConvertHBITMAPToFreeImage(ByVal WindowHandle As IntPtr,
ByVal ImageToConvert As Bitmap) As Int32
Try
Dim SrcBitmap As GDIBITMAP
Dim Success As Int32
Dim ImgHeight As Integer
Dim ImgBits As IntPtr
Dim ImgInfo As BITMAPINFO
Dim HBITMAP As IntPtr = ImageToConvert.GetHbitmap(Color.White)
GetObject(HBITMAP, Marshal.SizeOf(SrcBitmap), SrcBitmap)
''' On the freeimage sources, I wat told to put Null, or nothing in VB.NET,
for this to work. I tried that, however, with the same result, black image
....
DevContext = GetDC(WindowHandle)
'''
ConvertHBITMAPToFreeImage = MainImage.Allocate(ImageToConvert.Width,
ImageToConvert.Height, SrcBitmap.bmBitsPixel)
ImgHeight = MainImage.GetHeight(ConvertHBITMAPToFreeImage)
ImgBits = MainImage.GetBits(ConvertHBITMAPToFreeImage)
ImgInfo = MainImage.GetInfo(ConvertHBITMAPToFreeImage)
'''' This line always returns zero for me, what gives?
Success = GetDIBits(DevContext, HBITMAP, 0, ImgHeight, ImgBits, ImgInfo,
DIB_RGB_COLORS)
''''
If Success = 0 Then Throw New Exception("The conversion from a regular image
to a FREEIMAGE failed ...!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error with FreeImage Conversion
code!")
End Try
End Function
I would like to convert a system.drawing.image or bitmap to a FreeImage
bitmap. I am using the code below. NOTE: the problem I am having doesn't
seem to be with the FreeImage Library itself bit rather with the Windows API
calls. Am I doing something wrong on that end? This is the code that I am
using below, but the result is a black image and not the image that I put
in. Any ideas? Thank you very much for your time!
Sincerely,
Christopher Koeber
PS: The FreeImage Class is part of the freeimage project. You can visit
http://freeimage.sourceforge.net for more info.
Protected MainImage As New FreeImage
Private Declare Function GetDIBits Lib "gdi32.dll" (ByVal aHDC As Int32,
ByVal hBitmap As IntPtr, ByVal nStartScan As Int32, ByVal nNumScans As
Int32, ByVal lpBits As IntPtr, ByRef lpBI As BITMAPINFO, ByVal wUsage As
Int32) As Int32
Private Declare Function GetObject Lib "gdi32.dll" Alias "GetObjectA" (ByVal
hObject As IntPtr, ByVal nCount As Int32, ByRef lpObject As GDIBITMAP) As
Int32
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As
Int32
Private Const DIB_RGB_COLORS As Integer = 0
Private DevContext As Integer
<StructLayout(LayoutKind.Sequential)> _
Private Structure GDIBITMAP
Public bmType As Int32
Public bmWidth As Int32
Public bmHeight As Int32
Public bmWidthBytes As Int32
Public bmPlanes As Int16
Public bmBitsPixel As Int16
Public bmBits As Int32
End Structure
Public Function ConvertHBITMAPToFreeImage(ByVal WindowHandle As IntPtr,
ByVal ImageToConvert As Bitmap) As Int32
Try
Dim SrcBitmap As GDIBITMAP
Dim Success As Int32
Dim ImgHeight As Integer
Dim ImgBits As IntPtr
Dim ImgInfo As BITMAPINFO
Dim HBITMAP As IntPtr = ImageToConvert.GetHbitmap(Color.White)
GetObject(HBITMAP, Marshal.SizeOf(SrcBitmap), SrcBitmap)
''' On the freeimage sources, I wat told to put Null, or nothing in VB.NET,
for this to work. I tried that, however, with the same result, black image
....
DevContext = GetDC(WindowHandle)
'''
ConvertHBITMAPToFreeImage = MainImage.Allocate(ImageToConvert.Width,
ImageToConvert.Height, SrcBitmap.bmBitsPixel)
ImgHeight = MainImage.GetHeight(ConvertHBITMAPToFreeImage)
ImgBits = MainImage.GetBits(ConvertHBITMAPToFreeImage)
ImgInfo = MainImage.GetInfo(ConvertHBITMAPToFreeImage)
'''' This line always returns zero for me, what gives?
Success = GetDIBits(DevContext, HBITMAP, 0, ImgHeight, ImgBits, ImgInfo,
DIB_RGB_COLORS)
''''
If Success = 0 Then Throw New Exception("The conversion from a regular image
to a FREEIMAGE failed ...!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error with FreeImage Conversion
code!")
End Try
End Function