2013-02-16 103 views
3

我有以下功能用於將文件上傳到蔚藍存儲帳戶。調整服務器上的映像然後上傳到azure

正如你將看到它沒有那種調整等:

公共字符串UploadToCloud(文件上傳FUP,字符串容器名稱) {// 從連接字符串檢索存儲賬戶。 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; 
     } 

現在我所要做的就是儘量合併兩個......至少工作在將圖像存儲在天藍色之前,可以調整圖像的大小。

任何人有任何想法?

回答

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進行編碼。

希望這有助於

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