简体   繁体   中英

ASP.NET C# - get image from QueryString and save it to server as .ico

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM