using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Reflection;
using mshtml;
///
/// Summary description for Websitescreenshot
///
///
namespace GetSiteThumpnail
{
public class Websitescreenshot
{
private int S_Height;
private int S_Width;
private int F_Height;
private int F_Width;
private string MyURL;
private WebBrowser MyBrowser;
private string URL;
private int Height;
private int Width;
private System.Drawing.Bitmap oThumbNail;
public int ScreenHeight
{
get { return S_Height; }
set { S_Height = value; }
}
public int ScreenWidth
{
get { return S_Width; }
set { S_Width = value; }
}
public int ImageHeight
{
get { return F_Height; }
set { F_Height = value; }
}
public int ImageWidth
{
get { return F_Width; }
set { F_Width = value; }
}
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}
public Websitescreenshot(string WebSite, int ScreenWidth, int ScreenHeight, int ImageHeight, int ImageWidth)
{
this.WebSite = WebSite;
this.ScreenWidth = ScreenWidth;
this.ScreenHeight = ScreenHeight;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
public Bitmap GetBitmap()
{
//WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Thread m_thread = new Thread(new ThreadStart(GetIt));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
//Shot.GetIt();
Bitmap Pic = (Bitmap)oThumbNail; //DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
public void WebPageBitmap(string url, int width, int height)
{
this.Height = height;
this.Width = width;
this.URL = url;
}
public void GetIt()
{
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
//MyBrowser.Size = new Size(this.Width, this.Height);
MyBrowser.ClientSize = new Size(this.Width, this.Height);
MyBrowser.Navigate(this.URL);
MyBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MyBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser MyBrowser = (WebBrowser)sender;
Bitmap myBitmap = new Bitmap(Width, Height);
Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
oThumbNail = new Bitmap(this.ImageWidth, this.ImageHeight, imgOutput.PixelFormat);
oThumbNail.SetResolution(imgOutput.HorizontalResolution, imgOutput.VerticalResolution);
Graphics g = Graphics.FromImage(oThumbNail);
g.Clear(Color.White);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = new Rectangle(0, 0, this.ImageWidth, this.ImageHeight);
g.DrawImage(imgOutput, oRectangle);
//try
//{
// return oThumbNail;
//}
//catch
//{
// throw;
//}
//finally
//{
imgOutput.Dispose();
//MyBrowser.Dispose();
//}
}
private void GetHtml()
{
WebBrowser web = new WebBrowser();
web.Navigate(this.URL);
IHTMLDocument2 doc = web.Document.DomDocument as IHTMLDocument2;
IHTMLElement2 img = doc.images;
}
}
}
Calling this class function in the given format:-
private void CaptureScreen(string url)
{
try
{
string path = null;
path = Server.MapPath("~/images/orderdeals/");
string picName = null;
Random RandomClass = new Random();
int RandomNumber = RandomClass.Next();
picName = "order-" + Request.QueryString["id"].ToString() + "-" + RandomNumber + ".jpg"; //Format order-dealid-RandomNumber;
//int width = Int32.Parse(txtWidth.Text);
//int height = Int32.Parse(txtHeight.Text);
Websitescreenshot screenshot = new Websitescreenshot(url, 1024, 800, 800, 1024);
//Bitmap bmp = screenshot.GetBitmap();
System.Drawing.Image img = screenshot.GetBitmap();
img.Save(path + picName, ImageFormat.Jpeg);
//bmp.Save(path + "google.jpg");
//imgDeal.ImageUrl = Page.ResolveUrl("~/processimage.aspx?FilePath=../images/orderdeals/" + picName + "&height=464&width=933");
Response.Write(picName);
}
catch (Exception ex)
{
}
}
No comments:
Post a Comment