【问题标题】:Resizing an image on the server then uploading to azure调整服务器上的图像大小,然后上传到 azure
【发布时间】:2013-02-01 13:49:58
【问题描述】:

我有以下用于将文件上传到 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;
        }

现在我要做的是尝试将两者合并......或者至少想出一种能够在将图像存储在天蓝色之前调整图像大小的方法。

有人有什么想法吗?

【问题讨论】:

    标签: c#-4.0 azure-storage


    【解决方案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;
        }
    

    【讨论】:

      【解决方案2】:

      我在 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 进行编码。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-09
        • 2016-07-20
        • 1970-01-01
        • 2013-07-12
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多