[英]How to fix a opacity bug with DrawToBitmap on WebBrowser Control?
According to following link and my console application the method DrawToBitmap
doesn't respect opacity. 根据以下链接和我的控制台应用程序,方法
DrawToBitmap
不尊重不透明度。
Proof link: http://social.msdn.microsoft.com/Forums/vstudio/en-US/e9704309-0c52-442d-80e0-2f8393dcd313/webbrowser-opacity-problem- 证明链接: http : //social.msdn.microsoft.com/Forums/vstudio/en-US/e9704309-0c52-442d-80e0-2f8393dcd313/webbrowser-opacity-problem-
My HTML code : http://fiddle.jshell.net/L37TC/ 我的HTML代码: http : //fiddle.jshell.net/L37TC/
<div id="fader" style="background-color: #ff0000">ffff</div>
<div style="background-color: blue; opacity:0;filter:alpha(opacity=0);">HIDDEN TEXT!</div>
SomeText
My C# console code : 我的C#控制台代码:
var bmp = new Bitmap(640,480, PixelFormat::Format32bppArgb)
var web = (System.Windows.Forms.Control)sender;
web.DrawToBitmap(bmp, Rectangle(0, 0, 640,480));
So I'm looking for alternative .NET built-in solution ( no CEF, Awesomium, or any extension please ) just a built-in feature of .NET to fix the bug or alternative solution to take screenshot of a web URL in my console application. 所以我在寻找内置的解决方案替代.NET( 无CEF,Awesomium,或任何扩展请 )只是一个内置的.NET的功能来修复bug或替代解决方案采取了网页URL的屏幕截图在我的控制台应用。
If I make WebBrowser
window visible to my client and use CopyFromScreen
the opacity is respected and HIDDEN TEXT
isn't showing, how ever I don't want to make WebBrowser
window visible to desktop screen. 如果我使
WebBrowser
窗口对我的客户端可见,并使用CopyFromScreen
,则尊重不透明性,并且不显示HIDDEN TEXT
,但是我不想使WebBrowser
窗口对桌面屏幕可见。
I'm looking for a built-in solution to take a screenshot from posted URL in the question without HIDDEN TEXT
. 我正在寻找一种内置解决方案,以从问题中没有
HIDDEN TEXT
发布的URL截取屏幕截图。 In other words a solution to respect opacity
. 换句话说, 尊重
opacity
的解决方案。
EDIT1: All pixels in my Bitmap
class
(.NET class not BMP format) has alpha value of 255. So the problem is NOT with file format. EDIT1:我的
Bitmap
class
(.NET类不是BMP格式)中的所有像素的alpha值为255。因此问题不在于文件格式。 I have tried PNG and any other .NET supported format. 我尝试了PNG和其他任何.NET支持的格式。
Complete source code (Console Template, Need to add references to System.Drawing
and System.Windows.Forms
完整的源代码(控制台模板,需要添加对
System.Drawing
和System.Windows.Forms
引用
class Program
{
static System.Windows.Forms.WebBrowser w = new System.Windows.Forms.WebBrowser();
[STAThread]
static void Main(string[] args)
{
w.Navigate("http://fiddle.jshell.net/L37TC/show/");
w.DocumentCompleted += w_DocumentCompleted;
System.Windows.Forms.Application.Run();
while (true) Console.Read();
}
static void w_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
var bmp = new System.Drawing.Bitmap(w.Width, w.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
((System.Windows.Forms.Control)w).DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, w.Width, w.Height));
for (int i = 0; i < w.Width; i++) for (int j = 0; j < w.Height; j++) if (bmp.GetPixel(i, j).A != 255)
{
Console.WriteLine("Alpha != 255");
return;
}
Console.WriteLine("All pixels have alpha value of 255");
bmp.Save(@"d:\ss.png", System.Drawing.Imaging.ImageFormat.Png);
// HIDDEN TEXT has opcity of 0 but it's appearing in image
}
}
I'm looking for a built-in solution to take a screenshot from posted URL in the question without HIDDEN TEXT.
我正在寻找一个内置解决方案,以从问题中没有隐藏文本的发布URL截取屏幕截图。 In other words a solution to respect opacity.
换句话说,尊重不透明度的解决方案。
The following code does just that: respects CSS opacity . 以下代码就是这样做的: 尊重CSS opacity 。 Amongst other things, it uses a
Metafile
object and OleDraw
API to render the web page's image. 除其他外,它使用
Metafile
对象和OleDraw
API渲染网页图像。
The test HTML: 测试HTML:
<!DOCTYPE html>
<body style='background-color: grey'>
<div style='background-color: blue; opacity: 0.2; color: yellow'>This is a text</div>
</body>
The output: 输出:
The code (console app): 代码 (控制台应用程序):
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Console_21697048
{
// http://stackoverflow.com/q/21697048/1768303
class Program
{
const string HTML = "<!DOCTYPE html><body style='background-color: grey'><div style='background-color: blue; opacity: 0.2; color: yellow'>This is a text</div></body>";
const string FILE_NAME = "webpage.png";
readonly static Size IMAGE_SIZE = new Size(320, 200);
// Main
static void Main(string[] args)
{
try
{
// enable HTML5 etc (assuming we're running IE9+)
SetFeatureBrowserFeature("FEATURE_BROWSER_EMULATION", 9000);
// force software rendering
SetFeatureBrowserFeature("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI", 1);
SetFeatureBrowserFeature("FEATURE_GPU_RENDERING", 0);
using (var apartment = new MessageLoopApartment())
{
// create WebBrowser on a seprate thread with its own message loop
var webBrowser = apartment.Invoke(() => new WebBrowser());
// navigate and wait for the result
apartment.Invoke(() =>
{
var pageLoadedTcs = new TaskCompletionSource<bool>();
webBrowser.DocumentCompleted += (s, e) =>
pageLoadedTcs.TrySetResult(true);
webBrowser.DocumentText = HTML;
return pageLoadedTcs.Task;
}).Wait();
// save the picture
apartment.Invoke(() =>
{
webBrowser.Size = IMAGE_SIZE;
var rectangle = new Rectangle(0, 0, webBrowser.Width, webBrowser.Height);
// get reference DC
using (var screenGraphics = webBrowser.CreateGraphics())
{
var screenHdc = screenGraphics.GetHdc();
// create a metafile
using (var metafile = new Metafile(screenHdc, rectangle, MetafileFrameUnit.Pixel))
{
using (var graphics = Graphics.FromImage(metafile))
{
var hdc = graphics.GetHdc();
var rect = new Rectangle(0, 0, 320, 50);
OleDraw(webBrowser.ActiveXInstance, DVASPECT_CONTENT, hdc, ref rectangle);
graphics.ReleaseHdc(hdc);
}
// save the metafile as bitmap
metafile.Save(FILE_NAME, ImageFormat.Png);
}
screenGraphics.ReleaseHdc(screenHdc);
}
});
// dispose of webBrowser
apartment.Invoke(() => webBrowser.Dispose());
webBrowser = null;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// interop
const uint DVASPECT_CONTENT = 1;
[DllImport("ole32.dll", PreserveSig = false)]
static extern void OleDraw(
[MarshalAs(UnmanagedType.IUnknown)] object pUnk,
uint dwAspect,
IntPtr hdcDraw,
[In] ref System.Drawing.Rectangle lprcBounds);
// WebBrowser Feature Control
// http://msdn.microsoft.com/en-us/library/ie/ee330733(v=vs.85).aspx
static void SetFeatureBrowserFeature(string feature, uint value)
{
if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
return;
var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\" + feature,
appName, value, RegistryValueKind.DWord);
}
}
// MessageLoopApartment
// more info: http://stackoverflow.com/a/21808747/1768303
public class MessageLoopApartment : IDisposable
{
Thread _thread; // the STA thread
TaskScheduler _taskScheduler; // the STA thread's task scheduler
public TaskScheduler TaskScheduler { get { return _taskScheduler; } }
/// <summary>MessageLoopApartment constructor</summary>
public MessageLoopApartment()
{
var tcs = new TaskCompletionSource<TaskScheduler>();
// start an STA thread and gets a task scheduler
_thread = new Thread(startArg =>
{
EventHandler idleHandler = null;
idleHandler = (s, e) =>
{
// handle Application.Idle just once
Application.Idle -= idleHandler;
// return the task scheduler
tcs.SetResult(TaskScheduler.FromCurrentSynchronizationContext());
};
// handle Application.Idle just once
// to make sure we're inside the message loop
// and SynchronizationContext has been correctly installed
Application.Idle += idleHandler;
Application.Run();
});
_thread.SetApartmentState(ApartmentState.STA);
_thread.IsBackground = true;
_thread.Start();
_taskScheduler = tcs.Task.Result;
}
/// <summary>shutdown the STA thread</summary>
public void Dispose()
{
if (_taskScheduler != null)
{
var taskScheduler = _taskScheduler;
_taskScheduler = null;
// execute Application.ExitThread() on the STA thread
Task.Factory.StartNew(
() => Application.ExitThread(),
CancellationToken.None,
TaskCreationOptions.None,
taskScheduler).Wait();
_thread.Join();
_thread = null;
}
}
/// <summary>Task.Factory.StartNew wrappers</summary>
public void Invoke(Action action)
{
Task.Factory.StartNew(action,
CancellationToken.None, TaskCreationOptions.None, _taskScheduler).Wait();
}
public TResult Invoke<TResult>(Func<TResult> action)
{
return Task.Factory.StartNew(action,
CancellationToken.None, TaskCreationOptions.None, _taskScheduler).Result;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.