我正在從 azure blob 獲取影像并轉換為縮略圖。
當我運行以下代碼時,我給出了容器的路徑,例如“https://storageaccountname.blob.core.windows.net/images/”,但它顯示錯誤:-
System.IO.FileNotFoundException: C:\Users\Lenovo\桌面\xxx\sample1.jpg
代碼作業正常,直到 foreach 行,因為我從我的天藍色獲取檔案“sample1.jpg”,但在 Image.FromFile 開始將檔案查找到我的本地桌面之后
public async Task < List < byte[] >> GetImageInThumbnail(string path) {
List < byte[] > result = new List < byte[] > ();
var blobServiceClient = new BlobServiceClient("myconnectionstring");
var container = blobServiceClient.GetBlobContainerClient("images");
List < string > blobNames = new List < string > ();
var blobs = container.GetBlobsAsync(BlobTraits.None, BlobStates.None);
await foreach(var blob in blobs) {
blobNames.Add(blob.Name);
}
foreach(string blobItems in blobNames) {
var image = Image.FromFile(blobItems.Split('/').Last());
var resized = new Bitmap(image, new Size(150, 75));
using MemoryStream imageStream = new MemoryStream();
resized.Save(imageStream, ImageFormat.Jpeg);
byte[] imageContent = new byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Read(imageContent, 0, (int) imageStream.Length);
result.Add(imageContent);
}
return result;
}
uj5u.com熱心網友回復:
你打電話時:
var image = Image.FromFile(blobItems.Split('/').Last());
您試圖僅通過檔案名( .Last() ) Image.FromFile 獲取影像,然后嘗試在當前作業目錄中獲取影像。在啟動時,這是您啟動應用程式的目錄,這可能是可執行檔案所在的目錄(但不能是)。請注意,此作業目錄可能會更改!
你需要在這樣的事情之前下載真實的影像(我省略了你的調整大小)
foreach(string blobItems in blobNames)
{
using MemoryStream imageStream = new MemoryStream();
//key part to get the image from the blob
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(blobItems.Split('/').Last());
await cloudBlockBlob.DownloadToStreamAsync(imageStream );
// now you do wat you want with the data you got ;)
byte[] imageContent = new byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Read(imageContent, 0, (int) imageStream.Length);
result.Add(imageContent);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467146.html
上一篇:Azure持久函式:扇出與Parallel.ForEachAsync
下一篇:在陣列中查找元素出現的位置
