ImageGear .NET
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:

  • Your project should already be set to compile to target Debug and x86, and you should have the directory: $YOURLOCALPROJ\bin\x86\Debug\.
  • Throughout these instructions, replace x64 with x86.
  • The 32-bit ImageGear binaries are found in $INSTALLDIR\ImageGear .NET v22\Bin\

Using the desired version of Visual Studio (2010 or later):

  1. Create a new "Console Application" project, using C# or VB.NET and name the project: IG_Tutorial_Console.
  2. If you installed ImageGear .NET 64-bit, using the Configuration Manager, create a new project platform (if you don't have one already) for x64. Make sure your project is set to compile targeting Debug and x64. Make sure you now have  $YOURLOCALPROJ\bin\x64\Debug\, and if is not there, create it.
  3. Add references and required resources into your projects in one of the following ways:
    • Recommended: use our NuGet Packages. For this project, you need the following package:
      Accusoft.ImageGear.Core.nupkg (https://www.nuget.org/packages/Accusoft.ImageGear.Core/)
    • Manually:
      1. Copy all files (and folders) inside $INSTALLDIR\ImageGear .NET v22 64-bit\Bin\ to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
      2. Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
        • ImageGear23.Core.dll
        • ImageGear23.Evaluation.dll
        • ImageGear23.Formats.Common.dll

    Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\  

  4. At this point your project is ready for some code. The following code can be used to load an image, resize it to a 150 x 150 dimension, and convert it to another format specified by the user. In the next step, we will go over some areas of this sample code in more detail.
    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
  5. If you are using a Deployment (Runtime) licensing, add the license initialization code. This is not necessary if you are using an Evaluation or Development (Toolkit) license. Please add the ImGearEvaluationManager.Initialize() call if you are evaluating the product.
    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()
  6. We load the image using:
    C#
    Copy Code
    // Load the image into the page.
    ImGearPage imGearPage = ImGearFileFormats.LoadPage(stream, 0);
    VB.NET
    Copy Code
    ' Load the image into the page.
    Dim imGearPage As ImGearPage = ImGearFileFormats.LoadPage(stream, 0)
  7. We resize the image by defining the width, the height, and the interpolation approach as follows:
    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))
  8. Finally, we save the image based on the savingFormat specified:
    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())

 

 


©2017. Accusoft Corporation. All Rights Reserved.

Send Feedback