调整大小后图像为空白
Image is Blank After 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(流)的图像为 Jpeg 格式。一旦重新调整大小,它就是 MemoryBmp 格式。
根据你的描述,上传图片大小为0字节的原因是stream有position值,而你上传steam到blob存储时没有将position值设置为0。
另外,由于clientstream已经有值了,但是你还是把调整大小后的图片流保存到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;
}
结果:
这是我的代码:
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 格式。
根据你的描述,上传图片大小为0字节的原因是stream有position值,而你上传steam到blob存储时没有将position值设置为0。
另外,由于clientstream已经有值了,但是你还是把调整大小后的图片流保存到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;
}
结果: