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 10 11903
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
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
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
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
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
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
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 >
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.
--------------------
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. -------------------- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Surendra |
last post by:
Hi to all,
Can any one of u solve my problem.
How can i show a message box on client side using class library of .net it
may be vb.net or c#.net.
If any1 has any idea about it.
Please let me...
|
by: Christopher Kurtis Koeber |
last post by:
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...
|
by: Eniac |
last post by:
Hello,
I've started using Enterprise Library 2.0 recently and I've encountered
a problem that seems to be ... well... undocumented :)
Basically, when I set a Trace Listener (formatted event...
|
by: Burki |
last post by:
Hi
I want to know how to configure Access database using Enterprise
Library Data block. I want to be able to spacify local path to mdb file
in configuration file. I do not want users to creat...
|
by: =?Utf-8?B?WW9naSBXYXRjaGVy?= |
last post by:
Hello,
I am using Visual Studio-2003. I created a project to build my library.
Since I am using third party libraries as well, I have specified those
additional library dependencies in project...
|
by: zeenets |
last post by:
I am using Enterprise library v3.1 for developing a windows application. its working well.
but there is a security issue, when i deploy this application on client machine the...
|
by: =?Utf-8?B?U2hyaWthbnQgTW9yZQ==?= |
last post by:
Using enterprise library's 3.1 Logging Application Block , how can i get the
custom source name in the windows event log.When ever i am logging an error
in windows event log , it gives the source...
|
by: srizzler |
last post by:
Hi All:
I am trying to implement Exception Handling using Enterprise Library 3.1's Exception Handling Application Block as well as Logging Blocks.
I have a windows application developed in...
|
by: tvnaidu |
last post by:
I created an executable using static library instead shared lib, I am running multipe instances of this executable, does it takes more memory with static library compare to shared library?.
Does...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |