I am fetching the image from azure blob and converting into thumbnails.
When I run the below code I give the path of container like"https://storageaccountname.blob.core.windows.net/images/"
but it show error :-
System.IO.FileNotFoundException: C:\Users\Lenovo\Desktop\xxx\sample1.jpg
Code works fine till the foreach line because I am getting the file "sample1.jpg" from my azure but right after that Image.FromFile starts looking the file into my local desktop
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;
}
CodePudding user response:
When you call:
var image = Image.FromFile(blobItems.Split('/').Last());
You are trying to get an image only by filename (the .Last() ), Image.FromFile then try to get the image in the current working directory. At startup, this is the directory where you start your application from, this may be the directory where the executable resides (but must not be). Beware that this working directory can change!
You need to download the real images before something like this (I ommit your resize for siplicity )
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);
}