Frequently Asked Questions
How do I draw a FreeImage image to the screen in Windows
?
FIBITMAP *dib = FreeImage_Load(FIF_PNG, "test.png",
PNG_DEFAULT);
// ...
SetStretchBltMode(hDC, COLORONCOLOR);
StretchDIBits(hDC, rcDest.left, rcDest.top,
rcDest.right-rcDest.left, rcDest.bottom-rcDest.top,
0, 0, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib),
FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS,
SRCCOPY);
// ...
FreeImage_Unload(dib);
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);
// ...
FreeImage_Unload(dib);
How do I convert a HBITMAP to a FreeImage image ?
HBITMAP hbmp;
// ...
// the following code assumes that you have a valid HBITMAP
loaded into the memory
FIBITMAP *dib = NULL;
if(hbmp) {
BITMAP bm;
GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm);
dib = FreeImage_Allocate(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
// The GetDIBits function clears the biClrUsed and biClrImportant
BITMAPINFO members (dont't know why)
// So we save these infos below. This is needed for palettized
images only.
int nColors = FreeImage_GetColorsUsed(dib);
HDC dc = GetDC(NULL);
int Success = GetDIBits(dc, hbmp, 0, FreeImage_GetHeight(dib),
FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS);
ReleaseDC(NULL, dc);
// restore BITMAPINFO members
FreeImage_GetInfoHeader(dib)->biClrUsed = nColors;
FreeImage_GetInfoHeader(dib)->biClrImportant = nColors;
}
// ...
// don't forget to call FreeImage_Unload when you no longer
need the dib
FreeImage_Unload(dib);
How to use FreeImage as a static library instead of as a
DLL (Visual C++ 6) ?
Using FreeImage as a static library is not so easy, but it's
not more complicated than using any other library. Here is
a step by step method to do this :
1. Compile the FreeImage library in debug and release modes
and close your projects. You won't need to use the FreeImage
source code anymore.
Note: Do not compile the FreeImage DLL (project named FreeImage)
but the project named FreeImageLib. This should produce
a huge file named FreeImage.lib (in release mode) or FreeImaged.lib
(in debug mode) in the Dist\ directory.
2. Copy FreeImage.lib/FreeImaged.lib into your lib\ directory
or in a directory where the linker can find them (e.g. your
project directory). You can use "Menu->Tools->Options->Directories->Library
files" for this.
3. Create a new project and add your code in it.
Add a call to FreeImage_Initialise() at the beginning of
you main function and a call to FreeImage_DeInitialise()
at the end of this function.
4. Edit the compiler options (Menu -> Project ->
Settings)
1. tab C/C++ : Category "Preprocessor"
* Add FREEIMAGE_LIB to the preprocessor definitions
2. tab C/C++ : Category "Code Generation"
* Use the Multithreaded run-time library (in release mode)
* Use the Debug Multithreaded run-time library (in debug
mode)
5. Edit linker options (Menu -> Project -> Settings)
1. tab Link : Category Input
* Add FreeImage.lib to the list of object/library modules
(release mode)
* Add FreeImaged.lib to the list of object/library modules
(debug mode)
2. tab Link : Category Input
* Add LIBCMT to the Ignore library list (it helps to avoid
a warning)
6. Compile and link your program.
Why can't I save this image as JPEG ?
First, make sure to use FreeImage_SetOutputMessage to see
what's going on inside the JPEG plugin.
Next, the JPEG specification only allows 8-bit greyscale,
24-bit RGB and 32-bit CMYK images to be saved as JPEG. Thus,
you cannot save a 32-bit RGBA image to JPEG, nor a 1- or 4-bit
palettized images. As for 8-bit palettized images, if you
try to save one of them as JPEG, it will be transparently
converted to 24-bit by FreeImage.
|