Transparent Picturebox in VB.net 2005

V

vikasmaderna

Hi,

I have read the samples of transparent Picturebox, but i was not able
to get it working could anyone help me in solving the problem

****************************** Class code for Transparent PicBox
*************

Imports System.Drawing.Imaging

Imports System.Drawing.Imaging.ImageAttributes


Public Class PictureboxEX


Inherits System.Windows.Forms.PictureBox


Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)


Dim attr As Imaging.ImageAttributes = New
Imaging.ImageAttributes()
attr.SetColorKey(Color.Transparent, Color.Transparent)
' attr.SetColorKey(Color.FromArgb(255, 0, 255),
Color.FromArgb(255, 0, 255))
e.Graphics.DrawImage(Me.Image, Me.ClientRectangle, 0, 0,
Me.Image.Width, Me.Image.Height, GraphicsUnit.Pixel, attr)
End Sub
Protected Overloads Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim sb As New SolidBrush(Parent.BackColor)
e.Graphics.FillRectangle(sb, Me.ClientRectangle)

End Sub


End Class


************************** Form Code *********************

Public Class Form1
Protected WithEvents pb As PictureboxEX = New PictureboxEX
Dim x, y As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
' Dim pb1 As New PictureboxEX
Dim pb As PictureboxEX = New PictureboxEX

pb.Image = Image.FromFile("C:\gif1.gif")


pb.Location = New System.Drawing.Point(x, 0)
x += 50
' pb.Size = New System.Drawing.Size(240, 240)


Me.PictureBox1.Location = New System.Drawing.Point(56, 88)
Me.Picturebox1.Controls.Add(pb)


End Sub
End Class
***************************************

but when we use the code, we are not getting the true transparency, if
we want to see through the pictureboxes we cant, the black color of
is coming instead of form.

thanks & regards

vikas
 
A

Arun

This is in C#
Your SetColorKey need some changes,

try this
Bitmap bmp = new Bitmap(base.Image);
attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0))

Please let me know if this solved your issue.
 
V

vikas maderna

Hi Arun,

Sorry i didnt work, still the black background is coming behind the
image.

thanks for posting.
 
Joined
Aug 13, 2007
Messages
1
Reaction score
0
Dear Arun,


Thanks for your reply, but still it is not working , it is giving black color behind the picture instead of transparency.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top