Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: nolasco281 en 24 Julio 2015, 06:31 am



Título: Hacer picturebox transparente
Publicado por: nolasco281 en 24 Julio 2015, 06:31 am
Hola como estan.

Tengo una pregunta como hago transparente un picturebox, tengo dos picturebox uno encima del otro las dos son imagenes png sin fondo, pero una quiero superponerla sobre la otra pero no logro quitar el fondo de la de encima.

he probado pero el picture box dos me sique mostrando el contorno

Código
  1. PictureBox1.Controls.Add(PictureBox2)
  2. PictureBox2.BackColor = Color.Transparent
  3.  

Aca dejo unas imagenes de ejemplo de como seria lo que quiero hacer
(http://i.stack.imgur.com/JafAa.jpg)

(http://i.stack.imgur.com/NuEM6.png)

Muchas gracias saludos.


Título: Re: Hacer picturebox transparente
Publicado por: tincopasan en 24 Julio 2015, 07:16 am
lo hacía así:
Código
  1. Imports System.Drawing
  2. Imports System.Drawing.Graphics
  3.  
  4.  
  5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6. PictureBox3.Image = Overlay(PictureBox1.Image, PictureBox2.Image, Color.Magenta)
  7. End Sub
  8.  
  9. Public Function Overlay(ByVal SourceImage As Bitmap, ByVal OverlayImage As Bitmap) As Bitmap
  10.  
  11. 'Llama a la version con Color
  12. Return Overlay(SourceImage, OverlayImage, Color.Black)
  13.  
  14. End Function
  15.  
  16. Public Function Overlay(ByVal SourceImage As Bitmap, ByVal OverlayImage As Bitmap, ByVal ColorTransparent As Color) As Bitmap
  17. Dim g As Graphics
  18.  
  19. 'Obtengo Graphic de la imagen de fondo para poder dibujar sobe ella
  20. g = Drawing.Graphics.FromImage(SourceImage)
  21.  
  22. 'Hago trasparente la imagen que vamos a superponer
  23. OverlayImage.MakeTransparent(ColorTransparent)
  24.  
  25. 'Dibujo la imagen sobre el fondo
  26. g.DrawImage(OverlayImage, 0, 0)
  27.  
  28. 'Elimino manejador grafico
  29. g.Dispose()
  30.  
  31. 'Devuelve la imagen mezclada
  32. Return SourceImage
  33.  
  34. End Function
  35.  
  36. End Class
  37.  

en donde la segunda imagen (a superponer) tiene que ser el color de fondo en este caso Magenta, claro está que puede ser cualquier otro, pero preferentemente uno que no este en uso.


Título: Re: Hacer picturebox transparente
Publicado por: nolasco281 en 24 Julio 2015, 07:35 am
Hola.

Muchas gracias por responder pero en la funcion Overlay recive 3 parametros el color es decir ColorTransparent As Color pero si es transparente por que se le asigna un color.

Este es mi problema basicamente.

(http://2.bp.blogspot.com/-jrIOqbiGfLM/VbHOB0EIwAI/AAAAAAAAA-w/gHapArIAeNs/s1600/Carro.png)

Quiero que el fondo del picturebox de carro sea transparente y se vea el carro superpuesto a la carretera.

Muchas gracias por responder tincopasan

Saludos.


Título: Re: Hacer picturebox transparente
Publicado por: Eleкtro en 24 Julio 2015, 11:42 am
No te lo recomiendo, este tipo de hacks en WindowsForms solo consiguen disminuir el rendimiento general de la aplicación, pero ya te entiendo, es algo que necesitas hacer (pero podrías usar WPF y no tendrías este tipo de problemas).

Aquí tienes por donde empezar, con un control sub-classeado:

(http://i.imgur.com/F8VNt87.png)

Código
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 24-July-2015
  4. ' ***********************************************************************
  5. ' <copyright file="TransparentControl.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Option Statements "
  11.  
  12. Option Explicit On
  13. Option Strict On
  14. Option Infer Off
  15.  
  16. #End Region
  17.  
  18. #Region " Imports "
  19.  
  20. Imports System
  21. Imports System.Drawing
  22. Imports System.Windows.Forms
  23.  
  24. #End Region
  25.  
  26. #Region " TransparentControl "
  27.  
  28. ''' <summary>
  29. ''' A transparent <see cref="Control"/>.
  30. ''' </summary>
  31. Public NotInheritable Class TransparentControl : Inherits Control
  32.  
  33. #Region " Properties "
  34.  
  35.    ''' <summary>
  36.    ''' Gets or sets the background image displayed in the control.
  37.    ''' </summary>
  38.    ''' <value>The background image.</value>
  39.    Public Overloads Property BackgroundImage As Image
  40.        Get
  41.            Return Me.backgroundImageB
  42.        End Get
  43.        Set(ByVal value As Image)
  44.            Me.backgroundImageB = value
  45.            MyBase.RecreateHandle()
  46.        End Set
  47.    End Property
  48.    ''' <summary>
  49.    ''' ( Backing Field )
  50.    ''' The background image displayed in the control.
  51.    ''' </summary>
  52.    Private backgroundImageB As Image
  53.  
  54.    ''' <summary>
  55.    ''' Gets or sets the image displayed in the control.
  56.    ''' </summary>
  57.    ''' <value>The image.</value>
  58.    Public Overloads Property Image As Image
  59.        Get
  60.            Return Me.imageB
  61.        End Get
  62.        Set(ByVal value As Image)
  63.            Me.imageB = value
  64.            MyBase.RecreateHandle()
  65.        End Set
  66.    End Property
  67.    ''' <summary>
  68.    ''' ( Backing Field )
  69.    ''' The background image displayed in the control.
  70.    ''' </summary>
  71.    Private imageB As Image
  72.  
  73.    ''' <summary>
  74.    ''' Gets the background color for the control.
  75.    ''' </summary>
  76.    ''' <value>The background color.</value>
  77.    <EditorBrowsable(EditorBrowsableState.Never)>
  78.    Public Shadows ReadOnly Property BackColor() As Color
  79.        Get
  80.            Return Color.Transparent
  81.        End Get
  82.    End Property
  83.  
  84.    ''' <summary>
  85.    ''' Gets the required creation parameters when the control's handle is created.
  86.    ''' </summary>
  87.    ''' <value>The creation parameters.</value>
  88.    Protected Overrides ReadOnly Property CreateParams() As CreateParams
  89.        Get
  90.            Dim cp As CreateParams = MyBase.CreateParams
  91.            cp.ExStyle = (cp.ExStyle Or 32)
  92.            Return cp
  93.        End Get
  94.    End Property
  95.  
  96. #End Region
  97.  
  98. #Region " Constructors "
  99.  
  100.    ''' <summary>
  101.    ''' Initializes a new instance of the <see cref="TransparentControl"/> class.
  102.    ''' </summary>
  103.    Public Sub New()
  104.  
  105.    End Sub
  106.  
  107. #End Region
  108.  
  109. #Region " Event-Handlers "
  110.  
  111.    ''' <summary>
  112.    ''' Handles the <see cref="E:Control.Paint"/> event.
  113.    ''' </summary>
  114.    ''' <param name="e">A <see cref="T:Forms.PaintEventArgs"/> that contains the event data.</param>
  115.    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  116.  
  117.        If Me.backgroundImageB IsNot Nothing Then ' Draw background image (filled).
  118.            e.Graphics.DrawImage(Me.backgroundImageB, Me.ClientRectangle)
  119.        End If
  120.  
  121.        If Me.imageB IsNot Nothing Then ' Draw image (centered).
  122.            e.Graphics.DrawImage(Me.imageB,
  123.                                 ((Me.ClientRectangle.Width \ 2) - (Me.imageB.Width \ 2)),
  124.                                 CSng((Me.ClientRectangle.Height / 2) - (Me.imageB.Height / 2)))
  125.        End If
  126.  
  127.    End Sub
  128.  
  129.    ''' <summary>
  130.    ''' Handles the <see cref="E:PaintBackground" /> event.
  131.    ''' </summary>
  132.    ''' <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
  133.    Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
  134.  
  135.        ' Ignore painting (don't raise PaintBackground event).
  136.  
  137.    End Sub
  138.  
  139. #End Region
  140.  
  141. End Class
  142.  
  143. #End Region

Saludos


Título: Re: Hacer picturebox transparente
Publicado por: nolasco281 en 24 Julio 2015, 14:04 pm
Hola sos grande  ;-) ;-) ;-)

Logre hacerlo con un label y un picturebox pero el problema que el label no tiene ciertas propiedades del pic.

Ya logre avanzar y hacerlo como queria muchas gracias Eleкtro

asi quedo:

(http://2.bp.blogspot.com/-ckjQZljlgE8/VbInFvE83FI/AAAAAAAAA_A/tvCMTfhaa8U/s1600/carr0.png)

Muchas gracias te agradezco el tiempo que te tomaste para hacer 143 lineas de codigo.

Claro tambien sin menos preciar la ayuda del compa~nero tincopasan

Saludos y muchas gracias y se que es poco.