【发布时间】:2020-07-18 15:13:48
【问题描述】:
我正在使用ImageSharp 库在将图像上传到 Azure 之前重新缩放图像,应用程序在到达UploadBlob 操作时挂起且没有错误,我认为这是导致它的流。上传图像时,从图像流中收集信息,我创建一个空的MemoryStream,使用ImageSharp 调整图像大小,用我新缩放的图像填充MemoryStream,然后尝试将MemoryStream 上传到Azure而且我认为它不喜欢它,因为它挂在那里。
在这种情况下使用 MemoryStream 是正确的还是其他什么?
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);
}
【问题讨论】:
-
你的代码到底挂在哪里?为什么将图像高度设置为0?你为什么要
outStream.CopyTo(carImage)? -
如果您查看我的帖子,您会发现我声明它挂在我方法的
UploadBlob部分。图像高度设置为0特定于ImageSharp,它将图像大小设置为250,然后自动计算高度以保持纵横比。流复制是一项无效的测试,我现在已将其删除。 -
在调试器下运行时没有信息?
-
没什么,浏览器控制台中什么都没有。我什至放入了一个 catch 块,它仍然没有报告任何内容。从浏览器的角度来看,它发布然后等待。
-
@Yanayaya 你用什么
accesskey来创建BlobServiceClient?是帐号密钥吗?如果是,请使用连接字符串创建BlobServiceClient
标签: c# asp.net-core azure-blob-storage imagesharp