Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Keyen Night en 18 Noviembre 2012, 03:10 am



Título: Fondo Blur GDI+ [Aporte]
Publicado por: Keyen Night en 18 Noviembre 2012, 03:10 am
El Visual Studio es excelente para el diseño a falta de potencia, entonces si tenemos esta herramienta pues explotemosla :xD

Aquí le dejo un modulo que quizá a alguien le pueda servir, crea un Form cuyo BackGroundImage es un ScreenShot con el efecto Blur se muestra con control de opacidad de una manera muy visual, esto sirve si queremos llamar la atención del usuario a nuestra aplicación más o menos lo que hace el UAC cuando nos da una advertencia pero con Blur, es un código sencillo que puede resaltar el detalle del diseño en cualquier aplicación...

El BlurForce mientras más alto más desenfoca pero tarda más en aplicar el efecto, se recomiendan valores del 1 al 5, por defecto es 2, la función tiene por defecto 1.

Speed define la velocidad para pasar la opacidad del Form de 0 a 1 y viceversa, por defecto es 0.025/1 ms.

Coloque la función Blur a parte para que se pudiese detallar su funcionamiento.

El Sub BlurBitmap proviene de: http://vbdotnetpower.blogspot.com/2011/06/image-effect-bluring.html

Código
  1. Module BlurBackground
  2.  
  3.    Private WithEvents Background As New Form With {.Bounds = SystemInformation.VirtualScreen, _
  4.                                                    .StartPosition = FormStartPosition.Manual, _
  5.                                                    .TopMost = True, _
  6.                                                    .Opacity = 0, _
  7.                                                    .BackgroundImageLayout = ImageLayout.None, _
  8.                                                    .FormBorderStyle = FormBorderStyle.None, _
  9.                                                    .BackgroundImage = Nothing, _
  10.                                                    .ShowIcon = False, _
  11.                                                    .ShowInTaskbar = False}
  12.  
  13.    Private WithEvents Timer As New Timer With {.Enabled = False, .Interval = 1}
  14.  
  15.    Public Speed As Double
  16.  
  17.    Private Sub BlurBitmap(ByRef Image As Bitmap, Optional ByVal BlurForce As Integer = 1)
  18.        Dim _
  19.        Graph As Graphics = Graphics.FromImage(Image), _
  20.        ImgAtt As New ImageAttributes, _
  21.        Matrix As New ColorMatrix
  22.        Matrix.Matrix33 = 0.5F
  23.        ImgAtt.SetColorMatrix(Matrix)
  24.        For x As Integer = -BlurForce To BlurForce
  25.            For y As Integer = -BlurForce To BlurForce
  26.                Graph.DrawImage(Image, New Rectangle(x, y, _
  27.                                                     Image.Width, _
  28.                                                     Image.Height), _
  29.                                                     0, 0, Image.Width, _
  30.                                                     Image.Height, _
  31.                                                     GraphicsUnit.Pixel, ImgAtt)
  32.            Next
  33.        Next
  34.        ImgAtt.Dispose()
  35.        Graph.Dispose()
  36.    End Sub
  37.  
  38.    Public Sub ShowBackground()
  39.  
  40.        If Background.BackgroundImage IsNot Nothing Then
  41.            Background.BackgroundImage.Dispose()
  42.        End If
  43.  
  44.        Dim BlurBack As New Bitmap(SystemInformation.VirtualScreen.Width, _
  45.                                   SystemInformation.VirtualScreen.Height)
  46.  
  47.        Dim BlurGraph As Graphics = Graphics.FromImage(BlurBack)
  48.  
  49.        BlurGraph.CopyFromScreen(0, 0, 0, 0, SystemInformation.VirtualScreen.Size)
  50.  
  51.        BlurBitmap(BlurBack, 2)
  52.  
  53.        BlurGraph.Dispose()
  54.  
  55.        Background.BackgroundImage = BlurBack
  56.        If Not Timer.Enabled And Not Background.Visible Then
  57.            Background.Show()
  58.            Speed = 0.025
  59.            Timer.Start()
  60.        End If
  61.    End Sub
  62.  
  63.    Public Sub HideBackground()
  64.        If Not Timer.Enabled And Background.Visible Then
  65.            Speed = -0.025
  66.            Timer.Start()
  67.        End If
  68.    End Sub
  69.  
  70.    Private Sub Timer_Tick(ByVal sender As Timer, ByVal e As System.EventArgs) Handles Timer.Tick
  71.  
  72.        Select Case Background.Opacity + Speed
  73.            Case Is > 1
  74.                Timer.Stop()
  75.                Exit Sub
  76.            Case Is < 0
  77.                Timer.Stop()
  78.                Background.Hide()
  79.        End Select
  80.  
  81.        Background.Opacity += Speed
  82.  
  83.    End Sub
  84.  
  85.    'Solo por seguridad para cerrar el Form con un click'
  86.    Private Sub Background_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Background.Click
  87.        HideBackground()
  88.    End Sub
  89.  
  90. End Module