我使用以下功能将文件上传到azure存储帐户。
正如您将看到它没有任何大小调整等:
public string UploadToCloud(FileUpload fup,string containerName) { //从连接字符串中检索存储帐户。 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings [“StorageConnectionString”]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
string newName = "";
string ext = "";
CloudBlockBlob blob = null;
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
newName = "";
ext = Path.GetExtension(fup.FileName);
newName = string.Concat(Guid.NewGuid(), ext);
blob = container.GetBlockBlobReference(newName);
blob.Properties.ContentType = fup.PostedFile.ContentType;
//S5: Upload the File as ByteArray
blob.UploadFromStream(fup.FileContent);
return newName;
}
然后我有这个功能,我在未在azure上托管的网站上使用过:
public string ResizeandSave(FileUpload fileUpload, int width, int height, bool deleteOriginal, string tempPath = @"~\tempimages\", string destPath = @"~\cmsgraphics\")
{
fileUpload.SaveAs(Server.MapPath(tempPath) + fileUpload.FileName);
var fileExt = Path.GetExtension(fileUpload.FileName);
var newFileName = Guid.NewGuid().ToString() + fileExt;
var imageUrlRS = Server.MapPath(destPath) + newFileName;
var i = new ImageResizer.ImageJob(Server.MapPath(tempPath) + fileUpload.FileName, imageUrlRS, new ImageResizer.ResizeSettings(
"width=" + width + ";height=" + height + ";format=jpg;quality=80;mode=max"));
i.CreateParentDirectory = true; //Auto-create the uploads directory.
i.Build();
if (deleteOriginal)
{
var theFile = new FileInfo(Server.MapPath(tempPath) + fileUpload.FileName);
if (theFile.Exists)
{
File.Delete(Server.MapPath(tempPath) + fileUpload.FileName);
}
}
return newFileName;
}
现在我要做的是尝试将两者合并......或者至少找出一种能够在将图像存储到azure之前调整图像大小的方法。
有人有什么想法吗?
答案 0 :(得分:1)
我希望你已经让它成功了,但我今天正在寻找一些有效的解决方案,我找不到它。但最后我做到了。
这是我的代码,希望对某人有所帮助:
/// <summary>
/// Saving file to AzureStorage
/// </summary>
/// <param name="containerName">BLOB container name</param>
/// <param name="MyFile">HttpPostedFile</param>
public static string SaveFile(string containerName, HttpPostedFile MyFile, bool resize, int newWidth, int newHeight)
{
string fileName = string.Empty;
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists(BlobContainerPublicAccessType.Container);
string timestamp = Helper.GetTimestamp() + "_";
string fileExtension = System.IO.Path.GetExtension(MyFile.FileName).ToLower();
fileName = timestamp + MyFile.FileName;
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
blockBlob.Properties.ContentType = MimeTypeMap.GetMimeType(fileExtension);
if (resize)
{
Bitmap bitmap = new Bitmap(MyFile.InputStream);
int oldWidth = bitmap.Width;
int oldHeight = bitmap.Height;
GraphicsUnit units = System.Drawing.GraphicsUnit.Pixel;
RectangleF r = bitmap.GetBounds(ref units);
Size newSize = new Size();
float expectedWidth = r.Width;
float expectedHeight = r.Height;
float dimesion = r.Width / r.Height;
if (newWidth < r.Width)
{
expectedWidth= newWidth;
expectedHeight = expectedWidth/ dimesion;
}
else if (newHeight < r.Height)
{
expectedHeight = newHeight;
expectedWidth= dimesion * expectedHeight;
}
if (expectedWidth> newWidth)
{
expectedWidth= newWidth;
expectedHeight = expectedHeight / expectedWidth;
}
else if (nPozadovanaVyska > newHeight)
{
expectedHeight = newHeight;
expectedWidth= dimesion* expectedHeight;
}
newSize.Width = (int)Math.Round(expectedWidth);
newSize.Height = (int)Math.Round(expectedHeight);
Bitmap b = new Bitmap(bitmap, newSize);
Image img = (Image)b;
byte[] data = ImageToByte(img);
blockBlob.UploadFromByteArray(data, 0, data.Length);
}
else
{
blockBlob.UploadFromStream(MyFile.InputStream);
}
}
catch
{
fileName = string.Empty;
}
return fileName;
}
/// <summary>
/// Image to byte
/// </summary>
/// <param name="img">Image</param>
/// <returns>byte array</returns>
public static byte[] ImageToByte(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
答案 1 :(得分:0)
我在Windows Phone 8应用上进行了大小调整操作。正如您可以想象的那样,与整个.NET运行时相比,WP8的运行时更加有限,因此如果您没有与我相同的限制,可能还有其他选项可以更有效地执行此操作。
public static void ResizeImageToUpload(Stream imageStream, PhotoResult e)
{
int fixedImageWidthInPixels = 1024;
int verticalRatio = 1;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);
if (bitmap.PixelWidth > fixedImageWidthInPixels)
verticalRatio = bitmap.PixelWidth / fixedImageWidthInPixels;
writeableBitmap.SaveJpeg(imageStream, fixedImageWidthInPixels,
bitmap.PixelHeight / verticalRatio, 0, 80);
}
如果图像宽度大于1024像素,代码将调整图像大小。变量verticalRatio
用于计算图像的比例,以便在调整大小时不会丢失其宽高比。然后代码使用80%的原始图像质量对新jpeg进行编码。
希望这有帮助