【问题标题】:Image is Blank After Resizing调整大小后图像为空白
【发布时间】:2017-12-14 15:47:44
【问题描述】:

这是我的代码:

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(流)的图像是 Jpeg 格式。重新调整大小后,它是 MemoryBmp 格式。

【问题讨论】:

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


    【解决方案1】:

    根据您的描述,上传图片大小为0字节的原因是stream有位置值,而您在将steam上传到blob存储时没有将位置值设置为0。

    另外,由于 clientstream 已经有了 value,但是你仍然将调整大小的图像流保存到 clientstream,这意味着你将两个图像存储到一个图像中。

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

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

    主要方法:

     //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;
            }
    

    结果:

    【讨论】: