调整大小后图像为空白

时间:2017-07-10 19:17:36

标签: .net azure image-processing azure-storage image-resizing

这是我的代码:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Threading.Tasks;
using Navistar.Inventory.Business.Domain.Interfaces;
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
using System.Drawing.Drawing2D;

//get the storage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse([ConnectionString]);

//instantiate the client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

//set the container
CloudBlobContainer container = blobClient.GetContainerReference([ImagesContainerName]);

var blobUrl = Guid.NewGuid().ToString();

CloudBlockBlob blockBlob = container.GetBlockBlobReference([blobUrl]);

using (var client = new WebClient())
{
    using (var stream = await client.OpenReadTaskAsync([feedUrl]))
    {
        if (stream != null)
        {
            //resize large image
            Image img = Image.FromStream(stream);
            img = Resize(img);

            //save to stream with content type
            if (ImageFormat.Jpeg.Equals(img.RawFormat))
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                //imageBytes = ms2.ToArray();
                blockBlob.Properties.ContentType = "image/jpeg";
            }
            else if (ImageFormat.Png.Equals(img.RawFormat))
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                //imageBytes = ms2.ToArray();
                blockBlob.Properties.ContentType = "image/png";
            }
            else if (ImageFormat.Bmp.Equals(img.RawFormat))
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                //imageBytes = ms2.ToArray();
                blockBlob.Properties.ContentType = "image/bmp";
            }
            else if (ImageFormat.MemoryBmp.Equals(img.RawFormat))
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                //imageBytes = ms2.ToArray();
                blockBlob.Properties.ContentType = "image/bmp";
            }

            //upload
            await blockBlob.UploadFromStreamAsync(stream);
        }
    }
    client.Dispose();
}

public Image Resize(Image image)
{
    var destRect = new Rectangle(0, 0, int.Parse(_config.LargeImageWidth), int.Parse(_config.LargeImageHeight));
    var destImage = new Bitmap(int.Parse(_config.LargeImageWidth), int.Parse(_config.LargeImageHeight));

    destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    using (var graphics = Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighSpeed;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
        }
    }

    return destImage;
}

如您所见,我将图像上传到Azure存储。如果我上传图像而不调整图像大小,则在通过Azure门户导航时会显示图像。如果我在上传之前调整图像大小,则图像为空白。

来自feedUrl(Stream)的图像采用Jpeg格式。重新调整大小后,它将采用MemoryBmp格式。

1 个答案:

答案 0 :(得分:1)

根据您的描述,上传图片大小为0字节的原因是流具有位置值,当您将蒸汽上传到blob存储时,您不必将位置值设置为0。

此外,由于客户端流已经具有该值,但您仍然将已调整大小的图像流保存到客户端流,这意味着您将两个图像存储到一个图像中。

我建议你可以创建一个存储已调整大小的图像数据的新内存流。

更多细节,您可以参考下面的代码示例:

主要方法:

 //get the storage account from the connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

        //instantiate the client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        //set the container
        CloudBlobContainer container = blobClient.GetContainerReference("brando");

        var blobUrl = Guid.NewGuid().ToString();

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("4.PNG");

        using (var client = new WebClient())
        {
            using (var stream1 = client.OpenRead("url"))
{
               //used to store the resized image stream
               MemoryStream m2 = new MemoryStream();
                if (stream1 != null)
                {
                    //resize large image
                    Image img = Image.FromStream(stream1);
                    img = Resize(img);

                    //save to stream with content type
                    if (ImageFormat.Jpeg.Equals(img.RawFormat))
                    {
                        img.Save(m2, System.Drawing.Imaging.ImageFormat.Jpeg);
                        //imageBytes = ms2.ToArray();
                        blockBlob.Properties.ContentType = "image/jpeg";
                    }
                    else if (ImageFormat.Png.Equals(img.RawFormat))
                    {
                        img.Save(m2, System.Drawing.Imaging.ImageFormat.Png);
                        //imageBytes = ms2.ToArray();
                        blockBlob.Properties.ContentType = "image/png";
                    }
                    else if (ImageFormat.Bmp.Equals(img.RawFormat))
                    {
                        img.Save(m2, System.Drawing.Imaging.ImageFormat.Bmp);
                        //imageBytes = ms2.ToArray();
                        blockBlob.Properties.ContentType = "image/bmp";
                    }
                    else if (ImageFormat.MemoryBmp.Equals(img.RawFormat))
                    {
                        img.Save(m2, System.Drawing.Imaging.ImageFormat.Bmp);
                        //imageBytes = ms2.ToArray();
                        blockBlob.Properties.ContentType = "image/bmp";
                    }
                    m2.Position = 0;
                    //upload
                    blockBlob.UploadFromStream(m2);
                }
            }
            client.Dispose();

调整方法:

 public static Image Resize(Image image)
        {
            var destRect = new Rectangle(0, 0, int.Parse("1024"), int.Parse("1024"));
            var destImage = new Bitmap(int.Parse("1024"), int.Parse("1024"));

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighSpeed;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return destImage;
        }

结果:

enter image description here