簡體   English   中英

將Bitmap對象上載到Azure blob存儲

[英]Uploading a Bitmap object to Azure blob storage

我正在嘗試下載,調整大小,然后將圖像上傳到Azure blob存儲。

我可以下載原始圖像並調整大小如下:

private bool DownloadandResizeImage(string originalLocation, string filename)
    {
        try
        {
            byte[] img;
            var request = (HttpWebRequest)WebRequest.Create(originalLocation);

            using (var response = request.GetResponse())
            using (var reader = new BinaryReader(response.GetResponseStream()))
            {
                img = reader.ReadBytes(200000);
            }

            Image original;

            using (var ms = new MemoryStream(img))
            {
                original = Image.FromStream(ms);
            }

            const int newHeight = 84;
            var newWidth = ScaleWidth(original.Height, 84, original.Width);

            using (var newPic = new Bitmap(newWidth, newHeight))
            using (var gr = Graphics.FromImage(newPic))
            {
                gr.DrawImage(original, 0, 0, newWidth, newHeight);
                // This is where I save the file, I would like to instead
                // upload it to Azure
                newPic.Save(filename, ImageFormat.Jpeg);


            }

            return true;
        }
        catch (Exception e)
        {
            return false;
        }

    }

我知道我可以使用UploadFromFile來上傳保存的文件,但是想知道是否有辦法直接從我的對象中執行它,所以我不必先保存它? 我已嘗試從流上傳,並可以在使用ms函數后執行此操作,但隨后我調整了文件大小

只是為了在整個問題的背景下完成Crowcoder的回答,我認為你需要的是:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_blobcnxn);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);

using (MemoryStream memoryStream = new MemoryStream())
{
    newPic.Save(memoryStream, ImageFormat.Jpeg);
    memoryStream.Seek(0, SeekOrigin.Begin); // otherwise you'll get zero byte files
    CloudBlockBlob blockBlob = jpegContainer.GetBlockBlobReference(filename);
    blockBlob.UploadFromStream(memoryStream);
}

這是一個上傳blob作為Stream的示例。 它使用Azure客戶端SDK:

private async Task WriteBlob(Stream blob, string containerName, string blobPath)
{
    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_blobcnxn);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve a reference to a container.
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    // Create the container if it doesn't already exist.
    await container.CreateIfNotExistsAsync();

    // create a blob in the path of the <container>/email/guid
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobPath);

    await blockBlob.UploadFromStreamAsync(blob);
}

暫無
暫無

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

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