图片处理是OSS提供的海量、安全、低成本、高可靠的图片处理服务。原始图片上传到OSS后,您可以通过简单的RESTful接口,在任何时间、任何地点、任何互联网设备上对图片进行处理。

图片处理支持的参数请参见处理参数

使用图片处理参数处理图片

  • 使用单个图片处理参数处理图片并保存为本地图片
    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()
            {
                var endpoint = "<yourEndpoint>";
                var accessKeyId = "<yourAccessKeyId>";
                var accessKeySecret = "<yourAccessKeySecret>";
                var bucketName = "<yourBucketName>";
                var objectName = "<yourObjectName>";
                // 本地文件名称,上传本地图片文件时使用。
                // var localImageFilename = "<yourLocalImageFilename>";
                // 指定保存处理后的图片的本地文件夹。
                var downloadDir = "<yourDownloadDir>";
                // 创建OssClient实例。
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                try
                {
                    // 若目标图片不在目标Bucket内,需上传图片到目标Bucket。   
                    // client.PutObject(bucketName, objectName, localImageFilename);
                    // 将图片缩放为固定宽高100 px。
                    var process = "image/resize,m_fixed,w_100,h_100";
                    var 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);
                    }
                }
            }
        }
    }
  • 使用不同的图片处理参数处理图片并分别保存为本地图片
    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()
            {
                var endpoint = "<yourEndpoint>";
                var accessKeyId = "<yourAccessKeyId>";
                var accessKeySecret = "<yourAccessKeySecret>";
                var bucketName = "<yourBucketName>";
                var objectName = "<yourObjectName>";
                // 本地文件名称,上传本地图片文件时使用。
                // var localImageFilename = "<yourLocalImageFilename>";
                // 指定保存处理后的图片的本地文件夹。
                var downloadDir = "<yourDownloadDir>";
                // 创建OssClient实例。
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                try
                {
                    // 若目标图片不在目标Bucket内,需上传图片到目标Bucket。   
                    // client.PutObject(bucketName, objectName, localImageFilename);
                    // 将图片缩放为固定宽高100 px。
                    var process = "image/resize,m_fixed,w_100,h_100";
                    var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    // 指定处理后的图片名称。
                    WriteToFile(downloadDir + "/<LocalFilename>", ossObject.Content);
                    // 从坐标(100,100)开始,将图片裁剪为宽高100 px。
                    process = "image/crop,w_100,h_100,x_100,y_100";
                    ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    WriteToFile(downloadDir + "/<LocalFilename>", ossObject.Content);
                    // 将图片旋转90°。
                    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);
                    }
                }
            }
        }
    }
  • 使用多个图片处理参数处理图片并保存为本地图片
    使用多个图片处理参数处理图片时,多个参数之间以正斜线(/)分隔。
    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()
            {
                var endpoint = "<yourEndpoint>";
                var accessKeyId = "<yourAccessKeyId>";
                var accessKeySecret = "<yourAccessKeySecret>";
                var bucketName = "<yourBucketName>";
                var objectName = "<yourObjectName>";
                // 本地文件名称,上传本地图片文件时使用。
                // var localImageFilename = "<yourLocalImageFilename>";
                // 指定保存处理后的图片的本地文件夹。
                var downloadDir = "<yourDownloadDir>";
                // 创建OssClient实例。
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                try
                {
                    // 若目标图片不在目标Bucket内,需上传图片到目标Bucket。   
                    // client.PutObject(bucketName, objectName, localImageFilename);
                    // 将图片缩放为固定宽高100 px后,再旋转90°。
                    var process = "image/resize,m_fixed,w_100,h_100/rotate,90";
                    var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                    // 指定处理后的图片名称。
                    WriteToFile(downloadDir + "/<LocalFilename>", 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);
                    }
                }
            }
        }
    }

使用图片样式处理图片

您可以将多个图片处理参数封装在一个样式中,之后使用样式批量处理图片。详情请参见 图片样式。以下代码展示了使用图片样式处理图片:
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()
        {
            var endpoint = "<yourEndpoint>";
            var accessKeyId = "<yourAccessKeyId>";
            var accessKeySecret = "<yourAccessKeySecret>";
            var bucketName = "<yourBucketName>";
            var objectName = "<yourObjectName>";
            // 本地文件名称,上传本地图片文件时使用。
            // var localImageFilename = "<yourLocalImageFilename>";
            // 指定保存处理后的图片的本地文件夹。
            var downloadDir = "<yourDownloadDir>";
            // 创建OssClient实例。
            var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
            try
            {
                // 若目标图片不在目标Bucket内,需上传图片到目标Bucket。   
                // client.PutObject(bucketName, objectName, localImageFilename);
                // 使用指定图片样式处理图片。
                var process = "style/<yourCustomStyleName>";
                var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                // 指定处理后的图片名称。            
                WriteToFile(downloadDir + "/<LocalFilename>", 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);
                }
            }
        }
    }
}

生成带图片处理参数的文件签名URL

私有文件的访问URL带有签名。OSS不支持在带签名的URL后直接添加图片处理参数。如果您想要对私有文件进行图片处理,需要将图片处理参数加入到签名中,相关的代码示例如下:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var objectName = "<yourObjectName>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 将图片缩放为固定宽高100 px。
    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
    };
    // 生成带有签名的URI。
    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);
}

图片处理工具

您可以通过可视化图片处理工具ImageStyleViewer直观地看到OSS图片处理结果。