gpt4 book ai didi

asp.net-mvc - 当将位图流上传到Azure存储时,它存储零/空图像

转载 作者:行者123 更新时间:2023-12-04 20:00:59 30 4
gpt4 key购买 nike

我使用以下代码将 MemoryStream 从位图图像上传到我的 Microsoft Azure 存储帐户:

                         MemoryStream memoryStream = new MemoryStream();

img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

blob.Properties.ContentType = model.File.ContentType;
blob.UploadFromStream(memoryStream);

使用上面的代码会发生什么情况,它将空图像上传到 Azure 存储:(!(我找到了名称,但文件大小为零!)!

       if (model.File != null && model.File.ContentLength > 0)
{
Bitmap original = null;
var name = "newimagefile";
var errorField = string.Empty;

errorField = "File";
name = Path.GetFileNameWithoutExtension(model.File.FileName);
original = Bitmap.FromStream(model.File.InputStream) as Bitmap;
if (original != null)
{
var img = CreateImage(original, model.X, model.Y, model.Width, model.Height);



CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer container = blobClient.GetContainerReference("company"); // must always be lowercase
container.CreateIfNotExists();

container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess =
BlobContainerPublicAccessType.Blob
});


CloudBlockBlob blob = container.GetBlockBlobReference(imgName + ".png");

MemoryStream memoryStream = new MemoryStream();

img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

blob.Properties.ContentType = model.File.ContentType;
blob.UploadFromStream(memoryStream);

}

请帮忙!

最佳答案

我认为您需要在上传之前将位置设置回流的开头

img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
memoryStream.Position = 0;
blob.UploadFromStream(memoryStream);

关于asp.net-mvc - 当将位图流上传到Azure存储时,它存储零/空图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30784058/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com