2011-09-07 14 views
0

我是asp.net中的新手,所以我需要一些幫助來解決這個問題。ASP.NET C# - 從QueryString中獲取圖像並將其保存到服務器上像.ico

基本思路是:從查詢字符串

  1. 獲取圖像,例如:/Default.aspx?src=http://www.google.hr/images/logo.png
  2. 將其轉換和調整爲16×16像素 「ICO」 IE compilant
  3. 將其保存到服務器上,並打印/回聲網址ICO

使用ASP.NET 3.5 C# 這是我的嘗試:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Drawing; 
using System.IO; 
using System.Net; 

namespace WebApplication2 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

      var source = Request.QueryString["src"]; 

      if (source != null) 
      { 

       WebClient webclient = new WebClient(); 
       using (Stream stream = webclient.OpenRead(source)) 
       { 
        Bitmap iconbitmap = new Bitmap(System.Drawing.Image.FromFile(webclient)); 
        var icon = Icon.FromHandle((iconbitmap).GetHicon()); 
        FileStream fs = new FileStream("/test1.ico", FileMode.Create); 
        icon.Save(fs); 
        fs.Close(); 
       } 
      } 
     } 
    } 
} 

編輯:

得到了一些錯誤 (錯誤1 'System.Drawing.Image.FromFile(串)' 的最佳重載的方法匹配具有一些無效參數)

感謝

+0

什麼是你的問題? – Tejs

+1

你想幫忙什麼樣的問題? –

+0

我編輯了...我發佈了一些錯誤代碼。我不知道下一步該做什麼。 – enloz

回答

2

試試這個:

 WebClient webclient = new WebClient(); 
     using (Stream stream = webclient.OpenRead(source)) 
     { 
      Bitmap iconbitmap = new Bitmap(System.Drawing.Image.FromStream(stream)); 
      var icon = Icon.FromHandle((iconbitmap).GetHicon()); 
      FileStream fs = new FileStream("/test1.ico", FileMode.Create); 
      icon.Save(fs); 
      fs.Close(); 
     } 

,或者如果你不需要轉換:

 WebClient webclient = new WebClient(); 
     webclient.DownloadFile(source, "/test1.ico"); 
+0

非常感謝,真棒!它運作良好! :) – enloz

2

System.Drawing.Image.FromFile()正期待一個字符串,你傳遞給它一個WebClient

+0

感謝您的重播...正如我所說,我是ASP新手,這是我的第一個應用程序。 Samich解決方案效果很好。 – enloz

相關問題