Transparency on Bitmaps

Z

Zanna

Hi all!

I've done the class that follows to manage transparency on bitmaps.

I do this to add the class to my form

Me.PictureBox2 = New TransparentPictureBox
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.PictureBox2.Image =
CType(Resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(116, 108)
Me.PictureBox2.Size = New System.Drawing.Size(44, 28)
PictureBox2.SetTranparentColor(Color.Black)
Me.Controls.Add(PictureBox2)

It works, but not very well :)

The transparency does not show what there is under the picturebox, but
it simply does not refresh (try to open the start-menu over the PB and
then close it)

Here is the class
------------------

Imports System.Drawing.Imaging
Imports System.Drawing

Public NotInheritable Class TransparentPictureBox
Inherits PictureBox

Dim attr As ImageAttributes

Public Sub SetTranparentColor(ByVal color As Color)
SetTranparentColors(color, color)
End Sub


Public Sub SetTranparentColors(ByVal lowColor As Color, ByVal
hiColor As Color)
attr = New ImageAttributes
attr.SetColorKey(lowColor, hiColor)
End Sub

Public Function GetPixel(ByVal x As Int32, ByVal y As Int32) As Color
Try
Return DirectCast(Me.Image, Bitmap).GetPixel(x, y)
Catch
Return Color.Black
End Try
End Function

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim dstRect As New Rectangle(0, 0, MyBase.Width, MyBase.Height)
Dim g As Graphics = e.Graphics
If attr Is Nothing Then
g.DrawImage(Me.Image, 0, 0)
Else
g.DrawImage(Me.Image, dstRect, 0, 0, MyBase.Width,
MyBase.Height, GraphicsUnit.Pixel, attr)
End If
End Sub

End Class
 
A

Alex Yakhnin [MVP]

That's the "correct" behaviour. Windows CE doesn't support window
transparency.
 
Z

Zanna

Alex Yakhnin said:
That's the "correct" behaviour. Windows CE doesn't support window
transparency.

But... so... why does the CF SDK FAQs give this as the transparency
solution?
It does not work!

And also... what the hell can I do to show an icon with transparent
background?

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