ImageGear .NET v25.2 - Updated
Console Application
Getting Started > Tutorial: Create Your First Project > Console Application
Please refer to the System Requirements.

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 Visual Studio 2015 or later:

  1. Create a new .NET Framework 3.5 (or higher) "Console Application" project, using C# or VB.NET, and name the project: IG_Tutorial_Console.
  2. 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 it 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\Bin\x64 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\:
        • ImageGear.Core.dll
        • ImageGear.Evaluation.dll
        • ImageGear.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. You can use the following code to load an image, resize it to a 150 x 150 dimension, and convert it to another specified format. In the next section, 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 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 to 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 to 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 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 to 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 to 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

Now we'll take a closer look at each section of the code.

  1. First add the ImGearEvaluationManager.Initialize() call if you are evaluating the product. You also need to initialize common formats.
    C#
    Copy Code
    // Initialize evaluation license.
    ImGearEvaluationManager.Initialize();
    
    // Initialize common formats.
    ImGearCommonFormats.Initialize();
    VB.NET
    Copy Code
    'Initialize evaluation license.
    ImGearEvaluationManager.Initialize()
    
    'Initialize common formats.
    ImGearCommonFormats.Initialize()
  2. Next, load the image:
    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)
  3. Now, resize the image by defining the width, the height, and the interpolation approach:
    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 to 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 to other values, e.g. ImGearInterpolations.BICUBIC, ImGearInterpolations.NEAREST_NEIGHBOR.
    ImGearProcessing.Resize(imGearPage, 150, 150, ImGearInterpolationOptions.GetDefault(ImGearInterpolations.BILINEAR))
  4. Finally, 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())

 

Is this page helpful?
Yes No
Thanks for your feedback.