1

谁能告诉我如何通过我的 ASP.NET 应用程序检索网站的图像或缩略图?我已经在一些网站上看到了这个功能,比如 Alexa 等。

4

2 回答 2

1

试试SnapCasa的免费且易于使用的服务。只需像这样形成您的图像标签:

<img src="http://SnapCasa.com/Get.aspx?code=[code]&size=[size]&url=[url]" />

需要注册,但每月 500,000 个请求是免费的。[code] 是他们在注册后提供的 api 密钥。[尺寸] 是可用的三种尺寸之一。[url] 是您要为其显示缩略图的站点的网站地址。

如果您想使用代码中的图像,这里有几个辅助方法:

static public byte[] GetBytesFromUrl(string url)
{
    byte[] b;
    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
    WebResponse myResp = myReq.GetResponse();

    Stream stream = myResp.GetResponseStream();
    //int i;
    using (BinaryReader br = new BinaryReader(stream))
    {
        //i = (int)(stream.Length);
        b = br.ReadBytes(500000);
        br.Close();
    }
    myResp.Close();
    return b;
}

static public void WriteBytesToFile(string fileName, byte[] content)
{
    FileStream fs = new FileStream(fileName, FileMode.Create);
    BinaryWriter w = new BinaryWriter(fs);
    try
    {
        w.Write(content);
    }
    finally
    {
        fs.Close();
        w.Close();
    }
}

然后,在您的代码中,只需使用:

//get byte array for image
var imageBytes = GetBytesFromUrl("http://SnapCasa.com/Get.aspx?code=[code]&size=[size]&url=[url]");
//save file to disk
WriteBytesToFile("c:\someImageFile.jpg", imageBytes);
于 2010-07-28T16:18:14.750 回答
0

应该可以使用 Web 浏览器对象并将视口保存到调整为缩略图大小的位图。

我尚未测试此代码,但在替换缩略图参数后尝试对其进行调整。

using (WebBrowser wb = new WebBrowser()) {
    wb.ScrollBarsEnabled = false;
    wb.AllowNavigation = true;
    wb.ScriptErrorsSuppressed = true;
    wb.ClientSize = new Size(thumbInfo_viewportWidth, thumbInfo_viewportHeight);

    if ((thumbInfo_Uri != null)) {
        wb.Navigate(thumbInfo_Uri.AbsoluteUri);
    } else {
        wb.Navigate("about:blank");
        HtmlDocument doc = wb.Document.OpenNew(true);
        doc.Write(thumbInfo_HTML);
        wb.Refresh(WebBrowserRefreshOption.Completely);
    }

    // create an image of the client area of the webbrowser control, than 
    // scale it down to the dimensions specified.
    if ((wb.Document != null && wb.Document.Body != null)) {
        Rectangle rec = default(Rectangle);
        rec.Size = wb.ClientSize;
        using (Bitmap fullSizeBitmap = new Bitmap(thumbInfo_viewportWidth, thumbInfo_viewportHeight)) {
            wb.DrawToBitmap(fullSizeBitmap, wb.Bounds);
            using (Bitmap scaledBitmap = new Bitmap(thumbInfo_width, thumbInfo_height)) {
                using (Graphics gr = Graphics.FromImage(scaledBitmap)) {
                    gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality;
                    gr.CompositingQuality = Drawing2D.CompositingQuality.HighQuality;
                    gr.InterpolationMode = Drawing2D.InterpolationMode.High;
                    Rectangle rect = new Rectangle(0, 0, thumbInfo_width, thumbInfo_height);
                    gr.DrawImage(fullSizeBitmap, rect, 0, 0, rec.Size.Width, rec.Size.Height, GraphicsUnit.Pixel);

                    scaledBitmap.Save(thumbInfo_physicalPath);
                }
            }
        }
    }
}

需要注意的一点是,这是一个昂贵的过程。

于 2016-08-12T15:40:11.873 回答