User Guide > Getting Started > Tutorial: Create Your First Project > Console Application |
In this tutorial, you will configure a C# or VB.NET project for a console application and use ImageGear .NET capabilities. You will also learn how to load an image, then resize and convert it to a new format.
The following tutorial refers specifically to 64-bit installations; for 32-bit installations:
|
Using the desired version of Visual Studio (2010 or later):
Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\ |
C# |
Copy Code |
---|---|
using System; using System.IO; using ImageGear.Core; using ImageGear.Evaluation; using ImageGear.Formats; using ImageGear.Processing; using ImageGear.Display; namespace IG_Tutorial_Console { class Program { static void Main(string[] args) { // Initialize license for a Deployment(Runtime) license. //***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey //methods must be called to distribute the runtime.*** //ImGearLicense.SetSolutionName("YourSolutionName"); //ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345); //Manually Reported Runtime licenses also require the following method //call to SetOEMLicenseKey. //ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing..."); // Initialize evaluation license. ImGearEvaluationManager.Initialize(); // Initialize common formats. ImGearCommonFormats.Initialize(); Console.WriteLine("Enter the complete path to the image to be converted:"); string fileName = Console.ReadLine(); Console.WriteLine("Enter the format for the new image among BMP|GIF|JPG|PNG|TIFF:"); string newFormat = Console.ReadLine(); try { using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { try { // Load the image into the page. ImGearPage imGearPage = ImGearFileFormats.LoadPage(stream, 0); // Resize the image to the width and height specified. // ImGearInterpolations.BILINEAR is the value indicating the type of interpolation employed. // It may be changed for other values, e.g. ImGearInterpolations.BICUBIC, ImGearInterpolations.NEAREST_NEIGHBOR. ImGearProcessing.Resize(imGearPage, 150, 150, ImGearInterpolationOptions.GetDefault(ImGearInterpolations.BILINEAR)); string outputFileName = fileName.Substring(0,fileName.LastIndexOf('\\') + 1) + "out."; // Indicate the format used when saving the image. // It may be changed for other values, e.g. ImGearSavingFormats.JPG, ImGearSavingFormats.PNG. ImGearSavingFormats savingFormat = ImGearSavingFormats.UNKNOWN; // Identify the format selected. switch (newFormat) { case "BMP": outputFileName += "bmp"; savingFormat = ImGearSavingFormats.BMP_UNCOMP; break; case "GIF": outputFileName += "gif"; savingFormat = ImGearSavingFormats.GIF; break; case "JPG": outputFileName += "jpg"; savingFormat = ImGearSavingFormats.JPG; break; case "PNG": outputFileName += "png"; savingFormat = ImGearSavingFormats.PNG; break; case "TIFF": outputFileName += "tiff"; savingFormat = ImGearSavingFormats.TIF_UNCOMP; break; default: Console.WriteLine("Unsupported format."); break; } if (savingFormat != ImGearSavingFormats.UNKNOWN) { using (FileStream imGearConverted = new FileStream(outputFileName, FileMode.Create)) { // Save the image to the format selected. ImGearFileFormats.SavePage(imGearPage, imGearConverted, 1, ImGearSavingModes.OVERWRITE, savingFormat, new ImGearSaveOptions()); } Console.WriteLine("Conversion successful."); } } catch (ImGearException ex) { Console.WriteLine("Conversion failed: " + ex.Message); } } } catch (Exception) { Console.WriteLine("Incorrect path to an image file."); } Console.ReadLine(); } } } |
VB.NET |
Copy Code |
---|---|
Imports System Imports System.IO Imports ImageGear.Core Imports ImageGear.Evaluation Imports ImageGear.Formats Imports ImageGear.Processing Imports ImageGear.Display Module Module1 Sub Main() ' Initialize license for a Deployment(Runtime) license. '***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey 'methods must be called to distribute the runtime.*** 'ImGearLicense.SetSolutionName("YourSolutionName"); 'ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345); 'Manually Reported Runtime licenses also require the following method 'call to SetOEMLicenseKey. 'ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing..."); 'Initialize evaluation license. ImGearEvaluationManager.Initialize() 'Initialize common formats. ImGearCommonFormats.Initialize() Console.WriteLine("Enter the complete path to the image to be converted:") Dim fileName As String = Console.ReadLine() Console.WriteLine("Enter the format for the new image among BMP|GIF|JPG|PNG|TIFF:") Dim newFormat As String = Console.ReadLine() Try Using stream As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read) Try ' Load the image into the page. Dim imGearPage As ImGearPage = ImGearFileFormats.LoadPage(stream, 0) ' Resize the image to the width and height specified. ' ImGearInterpolations.BILINEAR is the value indicating the type of interpolation employed. ' It may be changed for other values, e.g. ImGearInterpolations.BICUBIC, ImGearInterpolations.NEAREST_NEIGHBOR. ImGearProcessing.Resize(imGearPage, 150, 150, ImGearInterpolationOptions.GetDefault(ImGearInterpolations.BILINEAR)) Dim outputFileName As String = fileName.Substring(0, fileName.LastIndexOf("\") + 1) + "out." ' Indicate the format used when saving the image. ' It may be changed for other values, e.g. ImGearSavingFormats.JPG, ImGearSavingFormats.PNG. Dim savingFormat As ImGearSavingFormats = ImGearSavingFormats.UNKNOWN ' Identify the format selected. Select Case newFormat Case "BMP" outputFileName += "bmp" savingFormat = ImGearSavingFormats.BMP_UNCOMP Case "GIF" outputFileName += "gif" savingFormat = ImGearSavingFormats.GIF Case "JPG" outputFileName += "jpg" savingFormat = ImGearSavingFormats.JPG Case "PNG" outputFileName += "png" savingFormat = ImGearSavingFormats.PNG Case "TIFF" outputFileName += "tiff" savingFormat = ImGearSavingFormats.TIF_UNCOMP End Select If Not savingFormat = ImGearSavingFormats.UNKNOWN Then Using imGearConverted As FileStream = New FileStream(outputFileName, FileMode.Create) ' Save the image to the format selected. ImGearFileFormats.SavePage(imGearPage, imGearConverted, 1, ImGearSavingModes.OVERWRITE, savingFormat, New ImGearSaveOptions()) Console.WriteLine("Conversion successful.") End Using Else Console.WriteLine("Unsupported format.") End If Catch ex As Exception Console.WriteLine("Conversion failed: " + ex.Message) End Try End Using Catch ex As Exception Console.WriteLine("Incorrect path to an image file.") End Try Console.ReadLine() End Sub End Module |
C# |
Copy Code |
---|---|
// Initialize license for a Deployment(Runtime) license. //***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey //methods must be called to distribute the runtime.*** //ImGearLicense.SetSolutionName("YourSolutionName"); //ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345); //Manually Reported Runtime licenses also require the following method //call to SetOEMLicenseKey. //ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing..."); // Initialize evaluation license. ImGearEvaluationManager.Initialize(); // Initialize common formats. ImGearCommonFormats.Initialize(); |
VB.NET |
Copy Code |
---|---|
' Initialize license for a Deployment(Runtime) license. '***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey 'methods must be called to distribute the runtime.*** 'ImGearLicense.SetSolutionName("YourSolutionName"); 'ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345); 'Manually Reported Runtime licenses also require the following method 'call to SetOEMLicenseKey. 'ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing..."); 'Initialize evaluation license. ImGearEvaluationManager.Initialize() 'Initialize common formats. ImGearCommonFormats.Initialize() |
C# |
Copy Code |
---|---|
// Resize the image to the width and height specified.
// ImGearInterpolations.BILINEAR is the value indicating the type of interpolation employed.
// It may be changed for other values, e.g. ImGearInterpolations.BICUBIC, ImGearInterpolations.NEAREST_NEIGHBOR.
ImGearProcessing.Resize(imGearPage, 150, 150, ImGearInterpolationOptions.GetDefault(ImGearInterpolations.BILINEAR)); |
VB.NET |
Copy Code |
---|---|
' Resize the image to the width and height specified.
' ImGearInterpolations.BILINEAR is the value indicating the type of interpolation employed.
' It may be changed for other values, e.g. ImGearInterpolations.BICUBIC, ImGearInterpolations.NEAREST_NEIGHBOR.
ImGearProcessing.Resize(imGearPage, 150, 150, ImGearInterpolationOptions.GetDefault(ImGearInterpolations.BILINEAR)) |
C# |
Copy Code |
---|---|
// Save the image to the format selected. ImGearFileFormats.SavePage(imGearPage, imGearConverted, 1, ImGearSavingModes.OVERWRITE, savingFormat, new ImGearSaveOptions()); |
VB.NET |
Copy Code |
---|---|
' Save the image to the format selected.
ImGearFileFormats.SavePage(imGearPage, imGearConverted, 1, ImGearSavingModes.OVERWRITE, savingFormat, New ImGearSaveOptions()) |