Using the FreeImage Library ....

  • Thread starter Christopher Kurtis Koeber
  • Start date
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
 
N

Nak

Hi Chris,

http://www.members.lycos.co.uk/nickpatemanpwp/

Click "My Own Software"
Click "FreeImage In VB.NET" (You will need to scroll down to the VB.NET
Section

You will then be able to download an example of converting FreeImage to
a .NET Bitmap, I'm sure it can be easily adapted to work in the reverse.
But saying that, working with Bitmaps *can* be a bit tricky if your having
to supply all of the data and header information yourself.

Nick.
 
N

Nak

LOL, saying that I think FreeImage to .NET is the easy bit. I've had an
idea though that *might* simplify things. How about possibly just saving
the .NET Image to memory stream and then loading the image into free image
from the byte array in memory? This way you won't have to do any of the
other stuff. I'm just taking a look at how this can be achieved now though
may not have a solution until tomorrow sometimes as my head hurts!

Nick.
 
N

Nak

For the benefit of the group,

Public Function freeImageFromBitmap(ByVal iImage As Bitmap, ByVal
iFormat As Imaging.ImageFormat) As Int32
Dim pFIFFormat As freeImage_Wrapper.FIF =
freeImage_Wrapper.imagingFormatToFIF(iFormat)
If (pFIFFormat = freeImage_Wrapper.FIF.FIF_UNKNOWN) Then
Throw New Exception("Image format not support for conversion
from .NET to FreeImage object.")
Else
Dim pMSmMemory As New System.IO.MemoryStream()
Call iImage.Save(pMSmMemory, iFormat)
Call pMSmMemory.Flush()

Dim pBytBuffer(pMSmMemory.Length - 1) As Byte
pMSmMemory.Position = 0
Call pMSmMemory.Read(pBytBuffer, 0, pMSmMemory.Length)
Call pMSmMemory.Close()

Dim pIPrBuffer As IntPtr =
Marshal.AllocHGlobal(pBytBuffer.Length)
Call Marshal.Copy(pBytBuffer, 0, pIPrBuffer, pBytBuffer.Length)
Dim streamhandle As IntPtr =
freeImage_Wrapper.FreeImage_OpenMemory(pIPrBuffer, pBytBuffer.Length)
Dim pIntHandle As Int32 =
freeImage_Wrapper.FreeImage_LoadFromMemory(FIFIF, streamhandle, 0)

Return (pIntHandle)
End If
End Function

Public Shared Function imagingFormatToFIF(ByVal iFormat As
Imaging.ImageFormat) As FIF
Select Case iFormat.ToString.ToLower
Case "bmp"
Return (FIF.FIF_BMP)
Case "jpg"
Return (FIF.FIF_JPEG)
Case Else
Return (FIF.FIF_UNKNOWN)
End Select
End Function

I've tested displaying the image and it works perfectly. I got stuck
temporarily as I hadn't reset the position of the memory stream back to 0,
but once this line was added it all worked fine.

Nick.
 
N

Nak

Doh!

Public Shared Function fromBitmap(ByVal iBitmap As Bitmap, ByVal iFormat As
Imaging.ImageFormat)
Dim pFIFFormat As freeImage_Wrapper.FIF =
freeImage_Wrapper.imagingFormatToFIF(iFormat)
If (pFIFFormat = freeImage_Wrapper.FIF.FIF_UNKNOWN) Then
Throw New Exception("Image format not support for conversion from
..NET to FreeImage object.")
Else
Dim pMSmMemory As New System.IO.MemoryStream()
Call iBitmap.Save(pMSmMemory, iFormat)
Call pMSmMemory.Flush()
Dim pBytBuffer(pMSmMemory.Length - 1) As Byte
pMSmMemory.Position = 0
Call pMSmMemory.Read(pBytBuffer, 0, pMSmMemory.Length)
Call pMSmMemory.Close()
Dim pIPrBuffer As IntPtr = Marshal.AllocHGlobal(pBytBuffer.Length)
Call Marshal.Copy(pBytBuffer, 0, pIPrBuffer, pBytBuffer.Length)
Dim pIPrStream As IntPtr =
freeImage_Wrapper.FreeImage_OpenMemory(pIPrBuffer, pBytBuffer.Length)
Dim pIntHandle As Int32 =
freeImage_Wrapper.FreeImage_LoadFromMemory(pFIFFormat, pIPrStream, 0)
Call freeImage_Wrapper.FreeImage_CloseMemory(pIPrStream)
Return (pIntHandle)
End If
End Function

sorry!
 
G

Gary Chang[MSFT]

Thanks Nick,

It's really wonderful to share your work with the community members, good
luck!


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top