Q: GIF image on a form

G

G .Net

Hi

I have a form on which I have a picture box. I have placed a GIF as the
image for this picture box. The GIF has a transparent background.

If I change the background color of the form, then the GIF appears on the
form correctly i.e. the background of the form shows through the transparent
part of the gif.

However, if I place a BackgroundImage on the form, the transparent part of
the form does not show the BackgroundImage of the form, rather is shows the
back color of the form. This obviously make the GIF not to be "transparent".

Can anybody help?

Thanks

G
 
F

Fred Hedges

What you need to do is make a user control and then modify it's transparent
property on window construction. You then need to handle "InvalidateEx"
messages in order to force the parent to redraw your background, rather than
you. Funny you mention this because I had to do this with a "working....."
animation yesterday.

This sample is in C# but it's easy to convert. You won't be needing a
picture box. Just use DrawImage to render your GIF on the control surface
but overriding the paint event.

http://www.devnewsgroups.net/link.a...g/dotnet/faqs/?id=transparentcontrol&lang=en>
 
G

G .Net

Hi Fred

Ah, looks like exactly what I need!!!

However, bit of newbie so need help in exactly what to do. Could I trouble
you to give the steps I need?

G
 
F

Fred Hedges

(1) Create a new User Control

(2) Override it's CreateParms property with this:

Protected Overrides ReadOnly Property CreateParams() As CreateParams

Get

Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT

Return cp

End Get

End Property

(3) In Sub New(), add the following code:


Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.UpdateStyles()
Me.BackColor = Color.Transparent


(4) Override the OnPaintBackground event, but don't put anything into it:

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
'
End Sub


(5) Import the GIF into your resources (Project Properties, Resources,
Images, Add Image...).

This will basically give you a transparent surface. Now you can draw your
..GIF onto this transparent surface any way you want, eg:

(6) Override the OnPaint event and use it to draw your image:
Protected Overrides Sub OnPaint(byval e As PaintEventArgs)

' Render our image here, no stretch, modify to stretch to fill
' the control if required.
e.Graphics.DrawImage ( My.Resources.WhateverMyGIFImageIsCalled, 0,
0 )

End Sub



Hope this helps.
 
G

gs

that looks more like VB than c#
I think you will have to fresh out the last stub for OnPaintBackground
with painting you GB image
Imports System
Imports System.Windows.Forms

Public Class TransparentControl
Inherits Control

Private Const WS_EX_TRANSPARENT As Int32 = &H20

Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.UpdateStyles()
Me.BackColor = Color.Transparent
End Sub

Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
Return cp
End Get
End Property

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
' fresh out this partto pain the gb image
End Sub


a primitive example is from the vb help
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs)
' Create a local version of the graphics object for the PictureBox.
Dim g As Graphics = e.Graphics

' Draw a string on the PictureBox.
g.DrawString("This is a diagonal line drawn on the control", _
New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
' Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, _
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
End Sub 'pictureBox1_Paint



but then if you already the GB image in some format (tif, bmp,) you may be
better off looking for some picture control to take care of that for you.


Hey, I am newbie to vb too. so I can help very much beyon what I can find by
goole or built help.

you may also want to look at the game snippets or starter kit/template. I
am sure there are plenty of examples for handling graphics
 
G

G .Net

Many thanks Fred for your comprehensive help.

G

Fred Hedges said:
(1) Create a new User Control

(2) Override it's CreateParms property with this:

Protected Overrides ReadOnly Property CreateParams() As CreateParams

Get

Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT

Return cp

End Get

End Property

(3) In Sub New(), add the following code:


Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.UpdateStyles()
Me.BackColor = Color.Transparent


(4) Override the OnPaintBackground event, but don't put anything into it:

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
'
End Sub


(5) Import the GIF into your resources (Project Properties, Resources,
Images, Add Image...).

This will basically give you a transparent surface. Now you can draw your
.GIF onto this transparent surface any way you want, eg:

(6) Override the OnPaint event and use it to draw your image:
Protected Overrides Sub OnPaint(byval e As PaintEventArgs)

' Render our image here, no stretch, modify to stretch to fill
' the control if required.
e.Graphics.DrawImage ( My.Resources.WhateverMyGIFImageIsCalled, 0,
0 )

End Sub



Hope this helps.
 
G

G .Net

Thanks gs.

G

gs said:
that looks more like VB than c#
I think you will have to fresh out the last stub for OnPaintBackground
with painting you GB image
Imports System
Imports System.Windows.Forms

Public Class TransparentControl
Inherits Control

Private Const WS_EX_TRANSPARENT As Int32 = &H20

Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.UpdateStyles()
Me.BackColor = Color.Transparent
End Sub

Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
Return cp
End Get
End Property

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
' fresh out this partto pain the gb image
End Sub


a primitive example is from the vb help
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs)
' Create a local version of the graphics object for the PictureBox.
Dim g As Graphics = e.Graphics

' Draw a string on the PictureBox.
g.DrawString("This is a diagonal line drawn on the control", _
New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
' Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, _
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
End Sub 'pictureBox1_Paint



but then if you already the GB image in some format (tif, bmp,) you may be
better off looking for some picture control to take care of that for you.


Hey, I am newbie to vb too. so I can help very much beyon what I can find
by goole or built help.

you may also want to look at the game snippets or starter kit/template. I
am sure there are plenty of examples for handling graphics
 
F

Fred Hedges

Yes thats correct. I couldn't find the C# version I used the other day, so
pasted the first VB version I came across, but did not edit the message to
take account of this. Actually, it turns out that this one is better.
 

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

Similar Threads


Top