This topic describes how to use Alibaba Cloud Image Search SDKs for .NET.

Preparations

  • Before you install and use Alibaba Cloud SDKs, make sure that you have created an Alibaba Cloud account and obtained an AccessKey pair. For more information, see Create an AccessKey pair.
  • You can install the core library of the Alibaba Cloud SDK for .NET by using the following methods:
    • (Recommended) Use dependency managers.

      You can install the core library of Alibaba Cloud SDK for .NET by using the NuGet package manager. In Solution Explorer, right-click your project and select Manage NuGet Packages. On the page that appears, click the Browse tab, enter aliyun-net-sdk-core, and then click Install.

    • Use the .NET command-line interface (CLI).
       dotnet add package aliyun-net-sdk-core
    • Download and install the core library of the Alibaba Cloud SDK for .NET.

      You can run the git clone command or use other methods to download aliyun-net-sdk-core, and add a solution.

  • You can install the Alibaba Cloud Image Search SDK for .NET by using the following methods:
    • (Recommended) Use dependency managers.

      You can install the Alibaba Cloud Image Search SDK for .NET by using the NuGet package manager. In Solution Explorer, right-click your project and select Manage NuGet Packages. On the page that appears, click the Browse tab, enter aliyun-net-sdk-imagesearch, and then click Install.

    • Use the .NET CLI.
       dotnet add package aliyun-net-sdk-imagesearch
    • Download and install the Alibaba Cloud Image Search SDK for .NET.

      You can run the git clone command or use other methods to download aliyun-net-sdk-imagesearch, and add a solution.

Sample code

The following complete sample code is provided:
using System;
using System.IO;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.ImageSearch.Model.V20190325;
namespace Test
{
    class Demo
    {
        static void Main(string[] args)
        {
            DefaultProfile profile = DefaultProfile.GetProfile("<region>", "<your-access-key-id>", "<your-access-key-secret>");
            profile.AddEndpoint("<region>", "<region>", "ImageSearch", "imagesearch.<region>aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
          
            // Add the image.
            AddImageRequest addRequest = new AddImageRequest();
            addRequest.InstanceName = "demo";
            addRequest.ProductId = "test";
            addRequest.PicName = "test";
            byte[] img = System.IO.File.ReadAllBytes("c:/demo.jpg");
            string pic = Convert.ToBase64String(img);
            addRequest.PicContent = pic;
            AddImageResponse addResponse = client.GetAcsResponse(addRequest);
            Console.WriteLine(addResponse.RequestId);
          
          // Search for images.
            SearchImageRequest searchRequest = new SearchImageRequest();
            searchRequest.InstanceName = "demo";
            searchRequest.Type = "searchByName";
            searchRequest.ProductId = "test";
            searchRequest.PicName = "test";
            SearchImageResponse searchResponse = client.GetAcsResponse(searchRequest);
            Console.WriteLine(searchResponse.RequestId);
          
            // Delete the image.
            DeleteImageRequest deleteRequest = new DeleteImageRequest();
            deleteRequest.InstanceName = "demo";
            deleteRequest.ProductId = "test";
            DeleteImageResponse deleteResponse = client.GetAcsResponse(deleteRequest);
            Console.WriteLine(deleteResponse.RequestId);
        }
    }
}