簡體   English   中英

上傳到Azure Blob時圖像損壞

[英]Image broken when uploaded to Azure blobs

我正在使用以下代碼將圖像上傳到Azure blob存儲。 圖片已上傳到存儲設備,但已損壞。 以下是我的代碼。 Base64字符串正確,我已經確認。

string match_val = "z5FuyuNiJDbbb...................";

System.Drawing.Bitmap img = Custom.ConertBase64ToFile(match_val);

var bytes = Convert.FromBase64String(match_val);

StorageCredentials creden = new StorageCredentials(accountname, accesskey);
CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true);
CloudBlobClient client = acc.CreateCloudBlobClient();

CloudBlobContainer cont = client.GetContainerReference(container);
 await cont.CreateIfNotExistsAsync();

await cont.SetPermissionsAsync(new BlobContainerPermissions
{
    PublicAccess = BlobContainerPublicAccessType.Blob
});

CloudBlockBlob cblob = cont.GetBlockBlobReference(Path.Combine(ProfileSignaturePath, string.Concat(fileName, ".", FileFormat.png)).Replace(@"\","/"));

cblob.Properties.ContentType = "image/png";

using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bytes), true, true))
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
        await cblob.UploadFromStreamAsync(memoryStream);
        blobUrl = cblob.Uri.AbsoluteUri;
    }
}

string resUrl = blobUrl;

這是瀏覽器顯示的

在此處輸入圖片說明

Blob圖片網址為https://mspmatefilestorage.blob.core.windows.net/production/Resources/ProfilePictures/Signatures/signature_16.png

我的圖片上傳代碼中有任何問題嗎?

我可以知道match_val是什么嗎? 它是png文件內容的base64編碼的字符串嗎?

我很快創建了一個控制台項目,並且一切正常。 這是我的示例:

    class Program
    {
        static string storageAccountName = "storagetest789";
        static string storageAccountKey = "G36mc********Zup3h9kzj1w==";
        static void Main(string[] args)
        {
            string match_val  = null;
            using(FileStream fileStream = File.Open(@"D:\User\Pictures\test.png",FileMode.Open))
            using(MemoryStream memoryStream = new MemoryStream()){
                fileStream.CopyTo(memoryStream);
                match_val  = Convert.ToBase64String(memoryStream.ToArray());
            }

            StorageCredentials storageCredentials = new StorageCredentials(storageAccountName,storageAccountKey);
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("pub"); // For convenience, it is a public container, so I do not need to set access permission
            cloudBlobContainer.CreateIfNotExists();
            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("test2.png");

            using(MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(match_val)))
            using(Image image = Image.FromStream(memoryStream,true,true))
            using(CloudBlobStream cloudBlobStream = cloudBlockBlob.OpenWrite()){
                image.Save(cloudBlobStream, System.Drawing.Imaging.ImageFormat.Png);
            }

            Console.WriteLine(cloudBlockBlob.Uri.AbsoluteUri);
            Console.ReadLine();
        }
    }

我沒有base64編碼的字符串。 所以我在代碼中生成了一個。 不同之處在於:

  1. 我剛剛通過cloudBlockBlob.OpenWrite()打開了輸出流
  2. 然后,我使用image.Save方法將圖像內容直接保存到Blob流中。

最后,我得到一個URL: https://storagetest789.blob.core.windows.net/pub/test2.png : https://storagetest789.blob.core.windows.net/pub/test2.png

而且我能夠在瀏覽器中查看圖像: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM