Pregunta

Soy novato en ASP.NET, así que necesito ayuda para resolver esto.

Básicamente, la idea es:

  1. Obtenga imagen de Querystring, por ejemplo: /default.aspx?src=http://www.google.hr/images/logo.png
  2. Conviertalo y redimensional a 16x16 px ".ICO" es decir, compilante
  3. Guárdelo en el servidor e imprima/eco URL a ICO

Usando ASP.NET 3.5 C# Este es mi intento:

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

EDITAR:

Obtuve algunos errores (Error 1 La mejor coincidencia de método sobrecargado para 'System.Drawing.Image.FromFile (String)' tiene algunos argumentos no válidos)

Gracias

¿Fue útil?

Solución

Prueba esto:

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

O si no necesita conversión:

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

Otros consejos

System.Drawing.Image.FromFile() está esperando una cuerda, lo está pasando un WebClient.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top