469,366 Members | 2,286 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Using the FreeImage Library ....

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
Nov 21 '05 #1
10 11619
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #2
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #3
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.

"Nak" <a@a.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #4
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.

"Nak" <a@a.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #5
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.

"Nak" <a@a.com> wrote in message
news:uS**************@TK2MSFTNGP14.phx.gbl...
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.

"Nak" <a@a.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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



Nov 21 '05 #6
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(pFIFFor mat, pIPrStream, 0)
Call freeImage_Wrapper.FreeImage_CloseMemory(pIPrStream )
Return (pIntHandle)
End If
End Function

sorry!

"Nak" <a@a.com> wrote in message
news:uE*************@TK2MSFTNGP11.phx.gbl...
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.

"Nak" <a@a.com> wrote in message
news:uS**************@TK2MSFTNGP14.phx.gbl...
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.

"Nak" <a@a.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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



Nov 21 '05 #7
Nak
LOL

http://www.members.lycos.co.uk/nickp.../soft-fivb.htm

^ A working example with a reusable wrapper might make this easier!

Nick.

"Nak" <a@a.com> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
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(pFIFFor mat, pIPrStream, 0)
Call freeImage_Wrapper.FreeImage_CloseMemory(pIPrStream )
Return (pIntHandle)
End If
End Function

sorry!

"Nak" <a@a.com> wrote in message
news:uE*************@TK2MSFTNGP11.phx.gbl...
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.

"Nak" <a@a.com> wrote in message
news:uS**************@TK2MSFTNGP14.phx.gbl...
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.

"Nak" <a@a.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
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.

"Christopher Kurtis Koeber" <c_******@myrealbox.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
> 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
>



Nov 21 '05 #8
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.
--------------------

Nov 21 '05 #9
Nak
Cheers, it's nice to be told that! :-D

Nick.

"Gary Chang[MSFT]" <v-******@online.microsoft.com> wrote in message
news:UG**************@cpmsftngxa10.phx.gbl...
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.
--------------------

Nov 21 '05 #10
^_^

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christopher Kurtis Koeber | last post: by
6 posts views Thread by =?Utf-8?B?WW9naSBXYXRjaGVy?= | last post: by
reply views Thread by =?Utf-8?B?U2hyaWthbnQgTW9yZQ==?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.