0

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

基本思路是:

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

谢谢

4

2 回答 2

2

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

于 2011-09-07T17:56:45.893 回答
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"); 
于 2011-09-07T17:57:28.687 回答