简体   繁体   English

Azure Blob和缩略图

[英]Azure blobs and thumbnails

I have two images clear.jpg and thumbclear.jpg, the second one is a thumbnail I create from the first one with the following code: I am not doing any resize yet 我有两张图片clear.jpg和thumbclear.jpg,第二张是我用第一张图片用以下代码创建的缩略图: 我尚未进行任何调整大小

  Bitmap bitmap = new Bitmap(File.InputStream);
  MemoryStream st = new MemoryStream();
  try
  {
     bitmap.Save(st, ImageFormat.Png);
     return st;
  }
  finally
  {
     bitmap.Dispose();
  }

so then I upload both Images to blobs and I get their URIs and copy/paste them to browser. 因此,我将两个图片都上传到Blob,然后获取了它们的URI,然后将其复制/粘贴到浏览器中。 The first one http://127.0.0.1:10000/devstoreaccount1/media/e1a987d1-c731-4e26-9e6c-d7a63b62f661/clear.png is working fine, 第一个http://127.0.0.1:10000/devstoreaccount1/media/e1a987d1-c731-4e26-9e6c-d7a63b62f661/clear.png工作正常,

but the second one http://127.0.0.1:10000/devstoreaccount1/media/b7ba6428-9db4-4282-8991-7a8198e7126f/thumbclear.png gives me the following error: 但是第二个http://127.0.0.1:10000/devstoreaccount1/media/b7ba6428-9db4-4282-8991-7a8198e7126f/thumbclear.png给我以下错误:

The image "http://...thumbclear.png" cannot be displayed, because it contains errors. 无法显示图像“ http://...thumbclear.png”,因为其中包含错误。

So I supose it has something to do with the bitmap to stream. 所以我认为这与要传输的位图有关。 Any help will be appreciated. 任何帮助将不胜感激。

* *Edit The code I use to save the blob * *编辑我用来保存Blob的代码

public static CloudBlob SaveFileToBlob(MemoryStream stream, string blobContainerName, string filename, string extension, string contentType, int fileSize)
        {
            if (stream != null)
            {
                CloudBlobContainer _BlobContainer = SessionHelper.GetBlobContainer(blobContainerName);
                var permissions = new BlobContainerPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                _BlobContainer.SetPermissions(permissions);

                Guid blobid = Guid.NewGuid();
                var blob = _BlobContainer.GetBlobReference(blobid.ToString() + "/" + filename);
                blob.UploadFromStream(stream);

                blob.Metadata["FileName"] = filename;
                blob.Metadata["Extension"] = extension;
                blob.Metadata["FileSize"] = fileSize.ToString();
                blob.SetMetadata();

                blob.Properties.ContentType = contentType;
                blob.SetProperties();

                return blob;
            }
            else
                return null;
        }

The solution was to set the stream position to zero before uploading to blob. 解决方案是在上传到Blob之前将流位置设置为零。

stream.Position = 0;
blob.UploadFromStream(stream);

For the first sample: 对于第一个样本:

  Bitmap bitmap = new Bitmap(File.InputStream); MemoryStream st = new MemoryStream(); try { bitmap.Save(st, ImageFormat.Png); //worked for me Response.ContentType = "image/png"; st.WriteTo(Response.OutputStream); //-- } finally { bitmap.Dispose(); } 

AND, I found today 而且,我今天发现

 Function Index() As FileContentResult Dim Resim = New WebClient().DownloadData("https://dosyalar.blob.core.windows.net/dosya/kartalisveris.gif") Return New FileContentResult(Resim, "image/png") '* With {.FileDownloadName = "Höbölö"} End Function 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM