private void _export_Click(object sender, RoutedEventArgs e) { //打开对话框 System.Windows.Forms.SaveFileDialog saveFile = new System.Windows.Forms.SaveFileDialog(); saveFile.Filter = "JPG(*.jpg)|*.jpg"; saveFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } var saveFilePath = saveFile.FileName; if (saveFilePath != "") { if (web != null) { if (web.ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete) { System.Drawing.Rectangle r = web.Document.Body.ScrollRectangle; web.Height = r.Height; web.Width = r.Width; Bitmap bitMapPic = new Bitmap(r.Width, r.Height); web.DrawToBitmap(bitMapPic, r); bitMapPic.Save(saveFilePath); Toolkit.MessageBox.Show("文件导出成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information); } } } }
private void setBitmap() { using (System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser()) { wb.ScrollBarsEnabled = false; wb.Width = width; wb.Navigate(url); //确保页面被解析完全 while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(100); } System.Threading.Thread.Sleep(1000); height = wb.Document.Body.ScrollRectangle.Height + 50; wb.Height = height; bitmap = new System.Drawing.Bitmap(width, height); wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height)); wb.Dispose(); } }
/// <summary> /// Handles the <see cref="E:DocumentCompleted" /> event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Windows.Forms.WebBrowserDocumentCompletedEventArgs"/> instance containing the event data.</param> /// <remarks>...</remarks> private void OnbrowserDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e) { if (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) return; // Detach browser.DocumentCompleted -= OnbrowserDocumentCompleted; // Container image var bitmap = new System.Drawing.Bitmap(browser.Width, browser.Height); try { // Save screenshot browser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, browser.Width, browser.Height)); bitmap.Save(Path.Combine(Location, customizedSettings.ScreenshotPath), System.Drawing.Imaging.ImageFormat.Png); // Save title customizedSettings = GetSettings().Customize<AppLinkPackageCustomizedSettings>(s => { s.Title = browser.DocumentTitle; }); } catch /* Eat */ { } finally { bitmap.Dispose(); browser.Dispose(); } // Next try { // Save icon bitmap = new System.Drawing.Bitmap(customizedSettings.LinkPath.GetFavicon().ToMemoryStream()); bitmap.Save(Path.Combine(Location, customizedSettings.IconPath), System.Drawing.Imaging.ImageFormat.Png); } catch /* Eat */ { } finally { bitmap.Dispose(); } // Finally load ! Application.Current.Dispatcher.BeginInvoke(new Action(() => { ReloadTile(); })); }
/// <summary> /// Export the map to an image. /// </summary> public Image Export() { // Create a Bitmap and draw the DataGridView on it. int width; int height; Gdk.Window gridWindow = MainWidget.GdkWindow; gridWindow.GetSize(out width, out height); if (ProcessUtilities.CurrentOS.IsWindows) { // Give the browser half a second to run all its scripts // It would be better if we could tap into the browser's Javascript engine // and see whether loading of the map was complete, but my attempts to do // so were not entirely successful. Stopwatch watch = new Stopwatch(); watch.Start(); while (watch.ElapsedMilliseconds < 500) { Gtk.Application.RunIteration(); } if ((browser as TWWebBrowserIE) != null) { System.Windows.Forms.WebBrowser wb = (browser as TWWebBrowserIE).Browser; System.Drawing.Bitmap bm = new System.Drawing.Bitmap(width, height); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height); wb.DrawToBitmap(bm, rect); return(bm); } } Gdk.Pixbuf screenshot = Gdk.Pixbuf.FromDrawable(gridWindow, gridWindow.Colormap, 0, 0, 0, 0, width, height); byte[] buffer = screenshot.SaveToBuffer("png"); MemoryStream stream = new MemoryStream(buffer); System.Drawing.Bitmap bitmap = new Bitmap(stream); return(bitmap); }