Awesomium Webview表面到字节缓冲区或图片框
本文关键字:缓冲区 Webview 表面 到字节 Awesomium | 更新日期: 2023-09-27 18:02:00
我开始用c#开发,我试图将表面转换为字节缓冲区或图片(也转换为字节缓冲区)。
我在其他问题中看到了这个代码:
string fileName = Path.GetTempFileName();
webView2.Render().SaveToPng(fileName);
byte[] bytes = File.ReadAllBytes(fileName);
File.Delete(fileName);
MemoryStream ms = new MemoryStream(bytes);
但是,webview没有Render(),他也没有说我需要导入哪些库。
我停在这里:
var view = (WebView)WebCore.Views.Last();
WebCore.Update();
BitmapSurface surface = (BitmapSurface)view.Surface;
surface.??
最近在Awesomium中有许多未记录的更改。
用WebView.Surface
代替WebView.Render
。
using (WebView vw = WebCore.CreateWebView(1024, 768)) {
vw.Source = new Uri("http://www.google.com");
while (vw.IsLoading) {
WebCore.Update();
}
((BitmapSurface)vw.Surface).SaveToJPEG("D:''google.jpg");
PictureBox1.Load("D:''google.jpg");
WebCore.Shutdown();
}
在评论中指出了另一组更改。为了保证正确性,这里是更新后的代码和文档链接。
using ( webView = WebCore.CreateWebView( 800, 600 ) )
{
webView.Source = new Uri( "http://www.google.com" );
view.LoadingFrameComplete += ( s, e ) =>
{
if ( !e.IsMainFrame )
return;
BitmapSurface surface = (BitmapSurface)view.Surface;
surface.SaveToPNG( "result.png", true );
WebCore.Shutdown();
}
}
WebCore.Run();
来源:http://docs.awesomium.net/html/b2fc3fe8 - 72 - bd - 4 - baf - 980 f - b9b9456d5ca4.htm
你是说这样吗?
private static byte[] getWebViewScreenshotAsBytes(ref WebView myWebView)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myWebView.Width, myWebView.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
BitmapSurface bmpSurface = (BitmapSurface)myWebView.Surface;
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
bmpSurface.CopyTo(bmpData.Scan0, bmpSurface.RowSpan, 4, false, false);
bmp.UnlockBits(bmpData);
bmp.Save(ms, ImageFormat.Png);
}
return ms.ToArray();
}
}
double width = 800;
double height = 1000;
var webView = WebCore.CreateWebView(width, height, WebViewType.Offscreen);
webView.Source = new Uri("https://www.google.com/");
while (webView.IsLoading)
{
WebCore.Update();
}
var bitmapSurface = (BitmapSurface)webView.Surface;
var writeableBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
writeableBitmap.Lock();
bitmapSurface.CopyTo(writeableBitmap.BackBuffer, bitmapSurface.RowSpan, 4, false, false);
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
writeableBitmap.Unlock();
var image = new Image();
image.Source = writeableBitmap;
Buffer方法呢?这应该是你想要的。