0

I'm using the ImageSharp library to rescale my images before they are uploaded to Azure, the application hangs with no errors when it reaches the UploadBlob action and I think it's the stream causing it. When the image is uploaded the information is gathered from the image stream, I create an empty MemoryStream, resize the image using ImageSharp, stuff the MemoryStream with my newly scaled image and try to upload that MemoryStream to Azure and I don't think it likes it as that is where it hangs.

Is MemoryStream the correct thing to use in this instance or is it something else?

CarController.cs

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Car car)
{
    // Define the cancellation token.
    CancellationTokenSource source = new CancellationTokenSource();
    CancellationToken token = source.Token;

    if (ModelState.IsValid)
    {
        //Access the car record
        _carService.InsertCar(car);

        //Get the newly created ID
        int id = car.Id;

        //Give it a name with some virtual directories within the container         
        string fileName = "car/" + id + "/car-image.jpg";
        string strContainerName = "uploads";

        //I create a memory stream ready for the rescaled image, not sure this is right.
        Stream outStream = new MemoryStream();

        //Access my storage account
        BlobServiceClient blobServiceClient = new BlobServiceClient(accessKey);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(strContainerName);

        //Open the image read stream
        var carImage = car.ImageFile.OpenReadStream();

        //Rescale the image, save as jpeg.
        using (Image image = Image.Load(carImage))
        {
            int width = 250;
            int height = 0;
            image.Mutate(x => x.Resize(width, height));                    
            image.SaveAsJpeg(outStream);
        }

        var blobs = containerClient.UploadBlob(fileName, outStream);
        return RedirectToAction(nameof(Index));
    }            
    return View(car);
}
Yanayaya
  • 2,044
  • 5
  • 30
  • 67
  • Where exactly is your code hanging? Why are you setting the image height to 0? Why are you doing `outStream.CopyTo(carImage)`? – Gaurav Mantri Apr 06 '20 at 15:58
  • If you look at my post you'll see that I state it hangs on the `UploadBlob` portion of my method. The image height is set to `0` is specific to `ImageSharp` which will size the image to 250 and then calculate the height automatically o maintain the aspect ratio. The stream copy was a test that didn't work and I've now removed. – Yanayaya Apr 06 '20 at 17:02
  • No info when running under a debugger? – mxmissile Apr 06 '20 at 17:15
  • Not a thing, nothing in the browser console. I even put in into a catch block and it still doesn't report anything back. From the browsers perspective it posts and then waits. – Yanayaya Apr 06 '20 at 17:25
  • @Yanayaya What is it `accesskey` you use to create `BlobServiceClient`? Is it account key? If so, please use connection string to create `BlobServiceClient` – Jim Xu Apr 08 '20 at 05:12

1 Answers1

1

It's not got anything to do with the ImageSharp library.

You need to reset your outStream position after saving. BlobContainerClient is trying to read from the end of the stream.

James South
  • 10,147
  • 4
  • 59
  • 115