ASP.NET C# - get image from QueryString and save it to server as .ico
-
27-10-2019 - |
Question
I'm newbie in asp.net so I need some help how to solve this.
Basically idea is:
- get image from QueryString, for example: /Default.aspx?src=http://www.google.hr/images/logo.png
- convert it and resize to 16x16 px ".ico" IE compilant
- 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
Solution
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");
OTHER TIPS
System.Drawing.Image.FromFile()
is expecting a string, you are passing it a WebClient
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow