本文整理汇总了C#中System.Windows.Forms.WebBrowser.DrawToBitmap方法的典型用法代码示例。如果您正苦于以下问题:C# WebBrowser.DrawToBitmap方法的具体用法?C# WebBrowser.DrawToBitmap怎么用?C# WebBrowser.DrawToBitmap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.WebBrowser
的用法示例。
在下文中一共展示了WebBrowser.DrawToBitmap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
public void Start()
{
BrowserThread = new Thread(() =>
{
Browser = new WebBrowser();
Browser.Width = Ballz.The().GraphicsDevice.Viewport.Width;
Browser.Height = Ballz.The().GraphicsDevice.Viewport.Height;
Browser.ScrollBarsEnabled = false;
//Browser.IsWebBrowserContextMenuEnabled = false;
LatestBitmap = new Bitmap(Browser.Width, Browser.Height);
Browser.Validated += (s, e) =>
{
lock (this)
{
Browser.DrawToBitmap(LatestBitmap, new Rectangle(0, 0, Browser.Width, Browser.Height));
}
};
Browser.DocumentCompleted += (s, e) =>
{
lock (this)
{
Browser.DrawToBitmap(LatestBitmap, new Rectangle(0, 0, Browser.Width, Browser.Height));
}
};
Browser.Navigate("file://C:/Users/Lukas/Documents/gui.html");
var context = new ApplicationContext();
Application.Run();
});
BrowserThread.SetApartmentState(ApartmentState.STA);
BrowserThread.Start();
}
示例2: Main
static void Main(string[] args)
{
System.Console.WriteLine("Create your own web history images.");
System.Console.WriteLine("Type the URL (with http://...");
string url = System.Console.ReadLine();
System.Console.WriteLine(@"Save to location (e.g. C:\Images\)");
string path = System.Console.ReadLine();
IArchiveService service = new WebArchiveService();
Website result = service.Load(url);
System.Console.WriteLine("WebArchive Sites found: " + result.ArchiveWebsites.Count);
WebBrowser wb = new WebBrowser();
int i = 0;
foreach (ArchiveWebsite site in result.ArchiveWebsites)
{
i++;
System.Console.WriteLine("Save image (Date " + site.Date.ToShortDateString() + ") number: " + i.ToString());
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(site.ArchiveUrl);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
wb.Width = wb.Document.Body.ScrollRectangle.Width;
wb.Height = wb.Document.Body.ScrollRectangle.Height;
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
bitmap.Save(path + site.Date.Year.ToString() + "_" + site.Date.Month.ToString() + "_" + site.Date.Day.ToString() + ".bmp");
}
wb.Dispose();
System.Console.WriteLine(result.Url);
}
示例3: GetScreenshot
public static Bitmap GetScreenshot(Uri uri, int width, int height)
{
Bitmap bitmap;
using (var webBrowser = new WebBrowser())
{
webBrowser.ScrollBarsEnabled = false;
webBrowser.ScriptErrorsSuppressed = true;
webBrowser.Navigate(uri);
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
webBrowser.Width = width > 0
? width
: webBrowser.Document.Body.ScrollRectangle.Width;
webBrowser.Height = height > 0
? height
: webBrowser.Document.Body.ScrollRectangle.Height;
if (webBrowser.Height <= 0)
webBrowser.Height = 768;
bitmap = new Bitmap(webBrowser.Width, webBrowser.Height);
webBrowser.DrawToBitmap(bitmap,
new Rectangle(0, 0, webBrowser.Width,
webBrowser.Height));
}
return bitmap;
}
示例4: GenerateScreenshot
public Bitmap GenerateScreenshot(string url, int width, int height)
{
WebBrowser wb = new WebBrowser();
wb.NewWindow += wb_NewWindow;
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
Thread.Sleep(int.Parse(ConfigurationSettings.AppSettings["WaitForLoadWebSite"].ToString().Trim()));
wb.Width = width;
wb.Height = height;
if (width == -1)
{
wb.Width = wb.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
wb.Height = wb.Document.Body.ScrollRectangle.Height;
}
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
}
示例5: Main
static void Main(string[] args)
{
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate("http://code-inside.de");
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
wb.Width = wb.Document.Body.ScrollRectangle.Width;
wb.Height = wb.Document.Body.ScrollRectangle.Height;
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
bitmap.Save("C://screenshot.bmp");
}
示例6: GetWebPageWorker
protected void GetWebPageWorker()
{
using (var browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.Navigate(_url);
// Wait for control to load page
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
browser.ClientSize = new Size(1280, 1024);
Thread.Sleep(1000);
bitmap = new Bitmap(1280, 1024);
browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
browser.Dispose();
}
}
示例7: ExtractSnapshot
internal static Image ExtractSnapshot(WebBrowser browser)
{
browser.ClientSize = new Size(850, 1500);
Rectangle bounds = browser.Document.Body.ScrollRectangle;
IHTMLElement2 body = browser.Document.Body.DomElement as IHTMLElement2;
IHTMLElement2 doc = (browser.Document.DomDocument as IHTMLDocument3).documentElement as IHTMLElement2;
int scrollHeight = Math.Max(body.scrollHeight, bounds.Height);
int scrollWidth = Math.Max(body.scrollWidth, bounds.Width);
scrollHeight = Math.Max(body.scrollHeight, scrollHeight);
scrollWidth = Math.Max(doc.scrollWidth, scrollWidth);
Rectangle finalBounds = new Rectangle(0, 0, scrollWidth, scrollHeight);
browser.ClientSize = finalBounds.Size;
var bitmap = new Bitmap(scrollWidth, scrollHeight);
browser.BringToFront();
browser.DrawToBitmap(bitmap, finalBounds);
return bitmap;
}
示例8: DoCapture
public void DoCapture(String url, int width = 1024, int height = 768)
{
try
{
WebBrowser browser = new WebBrowser();
browser.ScrollBarsEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.Navigate(url);
while (browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
// Set the size of the WebBrowser control
browser.Width = width;
browser.Height = height;
if (width == -1)
{
// get image with full width
browser.Width = browser.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
//get image with full height
browser.Height = browser.Document.Body.ScrollRectangle.Height;
}
Bitmap bitmap = new Bitmap(browser.Width, browser.Height);
browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
browser.Dispose();
result= bitmap;
}
catch (Exception ex)
{
result= null;
}
}
示例9: Main
static void Main(string[] args)
{
_wb = new WebBrowser { ScrollBarsEnabled = false, ScriptErrorsSuppressed = true };
_wb.Navigate("http://www.shirdisaibabaaz.org/Reports/RegularReceipts");
while (_wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
System.Threading.Thread.Sleep(1000);
if (_wb.Document != null)
{
if (_wb.Document.Body != null)
{
int width = _wb.Document.Body.ScrollRectangle.Width;
int height = _wb.Document.Body.ScrollRectangle.Height;
_wb.Width = width;
_wb.Height = height;
var bmp = new Bitmap(width, height);
_wb.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(@"C:\Users\Santhosh\Desktop\test.bmp");
}
}
}
示例10: pptThumbnail
public void pptThumbnail(string sourceFile, string targetFile, int thumbW, int thumbH)
{
// Open the document and convert into HTML pages
Microsoft.Office.Interop.PowerPoint.Application oApp = new Microsoft.Office.Interop.PowerPoint.Application();
PowerPoint.Presentation oDoc = oApp.Presentations.Open(
sourceFile,
Microsoft.Office.Core.MsoTriState.msoTrue, // read only
Microsoft.Office.Core.MsoTriState.msoTrue, // untitled
Microsoft.Office.Core.MsoTriState.msoFalse); // with window
string tmpHtmlFile = System.IO.Path.GetTempFileName() + ".html";
oApp.Presentations[1].SaveCopyAs(
tmpHtmlFile,
PowerPoint.PpSaveAsFileType.ppSaveAsHTML, // format
Microsoft.Office.Core.MsoTriState.msoTrue); // embed true type font
// Create the thumbnail from the HTML pages
Size browserSize = new Size(800, 800);
WebBrowser browser = new WebBrowser();
browser.Size = browserSize;
browser.Navigate(tmpHtmlFile);
while (WebBrowserReadyState.Complete != browser.ReadyState)
{
Application.DoEvents();
}
Bitmap bm = new Bitmap(browserSize.Width, browserSize.Height);
browser.DrawToBitmap(bm,
new Rectangle(0, 0, browserSize.Width, browserSize.Height));
Bitmap thumbnail = new Bitmap(thumbW, thumbH);
Graphics g = Graphics.FromImage(thumbnail);
g.DrawImage(
bm,
new Rectangle(0, 0, thumbnail.Width, thumbnail.Height),
new Rectangle(0, 0, browserSize.Width, browserSize.Height),
GraphicsUnit.Pixel);
thumbnail.Save(targetFile);
}
示例11: GenerateScreenshot
public Bitmap GenerateScreenshot(string url, int width, int height) {
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true; wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
// Set the size of the
wb.Width = width; wb.Height = height;
if (width == -1) {
// Take Screenshot of the web pages full width
wb.Width = wb.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
// Take Screenshot of the web pages full height
wb.Height = wb.Document.Body.ScrollRectangle.Height;
}
// 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 Rectangle(0, 0, wb.Width, wb.Height)); wb.Dispose();
return bitmap;
}
示例12: GetThumbnail
private void GetThumbnail()
{
int wm = int.MaxValue;
int hm = int.MaxValue;
foreach (var screen in Screen.AllScreens)
{
wm = Math.Min(screen.Bounds.Width, wm);
hm = Math.Min(screen.Bounds.Height, hm);
}
try
{
using (var web = new WebBrowser())
{
web.ScrollBarsEnabled = false;
web.ScriptErrorsSuppressed = true;
DateTime start = DateTime.UtcNow;
web.Navigate(m_url);
while (web.ReadyState != WebBrowserReadyState.Complete && (DateTime.UtcNow -start).TotalSeconds < 30)
Application.DoEvents();
int w, h;
try
{
dynamic doc = web.Document.DomDocument;
dynamic body = web.Document.Body;
body.DomElement.contentEditable = true;
doc.documentElement.style.overflow = "hidden";
}
catch
{ }
try
{
w = Math.Min(web.Document.Body.ScrollRectangle.Width, wm);
h = Math.Min(web.Document.Body.ScrollRectangle.Height, hm);
}
catch
{
w = Math.Min(web.Document.Body.ClientRectangle.Width, wm);
h = Math.Min(web.Document.Body.ClientRectangle.Height, hm);
}
//br.Width = w;
//br.Height = h;
web.ClientSize = new Size(w, h);
m_img = new Bitmap(w, h, PixelFormat.Format24bppRgb);
web.DrawToBitmap(m_img, new Rectangle(0, 0, w, h));
}
}
catch
{
if (this.m_img != null)
this.m_img.Dispose();
}
}
示例13: GenerateThumbnailFromCompletedPage
private void GenerateThumbnailFromCompletedPage(WebBrowser m_WebBrowser)
{
m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
m_Bitmap = (Bitmap) m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
}
示例14: GetImage
public BitmapImage GetImage()
{
BitmapImage result = null;
if (ImageAllowed)
{
using (var browser = new System.Windows.Forms.WebBrowser())
{
browser.ScriptErrorsSuppressed = true;
browser.ScrollBarsEnabled = false;
browser.DocumentCompleted += (s, e) =>
{
var width = browser.Document.InvokeScript("eval", new object[] { @"
function documentWidth(){
return Math.max(
document.documentElement['clientWidth'],
document.body['scrollWidth'], document.documentElement['scrollWidth'],
document.body['offsetWidth'], document.documentElement['offsetWidth']
);
}
documentWidth();
"});
var height = browser.Document.InvokeScript("eval", new object[] { @"
function documentHeight(){
return Math.max(
document.documentElement['clientHeight'],
document.body['scrollHeight'], document.documentElement['scrollHeight'],
document.body['offsetHeight'], document.documentElement['offsetHeight']
);
}
documentHeight();
"});
if (height != null && width != null)
{
browser.Height = int.Parse(height.ToString());
browser.Width = int.Parse(width.ToString());
using (var pic = new Bitmap(browser.Width, browser.Height))
{
browser.Focus();
browser.DrawToBitmap(pic, new System.Drawing.Rectangle(0, 0, pic.Width, pic.Height));
var strm = new MemoryStream();
pic.Save(strm, System.Drawing.Imaging.ImageFormat.Jpeg);
strm.Seek(0, SeekOrigin.Begin);
result = new BitmapImage();
result.BeginInit();
result.StreamSource = strm;
result.DecodePixelHeight = 300;
result.EndInit();
}
}
else
{
result = new System.Windows.Media.Imaging.BitmapImage(new Uri(_url));
}
};
browser.Navigate(_url);
while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
}
else
{
result = new System.Windows.Media.Imaging.BitmapImage(new Uri(_url));
}
return result;
}
示例15: CaptureWebPageBytesP
static byte[] CaptureWebPageBytesP(string body, int width, int height)
{
byte[] data;
using (WebBrowser web = new WebBrowser())
{
web.ScrollBarsEnabled = false; // no scrollbars
web.ScriptErrorsSuppressed = true; // no errors
web.DocumentText = body;
while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
web.Width = web.Document.Body.ScrollRectangle.Width;
web.Height = web.Document.Body.ScrollRectangle.Height;
// a bitmap that we will draw to
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(web.Width, web.Height))
{
// draw the web browser to the bitmap
web.DrawToBitmap(bmp, new Rectangle(web.Location.X, web.Location.Y, web.Width, web.Height));
// draw the web browser to the bitmap
GraphicsUnit units = GraphicsUnit.Pixel;
RectangleF destRect = new RectangleF(0F, 0F, width, height);
RectangleF srcRect = new RectangleF(0, 0, web.Width, web.Width * 1.5F);
Bitmap b = new Bitmap(width, height);
Graphics g = Graphics.FromImage((Image)b);
g.Clear(Color.White);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, destRect, srcRect, units);
g.Dispose();
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
EncoderParameter qualityParam = null;
EncoderParameters encoderParams = null;
try
{
ImageCodecInfo imageCodec = null;
imageCodec = GetEncoderInfo("image/jpeg");
qualityParam = new EncoderParameter(Encoder.Quality, 100L);
encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
b.Save(stream, imageCodec, encoderParams);
}
catch (Exception)
{
throw new Exception();
}
finally
{
if (encoderParams != null) encoderParams.Dispose();
if (qualityParam != null) qualityParam.Dispose();
}
b.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
}
}
}
return data;
}