C#将png图像上传到Azure会失去透明度

时间:2017-07-20 13:55:01

标签: c# azure png transparency azure-blob-storage

将png文件上传到Azure Blob存储时,透明度变为黑色。

我尝试过使用两者:

CloudBlockBlob.UploadFromStream(流)

CloudBlockBlob.UploadFromFile(文件)

这是我正在使用的C#MVC控制器代码:

public ActionResult _UploadAdImage()
    {
        var model = new ImageViewModel { status = "failed" };
        try
        {
            string stagingResult = "failed";
            string fileExtension = string.Empty;
            var imageFileName = string.Empty;
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                imageFileName = Guid.NewGuid().ToString();
                fileExtension = System.IO.Path.GetExtension(file.FileName).Replace(".", "").ToLower();

                // Test to make sure the png is still transparent at this point.
                //var image = Image.FromStream(file.InputStream);
                //var fileName = Server.MapPath("~/Files/" + imageFileName + ".png");
                //image.Save(fileName);

                var media = new MediaModel
                {
                    MediaStorageType = Common.MediaStorageType.TemporaryFile,
                    FileName = imageFileName,
                    FileStream = file.InputStream,
                    ContentType = "image/" + fileExtension
                };
                media.FileStream.Seek(0, SeekOrigin.Begin);

                azureComponent.SaveMedia(media);
                stagingResult = "success";

            }
            model.status = stagingResult;
            model.url = "/image/tempfile/" + imageFileName;
            model.mediaId = imageFileName;
        }
        catch (Exception ex)
        {
            model.ErrorMessage = ex.Message;
        }


        return Json(model, JsonRequestBehavior.AllowGet);
    }

以下是将文件保存到Azure的逻辑:

        public void SaveMedia(MediaModel mediaModel)
    {
        var cloudStorageAccount = GetCloudStorageAccount(mediaModel.MediaStorageType);

        var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

        var cloudBlobContainer = cloudBlobClient.GetContainerReference(GetContainerName(mediaModel.MediaStorageType));

        cloudBlobContainer.CreateIfNotExists();

        var blob = cloudBlobContainer.GetBlockBlobReference(mediaModel.FileName.ToLower());

        using (mediaModel.FileStream)
        {
            blob.UploadFromStream(mediaModel.FileStream);
            blob.FetchAttributes();
            blob.Metadata["Submitter"] = "Automated Encoder";
            blob.SetMetadata();

            // Set the properties
            blob.Properties.ContentType = mediaModel.ContentType;
            blob.SetProperties();

        }
    }

非常感谢任何见解。

以下是原始图片和最终图片的链接:

Original Image

Final Image

0 个答案:

没有答案