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

发布于 2024-12-03 10:28:12 字数 1362 浏览 5 评论 0原文

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

基本想法是:

  1. 从 QueryString 获取图像,例如: /Default.aspx?src=http://www.google.hr/images/logo.png
  2. 将其转换并调整大小为 16x16 px “.ico” IE 兼容
  3. 将其保存到 打印/回显 ico 的 URL

服务器,并使用 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)' 的最佳重载方法匹配有一些无效参数)

谢谢

I'm newbie in asp.net so I need some help how to solve this.

Basically idea is:

  1. get image from QueryString, for example: /Default.aspx?src=http://www.google.hr/images/logo.png
  2. convert it and resize to 16x16 px ".ico" IE compilant
  3. save it to server, and print/echo URL to ico

Using ASP.NET 3.5 C#
This is my try:

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

EDIT:

Got some errors
(Error 1 The best overloaded method match for 'System.Drawing.Image.FromFile(string)' has some invalid arguments )

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-12-10 10:28:12

试试这个:

        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"); 

Try this:

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

or if you don't need conversion:

        WebClient webclient = new WebClient();
        webclient.DownloadFile(source, "/test1.ico"); 
汹涌人海 2024-12-10 10:28:12

System.Drawing.Image.FromFile() 需要一个字符串,您正在向它传递一个 WebClient

System.Drawing.Image.FromFile() is expecting a string, you are passing it a WebClient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文