如何使用c#.net在amzon s3服务器上传之前压缩图像?

时间:2017-08-01 06:45:06

标签: c# image amazon-s3

您好我已经使用web api c#在amzon s3服务器上进行了图像上传api。但是我想在上传图片之前需要这个图片进行压缩,但是我怎么做也不知道。

这是我的api =>

        [HttpPost]
    [Route("FileUpload")]
    public HttpResponseMessage FileUpload()
    {
        try
        {               
            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Files.Count > 0)
            {
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];
                    string fname = System.IO.Path.GetFileNameWithoutExtension(postedFile.FileName.ToString());
                    string extension = Path.GetExtension(postedFile.FileName);
                    Image img = null;
                    string newFileName = "";
                    string path = "";
                   img = Image.FromStream(postedFile.InputStream);
                    string path = ConfigurationManager.AppSettings["ImageUploadPath"].ToString();
                    newFileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpeg";
                    string filePath = Path.Combine(path, newFileName);

            SaveJpg(img, filePath); // here i have call method for the save image in my local system. 

                    var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
                    try
                    {
                        PutObjectRequest putRequest = new PutObjectRequest
                         {
                             BucketName = "abc",
                             InputStream = postedFile.InputStream, // i need this image compress but how can do  
                             Key = path + newFileName
                         };
                        PutObjectResponse response = client.PutObject(putRequest);
                    }
                    catch (AmazonS3Exception amazonS3Exception)
                    {
                        if (amazonS3Exception.ErrorCode != null &&
                            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                            ||
                            amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                        {
                            throw new Exception("Check the provided AWS Credentials.");
                        }
                        else
                        {
                            throw new Exception("Error occurred: " + amazonS3Exception.Message);
                        }
                    }
                    return Request.CreateResponse(HttpStatusCode.OK, Status);
                }
            }            
        }
        catch (Exception ex)
        {
        }            
        return Request.CreateResponse(HttpStatusCode.OK, "Done");
    }

这是我的ImageCompress方法=>

public static void SaveJpg(Image image, string file_name, long compression = 60)
    {
        try
        {
            EncoderParameters encoder_params = new EncoderParameters(1);

            encoder_params.Param[0] = new EncoderParameter(
                System.Drawing.Imaging.Encoder.Quality, compression);

            ImageCodecInfo image_codec_info =
                GetEncoderInfo("image/jpeg");

            image.Save(file_name, image_codec_info, encoder_params);
        }
        catch (Exception ex)
        {

        }
    }     

这是我的api,我需要此图片才能上传图片压缩,然后我想上传此图片,以便任何一个想法怎么做才能让我知道。

0 个答案:

没有答案