Windows XP灰度在Windows窗体关闭效果
介绍 下面的代码将为Windows窗体创建一个灰度窗口关闭效果。 算法 通过捕捉窗体的屏幕截图来创建窗体的位图图像。转换位图到灰度。将面板控件的背景图像设置为灰度位图,并将其Dock属性设置为Fill。 使用的代码 创建一个窗口窗体的位图图像 在。net中没有直接的方法来捕获Windows窗体屏幕到位图中;因此,我们必须使用gdi32.dll的BitBlt函数。BitBlt函数执行将与矩形像素对应的颜色数据从指定的源设备上下文传输到目标设备上下文的位块传输。 隐藏,收缩,复制Code
//necessary function to create bitmap of form [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll", ExactSpelling = true)] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll", ExactSpelling = true)] public static extern IntPtr BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop); private Bitmap GetFormImage(Form frm) { //get graphics object of the form //pixel of the object will be copied to the bitmap Graphics g = frm.CreateGraphics(); Size s = frm.Size; //create blank bitmap of the size of form Bitmap formImage = new Bitmap(s.Width, s.Height, g); //create graphic object of the bitmap //the copied pixel will be transferred to this object Graphics mg = Graphics.FromImage(formImage); //get handles of the source and destinatio graphics objects //this handle will be used in BitBlt function IntPtr dc1 = g.GetHdc(); IntPtr dc2 = mg.GetHdc(); //here goes the function that will transfer //bits block from form graphics object to bitmap BitBlt(dc2, 0, 0, frm.ClientRectangle.Width, frm.ClientRectangle.Height, dc1, 0 , 0 , 13369376); g.ReleaseHdc(dc1); mg.ReleaseHdc(dc2); return formImage; }
转换位图到一个灰度位图 隐藏,复制Code
public static Bitmap MakeGrayscale(Bitmap original) { //make an empty bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); for (int i = 0; i < original.Width; i++) { for (int j = 0; j < original.Height; j++) { //get the pixel from the original image Color originalColor = original.GetPixel(i, j); //create the grayscale version of the pixel int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11)); //create the color object Color newColor = Color.FromArgb(grayScale, grayScale, grayScale); //set the new image's pixel to the grayscale version newBitmap.SetPixel(i, j, newColor); } } return newBitmap; }
将面板控件的背景图像设置为灰度图像,并将其Dock属性设置为Fill 隐藏,复制Code
private void btnExit_Click(object sender, EventArgs e) { //set the background image of the panel with grey scale form image panel1.BackgroundImage = MakeGrayscale(GetFormImage(this)); panel1.Visible = true; //dock this panel to fill the entire form panel1.Dock = DockStyle.Fill; if (DialogResult.Yes == MessageBox.Show("Do you want to Exit", "Exit Windows", MessageBoxButtons.YesNo)) Application.Exit(); else { panel1.BackgroundImage = null; panel1.Visible = false; } }
的兴趣点 我的代码缺少两件事,只需稍加努力,就可以改进。我希望在我的下一次更新中这样做。首先,从彩色位图到灰度图的转换立即完成。我们可以通过设置背景图像为中间灰度图像来产生渐变过渡。其次,代码在单个线程中运行,以便在转换完成后出现消息框。利用多线程的方法,我们可以在不同的线程上实现灰度转换。 本文转载于:http://www.diyabc.com/frontweb/news7012.html