当上传位图流Azure存储,其存储零/空图像 [英] When upload Bitmap stream to Azure storage, it store zero/empty image

查看:186
本文介绍了当上传位图流Azure存储,其存储零/空图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用低于code从位图图像的MemoryStream上传到我的微软Azure存储账户:

I am using below code to upload a MemoryStream from a bitmap image to my Microsoft azure storage account:

                         MemoryStream memoryStream = new MemoryStream();

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

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

由上述code使用,会发生什么是它上传一个空的图像Azure存储:(!(我发现这个名字,但文件大小为零!)!

What happen by use above code is it uploads an empty image to Azure storage :( !(I found the name but the file size is zero!)!

       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);

}

任何帮助,请!

推荐答案

我想你需要上传之前设置位置回到流的启动

I think you need to set the position back to the start of the stream before uploading

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

这篇关于当上传位图流Azure存储,其存储零/空图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆