正如已经说要做到这一点的方法是叠加在现有形式的顶部的另一个控制/窗体,并将它呈现的这之上灰度版本,你既可以做到这一点使用精确的位于另一种形式在原始表单上,或者使用位于所有其他控件顶部的类似Panel
的东西。
这里是放置另一种形式恰好在第一的客户区时,你会如何做这个工作的例子。如何使用它
using (Grayscale(this))
{
MessageBox.Show("Test");
}
实施
public static Form Grayscale(Form tocover)
{
var frm = new Form
{
FormBorderStyle = FormBorderStyle.None,
ControlBox = false,
ShowInTaskbar = false,
StartPosition = FormStartPosition.Manual,
AutoScaleMode = AutoScaleMode.None,
Location = tocover.PointToScreen(tocover.ClientRectangle.Location),
Size = tocover.ClientSize
};
frm.Paint += (sender, args) =>
{
var bmp = GetFormImageWithoutBorders(tocover);
bmp = ConvertToGrayscale(bmp);
args.Graphics.DrawImage(bmp, args.ClipRectangle.Location);
};
frm.Show(tocover);
return frm;
}
private static Bitmap ConvertToGrayscale(Bitmap source)
{
var bm = new Bitmap(source.Width, source.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = source.GetPixel(x, y);
var luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
}
}
return bm;
}
private static Bitmap GetControlImage(Control ctl)
{
var bm = new Bitmap(ctl.Width, ctl.Height);
ctl.DrawToBitmap(bm, new Rectangle(0, 0, ctl.Width, ctl.Height));
return bm;
}
private static Bitmap GetFormImageWithoutBorders(Form frm)
{
// Get the form's whole image.
using (Bitmap wholeForm = GetControlImage(frm))
{
// See how far the form's upper left corner is
// from the upper left corner of its client area.
Point origin = frm.PointToScreen(new Point(0, 0));
int dx = origin.X - frm.Left;
int dy = origin.Y - frm.Top;
// Copy the client area into a new Bitmap.
int wid = frm.ClientSize.Width;
int hgt = frm.ClientSize.Height;
var bm = new Bitmap(wid, hgt);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.DrawImage(wholeForm, 0, 0,
new Rectangle(dx, dy, wid, hgt),
GraphicsUnit.Pixel);
}
return bm;
}
}
需要注意的是:
如果我找到时间,我会尝试解决其中的一些问题,但上面至少给出了您的一般想法。
注意,在WPF这将是一个容易得多。
来源:
一个快速和肮脏的把戏,我使用,使灰色显示的形式是一个额外的控件添加到窗体。该控件将对其父图像('Form.DrawToBitmap()')进行处理,对其进行处理,将其用作背景并将最大化以填充完整的表单。 – Bobby
灰度,而不是灰度 – Indy9000
@Indeera无论是正确的。 http://en.wikipedia.org/wiki/Grayscale – Keplah