2013-06-22 37 views
4

大家下午好,带模糊背景的C#对话框形式

只是一个简单的问题。在winform中显示一个新的对话框形式时,是否有可能使父表单模糊不清?如果默认情况下不可行,是否有任何解决方法?这里只是我想做的存档样品图片: enter image description here

+0

将图像覆盖的形式和表演上/隐藏它时,没有得到重点 – Sayse

回答

7

我不认为这是可能的,但我发现了一个类似的问题在这里: Layer effects (blur, etc.) in WinForms

的代码是从一个答案在那里,但我已经稍微改变了它......你需要用不安全的代码进行编译(参见项目属性的构建选项卡(允许不安全的代码))。

基本上,你把所有的东西放在一个面板中(在代码中称为panel1)并调用模糊函数。我还添加了unblur函数以恢复正常:

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     private PictureBox pb; 

     public Form1() 
     { 
      InitializeComponent(); 

      pb = new PictureBox(); 
      panel1.Controls.Add(pb); 
      pb.Dock = DockStyle.Fill; 
     } 

     private void Blur() 
     { 
      Bitmap bmp = Screenshot.TakeSnapshot(panel1); 
      BitmapFilter.GaussianBlur(bmp, 4); 

      pb.Image = bmp; 
      pb.BringToFront(); 
     } 

     private void UnBlur() 
     { 
      pb.Image = null; 
      pb.SendToBack(); 
     } 
    } 

    public class BitmapFilter 
    { 
     private static bool Conv3x3(Bitmap b, ConvMatrix m) 
     { 
      // Avoid divide by zero errors 
      if (0 == m.Factor) return false; 

      Bitmap bSrc = (Bitmap)b.Clone(); 

      // GDI+ still lies to us - the return format is BGR, NOT RGB. 
      BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
      BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 

      int stride = bmData.Stride; 
      int stride2 = stride * 2; 
      System.IntPtr Scan0 = bmData.Scan0; 
      System.IntPtr SrcScan0 = bmSrc.Scan0; 

      unsafe 
      { 
       byte* p = (byte*)(void*)Scan0; 
       byte* pSrc = (byte*)(void*)SrcScan0; 

       int nOffset = stride + 6 - b.Width * 3; 
       int nWidth = b.Width - 2; 
       int nHeight = b.Height - 2; 

       int nPixel; 

       for (int y = 0; y < nHeight; ++y) 
       { 
        for (int x = 0; x < nWidth; ++x) 
        { 
         nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) + 
          (pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) + 
          (pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight))/m.Factor) + m.Offset); 

         if (nPixel < 0) nPixel = 0; 
         if (nPixel > 255) nPixel = 255; 

         p[5 + stride] = (byte)nPixel; 

         nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) + 
          (pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) + 
          (pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight))/m.Factor) + m.Offset); 

         if (nPixel < 0) nPixel = 0; 
         if (nPixel > 255) nPixel = 255; 

         p[4 + stride] = (byte)nPixel; 

         nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) + 
          (pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) + 
          (pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight))/m.Factor) + m.Offset); 

         if (nPixel < 0) nPixel = 0; 
         if (nPixel > 255) nPixel = 255; 

         p[3 + stride] = (byte)nPixel; 

         p += 3; 
         pSrc += 3; 
        } 

        p += nOffset; 
        pSrc += nOffset; 
       } 
      } 

      b.UnlockBits(bmData); 
      bSrc.UnlockBits(bmSrc); 

      return true; 
     } 

     public static bool GaussianBlur(Bitmap b, int nWeight /* default to 4*/) 
     { 
      ConvMatrix m = new ConvMatrix(); 
      m.SetAll(1); 
      m.Pixel = nWeight; 
      m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2; 
      m.Factor = nWeight + 12; 

      return BitmapFilter.Conv3x3(b, m); 
     } 

     public class ConvMatrix 
     { 
      public int TopLeft = 0, TopMid = 0, TopRight = 0; 
      public int MidLeft = 0, Pixel = 1, MidRight = 0; 
      public int BottomLeft = 0, BottomMid = 0, BottomRight = 0; 
      public int Factor = 1; 
      public int Offset = 0; 
      public void SetAll(int nVal) 
      { 
       TopLeft = TopMid = TopRight = MidLeft = Pixel = MidRight = BottomLeft = BottomMid = BottomRight = nVal; 
      } 
     } 
    } 

    class Screenshot 
    { 
     public static Bitmap TakeSnapshot(Control ctl) 
     { 
      Bitmap bmp = new Bitmap(ctl.Size.Width, ctl.Size.Height); 
      System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp); 
      g.CopyFromScreen(ctl.PointToScreen(ctl.ClientRectangle.Location), new Point(0, 0), ctl.ClientRectangle.Size); 
      return bmp; 
     } 
    } 
} 
+0

感谢它帮助!但是我怎样才能让它更加模糊?我试图改变bitmapfilter.gausinganblur(bmp,1)或更高的值,然后4,但我无法获得所需的模糊效果。 –

+0

理论上你应该可以只添加第二个位图... – Sam

+1

我总是得到我的IDE的屏幕截图,而不是表单。这是为什么? Aaa ok,当您在窗体构造函数中调用函数时,它会截屏显示当前屏幕,而不是窗体,这就是为什么我将C#代码视为屏幕截图的原因:D – Legends