In this tutorial, you will create a C/C++ Windows console application and use ImageGear to load an image, resize it, and convert it to a new raster format.
The following tutorial refers specifically to 64-bit installations. For 32-bit installations, throughout these instructions, substitute x64 with x86.
Using Visual Studio (2015 or later):
- Create a new "Win32 Console Application" project with name "IG_Tutorial_Console".
- Create a new solution platform for platform x64.
- Set the solution configuration to either "Debug|x64" or "Release|x64" and apply the following changes:
- Set Configuration Properties\General\Output Directory to the absolute path to the ImageGear binaries directory.
e.g. [INSTALLDIR]\ImageGear for C and C++ vXX\Build\Bin\x64\
- Modify project setting Configuration Properties\C/C++\General\Additional Include Directories to include the absolute path to ImageGear header files directory.
e.g. [INSTALLDIR]\ImageGear for C and C++ vXX\Build\Include
- Modify project setting Configuration Properties\Linker\General\Additional Library Directories to include the absolute path to ImageGear libraries directory.
e.g. [INSTALLDIR]\ImageGear for C and C++ vXX\Build\Lib\x64
- Modify project setting Configuration Properties\Linker\Input\Additional Library Dependencies to link with the ImageGear CORE component import library, igCORE19d.lib.
- At this point, your project is ready for some sample code. We will highlight key snippets in the following steps. But first, open the source file, IG_Tutorial_Console.cpp, which Visual Studio created automatically, and replace with the following:
C |
Copy Code
|
// IG_Tutorial_Console.c : Defines the entry point for the console application.
//
// Copyright Accusoft Corporation, Tampa Florida. All Rights Reserved.
#include "stdafx.h"
#include <string.h>
#include "gear.h"
int _tmain(int argc, _TCHAR* argv[])
{
AT_ERRCOUNT errCount = 0;
HIGEAR image = 0;
AT_LMODE saveFormat = IG_SAVE_UNKNOWN;
AT_CHAR inputPath[260];
AT_CHAR formatCode[15];
LPSTR outputPath = "out";
memset(inputPath, 0, sizeof(inputPath)*(sizeof(inputPath[0])));
memset(formatCode, 0, sizeof(formatCode)*(sizeof(formatCode[0])));
fprintf(stdout, "Enter the complete path to the image to be converted: ");
fscanf_s(stdin, "%259[^\n]%*c", inputPath);
fprintf(stdout, "Enter the format for the new image. Choose from \"BMP\","
"\"GIF\", \"JPG\", \"PNG\" or \"TIFF\": ");
fscanf_s(stdin, "%14[^\n]%*c", formatCode);
// Match the format code entered to an ImageGear save format handled with this sample.
saveFormat = IG_SAVE_UNKNOWN;
if (0 == _stricmp(formatCode, "BMP"))
{
saveFormat = IG_SAVE_BMP_UNCOMP;
outputPath = "out.bmp";
}
else if (0 == _stricmp(formatCode, "GIF"))
{
saveFormat = IG_SAVE_GIF;
outputPath = "out.gif";
}
else if (0 == _stricmp(formatCode, "JPG"))
{
saveFormat = IG_SAVE_JPG;
outputPath = "out.jpg";
}
else if (0 == _stricmp(formatCode, "PNG"))
{
saveFormat = IG_SAVE_PNG;
outputPath = "out.png";
}
else if (0 == _stricmp(formatCode, "TIFF"))
{
saveFormat = IG_SAVE_TIF_DEFLATE;
outputPath = "out.tif";
}
else
{
fprintf(stdout, "Format is not support in this application.");
return 1;
}
// Open the image.
IG_load_file(inputPath, &image);
// Resize the image to the width and height specified.
// Parameter nInterpMethod indicates the type of interpolation employed.
// It may be changed for other values, such as IG_INTERPOLATION_BICUBIC
// or IG_INTERPOLATION_NEAREST_NEIGHBOR.
IG_IP_resize_ex(image, 150, 150, IG_INTERPOLATION_BILINEAR, 0, 0);
// Save the imageto the format selected.
errCount = IG_save_file(image, outputPath, saveFormat);
// Release the imageobject.
IG_image_delete(image);
if (errCount == 0)
{
fprintf(stdout, "Saved %s", outputPath);
return 0;
}
else
{
fprintf(stdout, "Failed to save %s", outputPath);
return 1;
}
}
|
C++ |
Copy Code
|
// IG_Tutorial_Console.cpp : Defines the entry point for the console application.
//
// Copyright Accusoft Corporation, Tampa Florida. All Rights Reserved.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#include "gear.h"
int main()
{
cout << "Enter the complete path to the image to be converted: ";
string inputPath;
getline(cin, inputPath);
cout << "Enter the format for the new image. "
"Choose from \"BMP\", \"GIF\", \"JPG\", \"PNG\" or \"TIFF\": ";
string formatCode;
getline(cin, formatCode);
string outputPath("out");
// Match the format code entered to an ImageGear save format handled with this sample.
enumIGSaveFormats saveFormat = IG_SAVE_UNKNOWN;
if (0 == formatCode.compare("BMP"))
{
saveFormat = IG_SAVE_BMP_UNCOMP;
outputPath = "out.bmp";
}
else if (0 == formatCode.compare("GIF"))
{
saveFormat = IG_SAVE_GIF;
outputPath = "out.gif";
}
else if (0 == formatCode.compare("JPG"))
{
saveFormat = IG_SAVE_JPG;
outputPath = "out.jpg";
}
else if (0 == formatCode.compare("PNG"))
{
saveFormat = IG_SAVE_PNG;
outputPath = "out.png";
}
else if (0 == formatCode.compare("TIFF"))
{
saveFormat = IG_SAVE_TIF_DEFLATE;
outputPath = "out.tif";
}
else
{
cout << "Format is not supported in this application.";
return EXIT_FAILURE;
}
// Open the image.
HIGEAR image = 0;
IG_load_file((LPSTR)inputPath.c_str(), &image);
// Resize the image to the width and height specified.
// Parameter nInterpMethod indicates the type of interpolation employed.
// It may be changed for other values, such as IG_INTERPOLATION_BICUBIC
// or IG_INTERPOLATION_NEAREST_NEIGHBOR.
IG_IP_resize_ex(image, 150, 150, IG_INTERPOLATION_BILINEAR, 0, 0);
// Save the image to the format selected.
AT_ERRCOUNT errCount = IG_save_file(image, (LPSTR)outputPath.c_str(), saveFormat);
// Release the image object.
IG_image_delete(image);
if (errCount == 0)
{
cout << "Saved " << outputPath;
return EXIT_SUCCESS;
}
else
{
cout << "Failed to save " << outputPath;
return EXIT_FAILURE;
}
}
|
- If you are using a Deployment license (also known as "Runtime" license), then add the license initialization code. This is not necessary if you are using an Evaluation license or Development license (also known as a "Toolkit" license).
C |
Copy Code
|
//
// To unlock ImageGear for deployment you must call the
// IG_lic_solution_name_set() and IG_lic_solution_key_set() and possibly
// IG_lic_OEM_license_key_set() functions.
// See Licensing section in ImageGear User Manual for more details.
//
|
C++ |
Copy Code
|
//
// To unlock ImageGear for deployment you must call the
// IG_lic_solution_name_set() and IG_lic_solution_key_set() and possibly
// IG_lic_OEM_license_key_set() functions.
// See Licensing section in ImageGear User Manual for more details.
//
|
- We load the image using:
C |
Copy Code
|
// Open the image.
IG_load_file(inputPath, &image);
|
C++ |
Copy Code
|
// Open the image.
HIGEAR image= 0;
IG_load_file((LPSTR)inputPath.c_str(), &image);
|
- We resize the image to 150x150 pixels by defining the width, the height, and the interpolation approach as follows:
C |
Copy Code
|
// Resize the image to the width and height specified.
// Parameter nInterpMethod indicates the type of interpolation employed.
// It may be changed for other values, such as IG_INTERPOLATION_BICUBIC
// or IG_INTERPOLATION_NEAREST_NEIGHBOR.
IG_IP_resize_ex(image, 150, 150, IG_INTERPOLATION_BILINEAR, 0, 0);
|
C++ |
Copy Code
|
// Resize the image to the width and height specified.
// Parameter nInterpMethod indicates the type of interpolation employed.
// It may be changed for other values, such as IG_INTERPOLATION_BICUBIC
// or IG_INTERPOLATION_NEAREST_NEIGHBOR.
IG_IP_resize_ex(image, 150, 150, IG_INTERPOLATION_BILINEAR, 0, 0);
|
- We save the image based on the saveFormat specified:
The majority of ImageGear functions return either an error code, enumIGErrorCodes, or an error count, AT_ERRCOUNT. For brevity, we ignore ImageGear return values in this sample except for saving. This will allow us to indicate that saving was successful with an appropriate message and exit code.
C |
Copy Code
|
// Save the image to the format selected.
errCount = IG_save_file(image, outputPath, saveFormat);
|
C++ |
Copy Code
|
// Save the image to the format selected.
AT_ERRCOUNT errCount = IG_save_file(image, (LPSTR)savePath.c_str(), saveFormat);
|
- Finally, release any ImageGear resources before exiting:
C |
Copy Code
|
// Release the image object.
IG_image_delete(image);
|
C++ |
Copy Code
|
// Release the image object.
IG_image_delete(image);
|