ImageGear for .NET使用教程:为控制台应用程序配置C#或VB.NET项目(上)

ImageGear for .NET使用教程

ImageGear for .NET正式版

在本教程中,您将为控制台应用程序配置C#或VB.NET项目,并使用ImageGear .NET功能。 您还将学习如何加载图像,然后调整大小并将其转换为新格式。

以下教程专门针对64位安装。 对于32位安装:

  • 您的项目应该已经设置为可编译为以Debug和x86为目标,并且您应该具有以下目录:$ YOURLOCALPROJ bin x86 Debug 。
  • 在整个说明中,将x64替换为x86。
  • 在$ INSTALLDIR ImageGear .NET v24 Bin 中可以找到32位ImageGear二进制文件。

使用所需版本的Visual Studio(2010或更高版本):

1、使用C#或VB.NET创建一个新的“控制台应用程序”项目,并将该项目命名为IG_Tutorial_Console

2、如果使用配置管理器安装了ImageGear .NET 64位,则为x64创建一个新的项目平台(如果尚未安装)。确保将项目设置为针对Debug和x64进行编译。确保您现在拥有$ YOURLOCALPROJ bin x64 Debug ,如果不存在,请创建它。

3、通过以下方式之一将引用和所需资源添加到项目中:
推荐:使用我们的NuGet软件包。对于此项目,您需要以下软件包:Accusoft.ImageGear.Core.nupkg

手动:

将$ INSTALLDIR ImageGear .NET v24 64位 Bin 中的所有文件(和文件夹)复制到项目中的本地输出bin目录(即$ YOURLOCALPROJ bin x64 Debug )。

将以下引用从$ YOURLOCALPROJ bin x64 Debug 添加到您的项目:

  • ImageGear24.Core.dll
  • ImageGear24.Evaluation.dll
  • ImageGear24.Formats.Common.dll

您的输出目标目录应设置为$ YOURLOCALPROJ bin x64 Debug

4、至此,您的项目已准备好编写一些代码。以下代码可用于加载图像,将其调整为150 x 150尺寸,并将其转换为用户指定的另一种格式。在下一步中,我们将更详细地介绍此示例代码的某些区域。

C#

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

 Imports SystemImports System.IOImports ImageGear.CoreImports ImageGear.EvaluationImports ImageGear.FormatsImports ImageGear.ProcessingImports ImageGear.DisplayModule 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()TryUsing 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

                                                        

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年3月1日
下一篇 2020年3月1日

相关推荐

发表回复

登录后才能评论