2013-12-11 57 views
0

我在asp.net中有一個文件輸入。我想獲取圖像,顯示縮略圖並保存。這裏是aspx ...將圖像轉換爲縮略圖並保存它

form id="form1" runat="server" enctype="multipart/form-data"> 
<div> 
    <input type="file" runat="server" /> 
    <br /> 
    <asp:Button runat="server" ID="btn_upload" OnClick="btn_upload_Click" Text="Upload" /> 
    <br /> 
    <asp:Image ID="image_placeholder" runat="server" Visible="false" /> 
</div> 
</form> 

這裏是後面的c#代碼。我可以顯示圖像,但我很難保存。

var postedFile = httpRequest.Files[file]; 
      string fileName = postedFile.FileName; 

      if (fileName != "") 
      { 
       Stream fs = postedFile.InputStream; 

       System.Drawing.Image image = System.Drawing.Image.FromStream(fs); 

       using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero)) 
       { 
        using (MemoryStream memoryStream = new MemoryStream()) 
        { 
         thumbnail.Save(memoryStream, ImageFormat.Png); 
         Byte[] bytes2 = new Byte[memoryStream.Length]; 
         memoryStream.Position = 0; 
         memoryStream.Read(bytes2, 0, (int)bytes2.Length); 
         string base64String = Convert.ToBase64String(bytes2, 0, bytes2.Length); 
         image_placeholder.ImageUrl = "data:image/png;base64," + base64String; 
         image_placeholder.Visible = true; 
         var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 
         thumbnail.Save(desktopFolder, System.Drawing.Imaging.ImageFormat.Png); 
        } 
       } 

我得到的錯誤是 「在GDI +內容時發生一般錯誤:」我把它這是因爲我調用兩個thumbnail.Save

不管怎麼說,圖像顯示細膩,這只是問題的時候實際上保存它。

+1

可能是一個權利問題,您的iis服務器帳戶需要保存該文件的權利。 – Carra

+0

現在我只是從我的本地開發環境運行它,試圖將其保存到我的桌面 – prawn

回答

0

在這裏使用第三方庫http://imageresizer.codeplex.com/.我努力上傳圖像並保持質量,這個圖書館幫了我很多。

在你看來:

@model YourProject.ViewModels.ProductImageUploadCreateViewModel 
@using (Html.BeginForm("Upload", "ProductImage", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="ImageFile1" id="ImageFile1"> 
} 

你的控制器:

公共類ProductImageController:控制器 { [HttpPost] 公衆的ActionResult上傳(ProductImageUploadCreateViewModel視圖模型) { 如果(ModelState.IsValid ) { return View(viewModel); }

 if (viewModel.ImageFile1 != null) 
     { 
      UploadImage(viewModel.ProductId, "1", viewModel.ImageFile1); 
     } 

     // Return to where ever... 
} 

} 您的上傳方式:

私人無效UploadImage(INT的productId,串imageNo,HttpPostedFileBase鏡像文件) { 串uploadPath =使用Server.Mappath(「〜/資產/圖像/產品/「+ productId);如果(!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); }

Dictionary<string, string> versions = new Dictionary<string, string>(); 
versions.Add("_m", "width=150&height=150&scale=both&format=jpg"); // Medium size 

string filePrefix = productId + "_" + imageNo; 
versions.Add("_s", "width=90&height=90&scale=both&format=jpg"); // Small size 
versions.Add("_l", "width=300&height=300&scale=both&format=jpg"); // Large size 

foreach (string fileSuffix in versions.Keys) 
{ 
     // Generate a filename 
     string fileName = Path.Combine(uploadPath, filePrefix + fileSuffix); 

     // Let the image builder add the correct extension based on the output file type 
     fileName = ImageBuilder.Current.Build(imageFile, fileName, new ResizeSettings(versions[fileSuffix]), false, true); 
} 

} 在他們的網站上看看,有幾個樣品,你可以工作,通過的。我希望這有助於:)

1

我最近不得不編寫一個小應用程序來從PDF文件生成縮略圖。我最終使用了一個基於ImageMagick的庫,名爲Magick.NET。這個庫使得你可以很容易地完成你所描述的內容。我使用方法MagickImage.Resize()將圖像調整爲縮略圖大小,將屬性MagickImage.Format設置爲縮略圖的圖像格式,並使用方法MagickImage.Write()將縮略圖寫入磁盤。您可能想要查看https://magick.codeplex.com/

相關問題