Use AI Guardrails SDK for .NET to scan images for risky content, including pornography, terrorism, advertisements, QR codes, undesirable scenes, and logos.
Limitations
Accepts image URLs only. Local files and binary data are not supported.
URLs must be HTTP or HTTPS, with a maximum length of 2,048 characters.
Prerequisites
Before you begin, make sure you have:
Installed the .NET dependencies. See Install .NET dependencies and use the exact .NET version specified — otherwise operation calls will fail.
An AccessKey ID and AccessKey secret for a RAM user, stored as environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET.
Synchronous image moderation (recommended)
ImageSyncScanRequest submits a synchronous moderation request and returns results immediately. Use this when your workflow requires a real-time decision before proceeding.
For parameter details, see Synchronous detection API reference.
| Operation | Description | Supported regions |
|---|---|---|
ImageSyncScanRequest | Moderates images synchronously across multiple scenarios: pornography, terrorism, ad, QR code, undesirable scene, and logo detection. | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
// Read credentials from environment variables.
// ALIBABA_CLOUD_ACCESS_KEY_ID: AccessKey ID of a RAM user.
// ALIBABA_CLOUD_ACCESS_KEY_SECRET: AccessKey secret of a RAM user.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Reuse the client instance to improve performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest request = new ImageSyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// Build the task with a unique data ID and the image URL to moderate.
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "<your-data-id>");
task1.Add("url", "<image-url>");
// scenes: detection scenarios to apply. Specify one or more values.
// bizType: your business type identifier, used for custom moderation rules.
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("bizType", "<your-biz-type>");
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(
System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
"utf-8",
FormatType.JSON);
try
{
ImageSyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("Request failed. Status: {0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}Asynchronous image moderation
Use asynchronous moderation for batch workloads or when real-time results are not required. Submit a moderation task with ImageAsyncScanRequest, then retrieve results either by polling with ImageAsyncScanResultsRequest or by configuring a callback notification.
For parameter details, see Asynchronous detection API reference.
Submit an asynchronous moderation task
ImageAsyncScanRequest queues an image for moderation and returns a task ID. Use the task ID to poll for results or match incoming callback notifications.
| Operation | Description | Supported regions |
|---|---|---|
ImageAsyncScanRequest | Submits asynchronous moderation tasks across multiple scenarios: pornography, terrorism, ad, QR code, undesirable scene, and logo detection. Supports callback notification or polling to retrieve results. | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
// Read credentials from environment variables.
// ALIBABA_CLOUD_ACCESS_KEY_ID: AccessKey ID of a RAM user.
// ALIBABA_CLOUD_ACCESS_KEY_SECRET: AccessKey secret of a RAM user.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Reuse the client instance to improve performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanRequest request = new ImageAsyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// Build the task with a unique data ID and the image URL to moderate.
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "<your-data-id>");
task1.Add("url", "<image-url>");
// scenes: detection scenarios to apply. Specify one or more values.
// bizType: your business type identifier, used for custom moderation rules.
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("bizType", "<your-biz-type>");
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(
System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
"utf-8",
FormatType.JSON);
try
{
ImageAsyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("Request failed. Status: {0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}Poll for moderation results
ImageAsyncScanResultsRequest retrieves the results of one or more asynchronous moderation tasks by task ID. Pass a list of task IDs returned by ImageAsyncScanRequest.
| Operation | Description | Supported regions |
|---|---|---|
ImageAsyncScanResultsRequest | Queries the moderation results of multiple asynchronous tasks in a single call. | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
// Read credentials from environment variables.
// ALIBABA_CLOUD_ACCESS_KEY_ID: AccessKey ID of a RAM user.
// ALIBABA_CLOUD_ACCESS_KEY_SECRET: AccessKey secret of a RAM user.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Reuse the client instance to improve performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanResultsRequest request = new ImageAsyncScanResultsRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// Pass the task IDs returned by ImageAsyncScanRequest.
List<string> taskIdList = new List<string> { "<task-id>" };
request.SetContent(
System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(taskIdList)),
"utf-8",
FormatType.JSON);
try
{
ImageAsyncScanResultsResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("Request failed. Status: {0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}Submit feedback on moderation results
If a moderation result is incorrect, use ImageScanFeedbackRequest to correct it. AI Guardrails adds the image to the similar image blacklist or whitelist based on your feedback label. Subsequent submissions of similar images return results based on that label.
For parameter details, see Detection result feedback.
| Operation | Description | Supported regions |
|---|---|---|
ImageScanFeedbackRequest | Corrects algorithm detection results by submitting a feedback label. Adds the image to the blacklist or whitelist for future similar-image matching. | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
// Read credentials from environment variables.
// ALIBABA_CLOUD_ACCESS_KEY_ID: AccessKey ID of a RAM user.
// ALIBABA_CLOUD_ACCESS_KEY_SECRET: AccessKey secret of a RAM user.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Reuse the client instance to improve performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageScanFeedbackRequest request = new ImageScanFeedbackRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// scenes: the scenarios the feedback applies to.
// suggestion: your expected result. "pass" = normal content, "block" = violation.
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("suggestion", "block");
httpBody.Add("url", "<image-url>");
request.SetContent(
System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
"utf-8",
FormatType.JSON);
try
{
ImageScanFeedbackResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("Request failed. Status: {0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}What's next
Synchronous detection API reference — full parameter list for
ImageSyncScanRequestAsynchronous detection API reference — full parameter list for
ImageAsyncScanRequestandImageAsyncScanResultsRequestDetection result feedback — full parameter list for
ImageScanFeedbackRequest