This topic shows you how to use Image Search SDK for .NET and provides sample code.

Methods

Method Description
Add Adds images.
SearchImageByPic Searches for similar images based on a new image.
SearchImageByName Searches for similar images based on the name of an existing image in the image gallery.
Delete Deletes images.

Before you begin

You can install 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 AlibabaCloud.SDK.ImageSearch20201214, and then click Install.

Add

  • Sample code
    using AlibabaCloud.OpenApiClient.Models;
    using AlibabaCloud.SDK.ImageSearch20201214.Models;
    using AlibabaCloud.SDK.ImageSearch20201214;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    namespace Test
    {
        class Add
        {
            static void Main(string[] args)
            {
                // Console.WriteLine("Hello World!") ;
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXXXXXXXXXXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                FileStream fs = File.OpenRead("D:/111.jpg");
                AddImageAdvanceRequest request = new AddImageAdvanceRequest();
                // Required. The image name, which can be up to 512 characters in length.
                 // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
               // 2. If you add images that have the same ProductId and PicName values for multiple times, the image that is added in the last operation overwrites the images that are added in previous operations.
                request.PicName = "xusong.jpg";
                // Required. The product ID, which can be up to 512 characters in length.
                // A product ID can correspond to multiple image names.
                request.ProductId = "1001";
                // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXXXXXXXXXXXXXXXXXXXXX";
                // The image file. The file size of the image cannot exceed 2 MB. The transmission timeout period cannot exceed 5 seconds. Only JPG and PNG images are supported.
                // For product and generic image searches, the length and the width of the image must range from 200 pixels to 1,024 pixels.
                // The image cannot contain rotation information.
                request.PicContentObject = fs;
                // Optional. The integer attribute. The attribute can be used to filter images when you search for images. If you specify this parameter, the response includes this parameter and its value.
                // For example, you can set different IntAttr values for images from different sites or different users. This narrows down the search scope when you specify the integer attribute to search for images.
                request.IntAttr = 99;
                 // Optional. The string attribute. The value can be up to 128 characters in length. The attribute can be used to filter images when you search for images. If you specify this parameter, the response includes this parameter and its value.
                request.StrAttr = "aliyun.com";
                // Optional. The image category.
                // 1. For product image searches: If you specify a category for an image, the specified category prevails. If you do not specify a category for an image, the system predicts the category, and returns the prediction result in the response.
                // 2. For generic image searches: The parameter value is set to 88888888 regardless of whether a category is set.
                request.CategoryId = 5;
                // Optional. Specifies whether to recognize the subject in the image and search for images based on the recognized subject. The default value is true.
               // 1. If this parameter is set to true, the system recognizes the subject in the image, and searches for images based on the recognized subject. You can obtain the recognition result in the response.
               // 2. If this parameter is set to false, the system does not recognize the subject of the image, and searches for images based on the entire image.
                request.Crop = true;
                // Optional. The subject area in the image. The subject area is in the format of x1,x2,y1,y2. x1 and y1 represent the upper-left corner pixel. x2 and y2 represent the lower-right corner pixel.
               // If you specify the Region parameter, the system searches for images based on this parameter setting regardless of the value of the Crop parameter.
                request.Region = "66,333,97,356";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    AddImageResponse response = client.AddImageAdvance(request, runtimeObject);
                    Console.WriteLine("result:" + Newtonsoft.Json.JsonConvert.SerializeObject(response))
                }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • Sample responses
    result:{"Headers":{"date":"Mon, 11 Jan 2021 06:57:16 GMT","connection":"keep-alive","access-control-allow-origin":"*","access-control-allow-methods":"POST, GET, OPTIONS","access-control-allow-headers":"X-Requested-With, X-Sequence, _aop_secret, _aop_signature","access-control-max-age":"172800","x-acs-request-id":"6D32C931-D584-4927-938A-6053AE9B630D"},"Body":{"Message":"success","RequestId":"6D32C931-D584-4927-938A-6053AE9B630D","Code":0,"PicInfo":{"Region":"112,440,76,387","CategoryId":88888888},"Success":true}}

SearchImageByPic

  • Sample code
    using AlibabaCloud.OpenApiClient.Models;
    using AlibabaCloud.SDK.ImageSearch20201214.Models;
    using AlibabaCloud.SDK.ImageSearch20201214;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    namespace Test
    {
        class SearchImageByPic
        {
            static void Main(string[] args)
            {
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                FileStream fs = File.OpenRead("D:/123.jpg");
                SearchImageByPicAdvanceRequest request = new SearchImageByPicAdvanceRequest();
                // Optional. The product category.
               // 1. For product image searches: If you specify a category for an image, the specified category prevails. If you do not specify a category for an image, the system predicts the category, and returns the prediction result in the response.
               // 2. For generic image searches: The parameter value is set to 88888888 regardless of whether a category is set.
                request.CategoryId = 5;
                // The image file. The file size of the image cannot exceed 2 MB. The transmission timeout period cannot exceed 5 seconds. Only JPG and PNG images are supported.
                // For product and generic image searches, the length and the width of the image must range from 200 pixels to 1,024 pixels.
               // The image cannot contain rotation information.
                request.PicContentObject = fs;
                // Optional. Specifies whether to recognize the subject in the image and search for images based on the recognized subject. The default value is true.
                // 1. If this parameter is set to true, the system recognizes the subject in the image, and searches for images based on the recognized subject. You can obtain the recognition result in the response.
               // 2. If this parameter is set to false, the system does not recognize the subject of the image, and searches for images based on the entire image.
                request.Crop = true;
                // Optional. The filter condition.
               // The int_attr field supports the following operators: >, >=, <, <=, and =. The str_attr field supports the following operators: = and !=. Multiple filter conditions are joined by AND or OR.
              // Examples:
              //1. Filter results based on IntAttr: int_attr>=100
              //2. Filter results based on StrAttr: str_attr! ="value1"
              //3. Filter results based on IntAttr and StrAttr: int_attr=1000 AND str_attr="value1"
                request.Filter = "int_attr=99";
                // Optional. The subject area in the image. The subject area is in the format of x1,x2,y1,y2. x1 and y1 represent the upper-left corner pixel. x2 and y2 represent the lower-right corner pixel.
                // If you specify the Region parameter, the system searches for images based on this parameter setting regardless of the value of the Crop parameter.
                request.Region = "66,333,98,356";
                // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXX";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    SearchImageByPicResponse response = client.SearchImageByPicAdvance(request, runtimeObject);
                    String result = Newtonsoft.Json.JsonConvert.SerializeObject(response);
                    Console.WriteLine("result:" + result);
                    }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • Sample responses
    result:
    {
        "RequestId":"632814A0-F34D-46FB-882D-81D74E7C3290",
        "Success":true,
        "Code":0,
        "Msg":"success",
        "Auctions":[
            {
                "CategoryId":0,
                "ProductId":"test",
                "PicName":"test.jpg",
                "CustomContent":null,
                "Score":1.0
                "SortExprValues":"7.33136443711219;0",
                "IntAttr":null,
                "StrAttr":null
            },
            {......}
            ],
        "Head":{
            "DocsReturn":4,
            "DocsFound":4,
            "SearchTime":155
        },
        "PicInfo":{
            "CategoryId":0,
            "Region":"202,596,155,637",
            "AllCategories":[{"Id":0,"Name":"Tops"},{.....}],
            "MultiRegion":[
                {"Region":"112,440,76,387"}
            ]
         }
    }

SearchImageByName

  • Sample code
    using AlibabaCloud.OpenApiClient.Models;
    using AlibabaCloud.SDK.ImageSearch20201214.Models;
    using AlibabaCloud.SDK.ImageSearch20201214;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    namespace Test
    {
        class SearchImageByName
        {
            static void Main(string[] args)
            {
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                
                SearchImageByNameRequest request = new SearchImageByNameRequest();
                // Optional. The product category.
               // 1. For product image searches: If you specify a category for an image, the specified category prevails. If you do not specify a category for an image, the system predicts the category, and returns the prediction result in the response.
               // 2. For generic image searches: The parameter value is set to 88888888 regardless of whether a category is set.
                request.CategoryId = 5;
                // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXXXXXX";
                // Required. The image name, which can be up to 512 characters in length.
                // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
                request.PicName = "xusong.jpg";
                 // Required. The product ID, which can be up to 512 characters in length.
                // A product ID can correspond to multiple image names.
                request.ProductId = "1001";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    SearchImageByNameResponse response = client.SearchImageByName(request,runtimeObject);
                    String result = Newtonsoft.Json.JsonConvert.SerializeObject(response);
                    Console.WriteLine("result:" + result);
                }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • Sample responses
    result:
    {
        "RequestId":"5B16E5CD-FE8A-4A4E-A90B-02A5C9F8BD81",
        "Success":true,
        "Code":0,
        "Msg":"success",
        "Auctions":[
            {
                "CategoryId":0,
                "ProductId":"test",
                "PicName":"test.jpg",
                "CustomContent":null,
                "Score":1.0
                "SortExprValues":"7.33136443711219;0",
                "IntAttr":null,
                "StrAttr":null
            },
            {....}
        ],
        "Head":{
            "DocsReturn":4,
            "DocsFound":4,
            "SearchTime":50
        },
        "PicInfo":{
            "CategoryId":0,
            "Region":null,
            "AllCategories":[
                {"Id":0,"Name":"Tops"},
                {...}
            ],
           "MultiRegion":[
                {"Region":"112,440,76,387"}
           ]
        }
    }

Delete

  • Sample code
    using AlibabaCloud.OpenApiClient.Models;
    using AlibabaCloud.SDK.ImageSearch20201214.Models;
    using AlibabaCloud.SDK.ImageSearch20201214;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    namespace Test
    {
        class Delete
        {
            static void Main(string[] args)
            {
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                DeleteImageRequest request = new DeleteImageRequest();
               // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXXXXXXX";
                 // Required. The image name, which can be up to 512 characters in length.
                 // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
                request.PicName = "xusong.jpg";
                // Optional. The image name. If this parameter is not specified, the system deletes all the images that correspond to the specified ProductId parameter. If this parameter is specified, the system deletes only the image that is specified by the ProductId and PicName parameters.
                request.ProductId = "1001";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    DeleteImageResponse response = client.DeleteImage(request,runtimeObject);
                    Console.WriteLine("RequestId:" + response.RequestId);
                    Console.WriteLine("Message:" + response.Message);
                    Console.WriteLine("Code:" + response.Code);
                }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • Sample responses
    RequestId:D9BDEAE0-674C-4126-9D76-A79F0BE9E881
    Message:success
    Code:0