简体   繁体   中英

How do you retrieve a snapshot of a website in ASP.NET?

Could anyone tell me how I could retrieve an image or a thumbnail of a website through my ASP.NET application? I have seen this functionality in a few sites such as Alexa etc.

Try SnapCasa 's free and easy to use service. Just form your image tag like this:

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

Requires sign-up, but it's free for 500,000 requests a month. [code] is an api key that they provide after sign-up. [size] is one of three sizes available. [url] is the website address to the site for which you want to display a thumbnail.

If you want to work with the image from your code, here are a couple of helper methods:

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();
    }
}

Then, in your code, just use:

//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);

Should be doable using a web-browser object and saving the view port to a bitmap resized to thumbnail.

I have not tested this code but try tweaking it after substituting for the thumbnail params.

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);
                }
            }
        }
    }
}

One thing to note that it is an expensive process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM