Q: Transparent image on a form

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Can anybody help me with the following? It is driving me crazy!!!

I'm trying to produce a non-rectangular form as follows:

(1) Produce a bitmap e.g. gif, using paint shop pro 5.
(2) The Background color of the bitmap is set to white.
(3) Draw a design on the bitmap in a color other than white.
(4) Create a windows application.
(5) Set the forms background image to be the bitmap created earlier.
(6) Set the transparencyKey of the form to be white i.e. the same as the
background color of the bitmap.
(7) Set the FormBorderStyle to None
(8) Run the application.

I expected just the non-white images but instead I still see the white
background! D'OH!!!

Can anybody tell me what I'm doing wrong?

Here's a link to Microsoft describing the same thing.

Thanks in adavance

Geoff
 
Geoff Jones said:
I'm trying to produce a non-rectangular form as follows:

(1) Produce a bitmap e.g. gif, using paint shop pro 5.
(2) The Background color of the bitmap is set to white.
(3) Draw a design on the bitmap in a color other than white.
(4) Create a windows application.
(5) Set the forms background image to be the bitmap created earlier.
(6) Set the transparencyKey of the form to be white i.e. the same as the
background color of the bitmap.
(7) Set the FormBorderStyle to None
(8) Run the application.

I expected just the non-white images but instead I still see the white
background! D'OH!!!

You may want to use regions instead:

Region from Bitmap
<URL:http://www.bobpowell.net/region_from_bitmap.htm>
 
Hi, I also got this problem before, this following VB code may help:)

Public Sub SetBackgroundBitmap(ByVal image As Image, ByVal
transparencyColor As Color)

Me.backgroundImage = New Bitmap(image)
Width = Me.backgroundImage .Width
Height = Me.backgroundImage .Height
Region = BitmapToRegion(Me.backgroundImage, transparencyColor)

End Sub
 
Hi Cor

If you follow the instructions in the link I gave earlier, I'd be interested
to see if you have the same problem.

Geoff
 
Geoff,

I once had the same thing and the problem wasn't in my code but in the
colordepth of my screen (right click desktop/properties/settings) , if I
set it to truecolors(24 bits) the transparencyKey didn't work, if I set it
to heigh colors (16 bit) it does work, maybe this helps you

greetz Peter

zhang said:
Hi, I also got this problem before, this following VB code may help:)

Public Sub SetBackgroundBitmap(ByVal image As Image, ByVal
transparencyColor As Color)

Me.backgroundImage = New Bitmap(image)
Width = Me.backgroundImage .Width
Height = Me.backgroundImage .Height
Region = BitmapToRegion(Me.backgroundImage, transparencyColor)

End Sub
 
Geoff,

Did you see the message from Peter Proost
Otherwise it works for me.
(Therefore I find this useless this way)

I hope this helps anyway

Cor
 
Hi Cor

With your, and others help (ta Peter), it now works :)

I had to change the display to 16 bit and also re-paint the background of
the image I was using with "White" i.e. I'm not sure it was "true white" in
the initial bitmap.

Great, it now works. HOWEVER, this restriction that it has to be less than
24 bit seems to be less than ideal. I don't know what depth the application
will be on the client machines! Surely there must be an easier way to do
this??? Am I right in assuming that even zhang's idea will not work on a 24
bit for the same reasons?

Geoff
 
Geoff Jones said:
I had to change the display to 16 bit and also re-paint the background of
the image I was using with "White" i.e. I'm not sure it was "true white"
in the initial bitmap.

Great, it now works. HOWEVER, this restriction that it has to be less than
24 bit seems to be less than ideal. I don't know what depth the
application will be on the client machines! Surely there must be an easier
way to do this??? Am I right in assuming that even zhang's idea will not
work on a 24 bit for the same reasons?

If you are using regions, the problems will go away. Regions will work on
any color depth.
 
Geoff copy paste this to see how regions work.

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim myGraphicsPath As Drawing2D.GraphicsPath = New _
Drawing2D.GraphicsPath()
myGraphicsPath.AddEllipse(New Rectangle(0, 0, 125, 125))
myGraphicsPath.AddEllipse(New Rectangle(75, 75, 20, 20))
myGraphicsPath.AddEllipse(New Rectangle(120, 0, 125, 125))
myGraphicsPath.AddEllipse(New Rectangle(145, 75, 20, 20))

Me.BackColor = Color.Chartreuse
Me.Size = New System.Drawing.Size(256, 256)
Me.Region = New Region(myGraphicsPath)

End Sub
 
Hi Herfried

I'm trying to use your ideas for Regions. I have converted the GetRegion
code from C# to VB and have tried, but failed, to get it to do what I want
i.e. I am uncertain where to use the function etc. in the VB code.

I've tried the following:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

TheRegion = GetRegion(Me.BackgroundImage, Color.White)

Me.Region = TheRegion

End Sub

In the hope that it would just paint the image but not the white background.
Unfortunately, I get an overflow error. Can you help?

Geoff
 
I'm guessing that GetRegion merely returns the regions to be painted? Is
this correct?

Geoff

Geoff Jones said:
Hi Herfried

I'm trying to use your ideas for Regions. I have converted the GetRegion
code from C# to VB and have tried, but failed, to get it to do what I want
i.e. I am uncertain where to use the function etc. in the VB code.

I've tried the following:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

TheRegion = GetRegion(Me.BackgroundImage, Color.White)

Me.Region = TheRegion

End Sub

In the hope that it would just paint the image but not the white
background. Unfortunately, I get an overflow error. Can you help?

Geoff
 
Geoff Jones said:
I'm guessing that GetRegion merely returns the regions to be painted? Is
this correct?

A region is used to represent an area of arbitrary shape. The form's
Region' property can be used to set the form's shape. I assume that there
is a bug in your 'GetRegion' function. Can you post your implementation?
 
Thanks for looking at this for me Herfried. Here is my VB version:

Private Function GetRegion(ByRef _img As Bitmap, ByVal color As Color) As
Region

Dim _matchColor As Color = color.FromArgb(color.R, color.G, color.B)

Dim rgn As System.Drawing.Region = New Region

rgn.MakeEmpty()

Dim rc As Rectangle = New Rectangle(0, 0, 0, 0)

Dim inimage As Boolean = False

Dim x As Integer

Dim y As Integer

For y = 0 To _img.Height - 1

For x = 0 To _img.Width - 1

If Not (inimage) Then

If Not color.op_Equality(_img.GetPixel(x, y), _matchColor) Then

inimage = True

rc.X = x

rc.Y = y

rc.Height = 1

Else

inimage = False

rc.Width = x - rc.X

rgn.Union(rc)

End If

Else

inimage = False

rc.Width = _img.Width - rc.X

rgn.Union(rc)

End If

Next

Next

Return rgn

End Function
 
Hi again Herfried

After thinking about it, I'm not sure if the region that the code returns is
the "transparent area" i.e. white background in my case, or the region of
the bitmap I want displayed. Could you enlighten me?

In addition, am I calling this in the correct way i.e. does this next line
tell the application to only draw the region returned by the function?

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

TheRegion = GetRegion(Me.BackgroundImage, Color.White)

Me.Region = TheRegion

End Sub

Note that I'm placing it in the Load member. Is this the correct place to
put it???

Thanks in advance

Geoff
 
Geoff,

Why you dont use the sample I showed you the only thing you have to do with
that is in my opinion to paste it in and than make another region.

So you make me curious.

Cor
 
Cor Ligthert said:
Why you dont use the sample I showed you the only thing you have to do
with that is in my opinion to paste it in and than make another region.

I assume the OP wants to create a region from a bitmap.
 
Back
Top