我希望我的 webBrowser 看起来透明。我的代码如下所示:
Bitmap backGroungImage = new Bitmap(Width, Height);
myWebBrowser1.Visible = false;
DrawToBitmap(backGroungImage, new Rectangle(0, 0, backGroungImage.Width, backGroungImage.Height));
myWebBrowser1.Visible = true;
Bitmap backGroundImage2 = new Bitmap(myWebBrowser1.Width - 20, myWebBrowser1.Height - 20);
Graphics.FromImage(backGroundImage2).DrawImage(backGroungImage, new Rectangle(0, 0, backGroundImage2.Width, backGroundImage2.Height), new Rectangle(SystemInformation.FrameBorderSize.Width + myWebBrowser1.Location.X + 10, SystemInformation.CaptionHeight + SystemInformation.FrameBorderSize.Height + 10 + myWebBrowser1.Location.Y, myWebBrowser1.Width - 20, myWebBrowser1.Height - 20), GraphicsUnit.Pixel);
ImageAttributes imageAttributes = new ImageAttributes();
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1f - 230f/255f, 0},
new float[] {0, 0, 0, 0, 1}
});
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Bitmap backGroundImage3 = new Bitmap(backGroundImage2.Width, backGroundImage2.Height);
Graphics.FromImage(backGroundImage3).DrawImage(backGroundImage2, new Rectangle(0, 0, backGroundImage3.Width, backGroundImage3.Height), 0, 0, backGroundImage2.Width, backGroundImage2.Height, GraphicsUnit.Pixel, imageAttributes);
backGroundImage3.Save("d:\\BG.png", ImageFormat.Png);
然后我在 html 代码中使用“d:\BG.png”。
它在我的 Windows 7 上运行良好,但在 Windows XP 上它更暗,不适合在 GDI+ 中绘制的其他白色元素,alpha = 230(我的意思是 WebBrowser 控件周围的圆形框架)。
之前,当我不使用 ColorMatrix 时,我是这样做的:
int alpha = 230;
Color c1, color = Color.FromArgb(255, 255, 255);
for (int x = 0; x < d.Width; x++)
for (int y = 0; y < d.Height; y++)
{
c1 = d.GetPixel(x, y);
d.SetPixel(x, y, Color.FromArgb(((255 - alpha) * c1.R + alpha * color.R) / 255, ((255 - alpha) * c1.G + alpha * color.G) / 255, ((255 - alpha) * c1.B + alpha * color.B) / 255));
}
在两个操作系统上看起来都很棒,但速度很慢。我希望它看起来与 GDI+ 中带有 alpha 230 的白色相同并且速度更快。顺便提一句。有没有办法在 WebBrowser 中放入背景图像,然后使其透明?
PS。它是从两台计算机上的同一文件安装的 Visual Studio 2010 Professional 和 .NET Framework 4。