1

ASP.NET アプリケーションを介して Web サイトの画像またはサムネイルを取得する方法を教えてください。この機能は、Alexa などのいくつかのサイトで見たことがあります。

4

2 に答える 2

1

SnapCasaの無料で使いやすいサービスをお試しください。次のようにイメージ タグを作成するだけです。

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

サインアップが必要ですが、1 か月あたり 500,000 件のリクエストまでは無料です。[コード] は、サインアップ後に提供される API キーです。【サイズ】は3サイズ展開です。[url] は、サムネイルを表示するサイトの Web サイト アドレスです。

コードから画像を操作する場合は、いくつかのヘルパー メソッドを次に示します。

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 に答える