Picturebox semitransparente en VB.NET 2010

(1/2) > >>

bybaal:
Hola a todos, necesito ayuda para lograr poner un picturebox semitransparente, no me refiero a un png o gif con fondo transparente , lo que necesito es una imagen, por ejemplo un cuadrado azul, que se vea un 25% transparente y poder cambiar el valor de transparencia en tiempo de ejecución.

Gracias por la ayuda de los que puedan.

Eleкtro:
Cita de: bybaal en 22 Diciembre 2015, 17:23 pm

lo que necesito es una imagen, por ejemplo un cuadrado azul, que se vea un 25% transparente y poder cambiar el valor de transparencia en tiempo de ejecución.

Creo que te refieres a lo siguiente, y te sugiero hacerlo de la siguiente manera:

1. Mantener una referencia a la imagen original, opaca.
2. Crear una copia de la imagen opaca, con la transparencia deseada;
    cada vez que quieras modificar la transparencia haces una copia de la imagen opaca, y liberas los recursos de la copia semi-transparente anterior.

Código sacado de mi framework y modificado un poco a las circunstancias:
Código
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sets the opacity of an <see cref="Image"/>.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim img As Image = Bitmap.FromFile("C:\File.png").SetOpacity(opacity:=0.5F)
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="sender">
''' The source <see cref="Image"/>.
''' </param>
'''
''' <param name="opacity">T
''' The target opacity level, from <c>0.0</c> to <c>1.0</c>.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The <see cref="Image"/> with the opacity applied.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="ArgumentOutOfRangeException">
''' opacity
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Function SetOpacity(ByVal sender As Image, ByVal opacity As Single) As Image
 
   If (opacity < 0.0F) OrElse (opacity > 1.0F) Then
       Throw New ArgumentOutOfRangeException(paramName:="opacity")
 
   Else
       Using g As Graphics = Graphics.FromImage(sender)
 
           Dim matrix As New ColorMatrix
           matrix.Matrix33 = opacity
 
           Using ia As New ImageAttributes
 
               ia.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
               g.DrawImage(sender, New Rectangle(0, 0, sender.Width, sender.Height), 0, 0, sender.Width, sender.Height, GraphicsUnit.Pixel, ia)
 
           End Using
 
       End Using
 
       Return sender
 
   End If
 
End Function

Source original:
ElektroKit/Elektro.Imaging/Extensions/ImageExtensions.vbElektroKit/Elektro.Imaging/Extensions/BitmapExtensions.vb
Saludos!

bybaal:
Discúlpame, pero no queda muy claro los dos primeros pasos si pudiera ejemplificar un poco, sería magnifico.

Gracias

Cita de: Eleкtro en 22 Diciembre 2015, 18:15 pm

Creo que te refieres a lo siguiente, y te sugiero hacerlo de la siguiente manera:

1. Mantener una referencia a la imagen original, opaca.
2. Crear una copia de la imagen opaca, con la transparencia deseada;
    cada vez que quieras modificar la transparencia haces una copia de la imagen opaca, y liberas los recursos de la copia semi-transparente anterior.

Código sacado de mi framework y modificado un poco a las circunstancias:
Código
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sets the opacity of an <see cref="Image"/>.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim img As Image = Bitmap.FromFile("C:\File.png").SetOpacity(opacity:=0.5F)
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="sender">
''' The source <see cref="Image"/>.
''' </param>
'''
''' <param name="opacity">T
''' The target opacity level, from <c>0.0</c> to <c>1.0</c>.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The <see cref="Image"/> with the opacity applied.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="ArgumentOutOfRangeException">
''' opacity
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Function SetOpacity(ByVal sender As Image, ByVal opacity As Single) As Image
 
   If (opacity < 0.0F) OrElse (opacity > 1.0F) Then
       Throw New ArgumentOutOfRangeException(paramName:="opacity")
 
   Else
       Using g As Graphics = Graphics.FromImage(sender)
 
           Dim matrix As New ColorMatrix
           matrix.Matrix33 = opacity
 
           Using ia As New ImageAttributes
 
               ia.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
               g.DrawImage(sender, New Rectangle(0, 0, sender.Width, sender.Height), 0, 0, sender.Width, sender.Height, GraphicsUnit.Pixel, ia)
 
           End Using
 
       End Using
 
       Return sender
 
   End If
 
End Function

Source original:
ElektroKit/Elektro.Imaging/Extensions/ImageExtensions.vbElektroKit/Elektro.Imaging/Extensions/BitmapExtensions.vb
Saludos!


Eleкtro:
Aquí tienes un ejemplo:

Código
Imports System.Drawing.Imaging
 
Public NotInheritable Class Form1 : Inherits Form
 
   Private srcImg As Image ' Source image.
   Private tmpImg As Image ' Transparent image.
 
   Public Sub New()
       MyClass.InitializeComponent()
       Me.srcImg = Bitmap.FromFile("C:\image.bmp")
   End Sub
 
   Private Sub Button1_Click() Handles Button1.Click
 
       If (Me.tmpImg IsNot Nothing) Then
           Me.tmpImg.Dispose()
       End If
 
       Me.tmpImg = Me.SetOpacity(Me.srcImg, 0.25) ' 25%
       Me.PictureBox1.BackgroundImage = Me.tmpImg
 
   End Sub
 
   Public Function SetOpacity(ByVal img As Image, ByVal opacity As Single) As Image
 
       If (opacity < 0.0F) OrElse (opacity > 1.0F) Then
           Throw New ArgumentOutOfRangeException(paramName:="opacity")
 
       Else
           Dim bmp As New Bitmap(img.Width, img.Height)
 
           Using g As Graphics = Graphics.FromImage(bmp)
 
               Dim matrix As New ColorMatrix
               matrix.Matrix33 = opacity
 
               Using ia As New ImageAttributes
 
                   ia.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
                   g.DrawImage(img, New Rectangle(0, 0, bmp.Width, bmp.Height),
                               0, 0, bmp.Width, bmp.Height,
                               GraphicsUnit.Pixel, ia)
 
               End Using
 
           End Using
 
           Return bmp
 
       End If
 
   End Function
 
End Class
 

Saludos

bybaal:
Lo siento mucho, pero la imagen sale completamente sin transparencia, no importa el porciento que se le ponga

Cita de: Eleкtro en 22 Diciembre 2015, 21:04 pm

Aquí tienes un ejemplo:

Código
Imports System.Drawing.Imaging
 
Public NotInheritable Class Form1 : Inherits Form
 
   Private srcImg As Image ' Source image.
   Private tmpImg As Image ' Transparent image.
 
   Public Sub New()
       MyClass.InitializeComponent()
       Me.srcImg = Bitmap.FromFile("C:\image.bmp")
   End Sub
 
   Private Sub Button1_Click() Handles Button1.Click
 
       If (Me.tmpImg IsNot Nothing) Then
           Me.tmpImg.Dispose()
       End If
 
       Me.tmpImg = Me.SetOpacity(Me.srcImg, 0.25) ' 25%
       Me.PictureBox1.BackgroundImage = Me.tmpImg
 
   End Sub
 
   Public Function SetOpacity(ByVal img As Image, ByVal opacity As Single) As Image
 
       If (opacity < 0.0F) OrElse (opacity > 1.0F) Then
           Throw New ArgumentOutOfRangeException(paramName:="opacity")
 
       Else
           Dim bmp As New Bitmap(img.Width, img.Height)
 
           Using g As Graphics = Graphics.FromImage(bmp)
 
               Dim matrix As New ColorMatrix
               matrix.Matrix33 = opacity
 
               Using ia As New ImageAttributes
 
                   ia.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
                   g.DrawImage(img, New Rectangle(0, 0, bmp.Width, bmp.Height),
                               0, 0, bmp.Width, bmp.Height,
                               GraphicsUnit.Pixel, ia)
 
               End Using
 
           End Using
 
           Return bmp
 
       End If
 
   End Function
 
End Class
 

Saludos

Navegación

[0] Índice de Mensajes

[#] Página Siguiente