asp.net c# -querystringから画像を取得し、.icoとしてサーバーに保存します
-
27-10-2019 - |
質問
私はasp.netの初心者なので、これを解決する方法が必要です。
基本的にアイデアは次のとおりです。
- QueryStringから画像を取得する:/default.aspx?src=http://www.google.hr/images/logo.png
- それを変換し、16x16 px ".ico" IEコンパイラントにサイズを変更します
- サーバーに保存し、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)'の最良のオーバーロードされたメソッドマッチがいくつかの無効な引数があります)
ありがとう
解決
これを試して:
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");
他のヒント
System.Drawing.Image.FromFile()
文字列を期待しています、あなたはそれを渡しています WebClient
.
所属していません StackOverflow