English 中文(简体)
ASP.NET C#——从金字典中获取图像,将其节省到服务器上。
原标题:ASP.NET C# - get image from QueryString and save it to server as .ico

我把新东西用在像样子中,因此我需要一些帮助,以解决这个问题。

基本想法是:

  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 )

增 编

最佳回答

为此:

        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(>> , 您正在通过





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签