首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ASP.NET中检索网站的快照?

如何在ASP.NET中检索网站的快照?
EN

Stack Overflow用户
提问于 2010-07-28 23:59:56
回答 2查看 825关注 0票数 1

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

EN

回答 2

Stack Overflow用户

发布于 2010-07-29 00:18:15

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

代码语言:javascript
复制
<img src="http://SnapCasa.com/Get.aspx?code=[code]&size=[size]&url=[url]" />

需要注册,但每月有500,000个请求是免费的。代码是他们在注册后提供的api密钥。大小是三个可用的大小之一。url是您要显示缩略图的网站的网站地址。

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

代码语言:javascript
复制
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();
    }
}

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

代码语言:javascript
复制
//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);
票数 1
EN

Stack Overflow用户

发布于 2016-08-12 23:40:12

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

我没有测试这段代码,但尝试在替换缩略图参数后对其进行调整。

代码语言:javascript
复制
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);
                }
            }
        }
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3354819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档