ASP.NET C# - 从QueryString获取图像并将其作为.ico保存到服务器

时间:2011-09-07 17:52:45

标签: c# asp.net visual-studio ico

我是asp.net的新手,所以我需要一些帮助来解决这个问题。

基本上是想法:

  1. 从QueryString获取图片,例如:/Default.aspx?src = http://www.google.hr/images/logo.png
  2. 转换并调整为16x16 px“.ico”IE compilant
  3. 将其保存到服务器,并打印/回显URL到ico
  4. 使用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(string)'的最佳重载方法匹配有一些无效的参数)

    由于

2 个答案:

答案 0 :(得分:2)

System.Drawing.Image.FromFile()期待一个字符串,你传递的是WebClient

答案 1 :(得分: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");