Image Processing (IMG) is an image processing service that is provided by Object Storage Service (OSS). The service is secure, cost-effective, and highly reliable and can be used to process large numbers of images. After you upload source images to OSS, you can call RESTful API operations to process the images on a device that is connected over the Internet anytime, anywhere.

Use IMG parameters to process images

  • Use a single IMG parameter to process an image and save the image to your local computer
    using System;
    using System.IO;
    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    using Aliyun.OSS.Util;
    namespace ImageProcess
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program.ImageProcess();
                Console.ReadKey();
            }
            public static void ImageProcess()
            {
                // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
                var endpoint = "yourEndpoint";
                // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
                var accessKeyId = "yourAccessKeyId";
                var accessKeySecret = "yourAccessKeySecret";
                // Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
                var bucketName = "examplebucket";
                // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
                var objectName = "exampledir/example.jpg";
                // Specify the local full path of the source image. 
                // var localImageFilename = "D:\\localpath\\example.jpg";
                // Specify the local directory to which you want to save the processed image. 
                var downloadDir = "yourDownloadDir";
                // Create an OSSClient instance. 
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                try
                {
                    // If the image does not exist in the specified bucket, you must upload the image to the bucket.    
                    // client.PutObject(bucketName, objectName, localImageFilename);
                    // Resize the image to 100 x 100 pixels. 
                    var process = "image/resize,m_fixed,w_100,h_100";
                    var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    // Specify the name of the processed image. 
                    WriteToFile(downloadDir + "/<LocalFilename>", ossObject.Content);
                }
                catch (OssException ex)
                {
                    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
            private static void WriteToFile(string filePath, Stream stream)
            {
                using (var requestStream = stream)
                {
                    using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                    {
                        IoUtils.WriteTo(stream, fs);
                    }
                }
            }
        }
    }
  • Use different IMG parameters to process an image and separately save the images to your local computer
    using System;
    using System.IO;
    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    using Aliyun.OSS.Util;
    namespace ImageProcess
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program.ImageProcess();
                Console.ReadKey();
            }
            public static void ImageProcess()
            {
                // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
                var endpoint = "yourEndpoint";
                // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
                var accessKeyId = "yourAccessKeyId";
                var accessKeySecret = "yourAccessKeySecret";
                // Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
                var bucketName = "examplebucket";
                // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
                var objectName = "exampledir/example.jpg";
                // Specify the local full path of the source image. 
                // var localImageFilename = "D:\\localpath\\example.jpg";
                // Specify the local directory to which you want to save the processed image. 
                var downloadDir = "yourDownloadDir";
                // Create an OSSClient instance. 
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                try
                {
                    // If the image does not exist in the specified bucket, you must upload the image to the bucket.    
                    // client.PutObject(bucketName, objectName, localImageFilename);
                    // Resize the image to 100 x 100 pixels. 
                    var process = "image/resize,m_fixed,w_100,h_100";
                    var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    // Specify the name of the processed image. 
                    WriteToFile(downloadDir + "/<LocalFilename>", ossObject.Content);
                    // Crop the image to 100 x 100 pixels by setting the coordinate pair to (100, 100). 
                    process = "image/crop,w_100,h_100,x_100,y_100";
                    ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    WriteToFile(downloadDir + "/<LocalFilename>", ossObject.Content);
                    // Rotate the image 90 degrees. 
                    process = "image/rotate,90";
                    ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    WriteToFile(downloadDir + "/<LocalFilename>", ossObject.Content);
                }
                catch (OssException ex)
                {
                    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
            private static void WriteToFile(string filePath, Stream stream)
            {
                using (var requestStream = stream)
                {
                    using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                    {
                        IoUtils.WriteTo(stream, fs);
                    }
                }
            }
        }
    }
  • Use multiple IMG parameters to process an image and save the image to your local computer
    The following code provides an example on how to use multiple IMG parameters to process an image at a time. Separate the parameters with forward slashes (/).
    using System;
    using System.IO;
    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    using Aliyun.OSS.Util;
    namespace ImageProcessCascade
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program.ImageProcessCascade();
                Console.ReadKey();
            }
            public static void ImageProcessCascade()
            {
                // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
                var endpoint = "yourEndpoint";
                // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
                var accessKeyId = "yourAccessKeyId";
                var accessKeySecret = "yourAccessKeySecret";
                // Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
                var bucketName = "examplebucket";
                // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
                var objectName = "exampledir/example.jpg";
                // Specify the local full path of the source image. 
                // var localImageFilename = "D:\\localpath\\example.jpg";
                // Specify the local directory to which you want to save the processed image. 
                var downloadDir = "yourDownloadDir";
                // Create an OSSClient instance. 
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                try
                {
                    // If the image that you want to process does not exist in the specified bucket, you must upload the image to the specified bucket.    
                    // client.PutObject(bucketName, objectName, localImageFilename);
                    // After you resize the image to 100 × 100 pixels, rotate the image 90 degrees. 
                    var process = "image/resize,m_fixed,w_100,h_100/rotate,90";
                    var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    // Specify the name of the processed image. 
                    WriteToFile(downloadDir + "/yourLocalFilename", ossObject.Content);
                    Console.WriteLine("Get Object:{0} with process:{1} succeeded ", objectName, process);
                }
                catch (OssException ex)
                {
                    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed with error info: {0}", ex.Message);
                }
            }
            private static void WriteToFile(string filePath, Stream stream)
            {
                using (var requestStream = stream)
                {
                    using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                    {
                        IoUtils.WriteTo(stream, fs);
                    }
                }
            }
        }
    }

Use an image style to process an image

You can encapsulate multiple IMG parameters within a style and use the style to process an image. For more information, see Image styles. The following code provides an example on how to use an image style to process an image:
using System;
using System.IO;
using Aliyun.OSS;
using Aliyun.OSS.Common;
using Aliyun.OSS.Util;
namespace ImageProcessCustom
{
    class Program
    {
        static void Main(string[] args)
        {
            Program.ImageProcessCustomStyle();
            Console.ReadKey();
        }
        public static void ImageProcessCustomStyle()
        {
            // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
            var endpoint = "yourEndpoint";
            // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
            var accessKeyId = "yourAccessKeyId";
            var accessKeySecret = "yourAccessKeySecret";
            // Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
            var bucketName = "examplebucket";
            // Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
            var objectName = "exampledir/example.jpg";
            // Specify the local full path of the source image. 
            // var localImageFilename = "D:\\localpath\\example.jpg";
            // Specify the local directory to which you want to save the processed image. 
            var downloadDir = "yourDownloadDir";
            // Create an OSSClient instance. 
            var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
            try
            {
                // If the image that you want to process does not exist in the specified bucket, you must upload the image to the specified bucket.    
                // client.PutObject(bucketName, objectName, localImageFilename);
                // Use an image style to process the image. In this example, yourCustomStyleName is set to the name of the image style that you created in the OSS console. 
                var process = "style/yourCustomStyleName";
                var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                // Specify the name of the processed image.             
                WriteToFile(downloadDir + "/yourLocalFilename", ossObject.Content);
                Console.WriteLine("Get Object:{0} with process:{1} succeeded ", objectName, process);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        private static void WriteToFile(string filePath, Stream stream)
        {
            using (var requestStream = stream)
            {
                using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                {
                    IoUtils.WriteTo(stream, fs);
                }
            }
        }
    }
}

Generate a signed URL that includes IMG parameters for an object

URLs of private objects must be signed. IMG parameters cannot be added to a signed URL. If you want to process a private object, add IMG parameters to the signature. The following code provides an example on how to add IMG parameters to the signature:
using Aliyun.OSS;
using Aliyun.OSS.Common;

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
var bucketName = "examplebucket";
// Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
var objectName = "exampledir/exampledir.jpg";
// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Resize the image to 100 x 100 pixels. 
    var process = "image/resize,m_fixed,w_100,h_100";
    var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get)
    {
        Expiration = DateTime.Now.AddHours(1),
        Process = process
    };
    // Generate a signed URL. 
    var uri = client.GeneratePresignedUri(req);
    Console.WriteLine("Generate Presigned Uri:{0} with process:{1} succeeded ", uri, process);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

References

  • For more information about the complete sample code of IMG, visit GitHub.
  • For more information about the supported IMG parameters, see IMG parameters.