Asp.net ile başka sitenin görüntüsünü alma(Thumbnail)
SiteCapture.aspx adında bir sayfamız var ve .cs in kodları şöyle
using System.Text;
using System.Web.UI;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Collections;
using System;
namespace Matematikistan
{
public partial class SiteCapture : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Thread webBrowseThread = new Thread(new ThreadStart(GenerateScreenshot));
webBrowseThread.SetApartmentState(ApartmentState.STA);
webBrowseThread.Start();
}
//this method on providing the url of the webpage copies the image of that webpage.
public void GenerateScreenshot()
{
string url = "http://www.go4coding.com/post/2011/05/30/Getting-Screenshot-of-a-web-page-in-aspnet.aspx";
int width = 1024;
try
{
DateTime timeStamp = DateTime.Now;
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
WebBrowserReadyState flag = wb.ReadyState;
while (flag != WebBrowserReadyState.Complete && DateTime.Now < timeStamp.AddSeconds(10))
{
System.Windows.Forms.Application.DoEvents();
flag = wb.ReadyState;
TimeSpan elapsed = DateTime.Now.Subtract(timeStamp);
}
wb.Width = wb.Document.Body.ScrollRectangle.Width;
wb.Height = wb.Document.Body.ScrollRectangle.Height;
if (wb.Width < width)
{
// Take Screenshot of the web pages full width
wb.Width = width;
}
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
Graphics Grfx = Graphics.FromImage(bitmap);
Grfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Grfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Grfx.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
Grfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
Grfx.DrawImage(bitmap, 0, 0, wb.Width, wb.Height);
wb.Dispose();
SplitAndSaveImages(bitmap);
}
catch (Exception ex)
{
}
}
protected void SplitAndSaveImages(Bitmap input)
{
try
{
int height = input.Height;
int width = input.Width;
if (input.Height > 768)
{
int y = 0;
while (y < height)
{
using (Bitmap img = input.Clone() as Bitmap)
{
Bitmap newimage = img.Clone(new System.Drawing.Rectangle(0, y, width, height - y > 768 ? 768 : height - y), img.PixelFormat);
newimage.Save(@"D:\" + "output" + y.ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
y += 768;
}
}
else
{
using (Bitmap img = input.Clone() as Bitmap)
{
Bitmap newimage = img.Clone(new System.Drawing.Rectangle(0, 0, width, height), img.PixelFormat);
newimage.Save(@"D:\BitmapSplit\Output\" + "output.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}
catch (Exception ex)
{
}
}
}
}
Hiç yorum yok :
Yorum Gönder