How to eliminate Tiling of windows forms background images

G

Guest

I want to place a background image on a windows form. Is there a way to
prevent the image from Tiling without using an image box resized to the size
of the form?
I am using Visual Studio 2003 .NET. This is an easy thing to do in VB6.
 
K

Kevin Yu [MSFT]

Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need the background image size to
be the same as client rectangle by stretching instead of tiling. If there
is any misunderstanding, please feel free to let me know.

As far as I know, this cannot be implemented so simply as in VB6. We can
try to override the OnPaint event to achieve this. When painting, we just
use Graphics.DrawImage to draw the particular bitmap to the client area.
Here is an example:

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Bitmap b = new Bitmap(@"D:\My Documents\My Pictures\untitled.bmp");
e.Graphics.DrawImage(b,
this.ClientRectangle.X,this.ClientRectangle.Y,this.ClientRectangle.Width,
this.ClientRectangle.Height);
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Hi,
Thank you for your reply. Yes, you have a complete understanding of my
problem. I am a visual basic programmer and will have to study your C# code
closely to see if I can glean a vb method to accomplish importing my image
file using the OnPaint event. Thank you again for your response. Are you able
to provide an example of this procedure in visual basic code? Thanks again.
 
K

Kevin Yu [MSFT]

Of course, here is the VB.NET code.

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim b As New System.Drawing.Bitmap("D:\My Documents\My
Pictures\untitled.bmp")
e.Graphics.DrawImage(b, Me.ClientRectangle.X, Me.ClientRectangle.Y,
Me.ClientRectangle.Width, Me.ClientRectangle.Height)
End Sub

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

Jay B. Harlow [MVP - Outlook]

Ron,
In addition to (instead of) handling the Paint event as Kevin showed, I
would consider overriding the OnPaintBackground method, as the
OnPaintBackground is the method that paints the background.

Something like:

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
If Me.BackgroundImage Is Nothing Then Exit Sub
e.Graphics.DrawImage(Me.BackgroundImage, Me.ClientRectangle)
End Sub

NOTE: You do not want (or need) to call MyBase.OnPaintBackground as you are
painting the entire background.

http://msdn.microsoft.com/library/d...wsformscontrolclassonpaintbackgroundtopic.asp

The following articles discusses the OnPaintBackground method:
http://msdn.microsoft.com/library/d...de/html/cpconrenderingwindowsformscontrol.asp

Hope this helps
Jay
 
G

Guest

Please excuse my stupidity but I upgraded my vb6 program to

VS .NET 2003 and in my VB6 application, I stored the Form1

Background_Picture in my applications emts.ini file. During

the Form1 load event I use the following procedure to get

the backgroung picture location and file name:

In my VB6 BAS Module I have defined pix globally:

Public pix as string

'Procedure to Get the Form1.Picture from emts.ini

pix = VBGetPrivateProfileString("Background_Picture",

"Path", "c:\Program Files\Holmes

Industries\Megatech32\emts.ini")

Next, I used the following procedure to load the file as

the Form1 background:

Form1.DefInstance.BackgroundImage = System.Drawing.Image.FromFile(pix)

This caused the objectionable Tileing that I was

describing.

I placed the code that you so kindly sent me, into my

form1 Paint event and replaced the "D:\My Documents\My

Pictures\untitled.bmp" with the global value pix as shown

here:

Dim b As New System.Drawing.Bitmap("D:\My Documents\My

Pictures\untitled.bmp") AS

Dim b As New System.Drawing.Bitmap(pix)

and now I can't figure out how to call the Form1_Paint

procedure as the call wants an event as sender ( the e

variable in your code ).

e.Graphics.DrawImage(b, Me.ClientRectangle.X, Me.ClientRectangle.Y,
Me.ClientRectangle.Width, Me.ClientRectangle.Height)

I would like the background to load automatically during the form load
procedure but without the Tiling?????

Thank you for all of your help.
After 10 years of using VB3, VB4, VB5 and recently VB6, I never knew how
little I know about programming until I tried using VS .NET 2003. You are
suggestion I consider overriding the OnPaintBackground method and I am
realizing that I don't know how to do this. I know I am asking too much but
this is very confusing. I wrote a very involved program to track equipment
maintenance problems. My VB6 application's executible is over 6 MB in size
and really works quite well and I never heard of any of this. What should I
do next after the form1_Load code shown below?

'Procedure to Get the Form1.Picture from emts.ini

pix = VBGetPrivateProfileString("Background_Picture", "Path", "c:\Program
Files\Holmes Industries\Megatech32\emts.ini")

Next, I used the following procedure to load the file as the Form1 background:

Form1.DefInstance.BackgroundImage = System.Drawing.Image.FromFile(pix)

This is when the background Tiling appeared.

I think the RAD (Rapid Application Development) name is wishful thinking. At
least until you learn the 10 thousand other things you never knew about,
while using the former languages. I admire you guys who have attained the MVP
rating, and I know you know what I mean when I say this new language is
confusing. Thank you for replying to my cry for HELP!!!
Do I Paste the code below in the Form1_Paint event?

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
If Me.BackgroundImage Is Nothing Then Exit Sub
e.Graphics.DrawImage(Me.BackgroundImage, Me.ClientRectangle)
End Sub
If I do paste this code... where does the picture file information go?
 
J

Jay B. Harlow [MVP - Outlook]

Ron,
the Form1 background:

Form1.DefInstance.BackgroundImage = System.Drawing.Image.FromFile(pix)

I would set BackgroundImage in the Load event with the following:
Me.BackgroundImage = System.Drawing.Image.FromFile(pix)

Simply to ensure that all instances of the form have the property set.

and now I can't figure out how to call the Form1_Paint
procedure as the call wants an event as sender ( the e
variable in your code ).
You DO NOT call it as its a form level event, the form itself will call it
when it needs to be called.

VB (.NET, 1, 2, 3, 4, 5, 6) 101. Paint is an event handler for the form
itself, you need to select the Paint event from combo boxes at the top of
the editor to add a handler for the Paint event. Which will add a routine
such as:

Private Sub MainForm_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Kevin's code
End Sub

Alternatively you can override the OnPaint method, as the OnPaint method
raises the Paint event (this should have been explained in the link I gave).

' you can (and should) use the following instead of the above
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e) ' important this needs to be called!
' Kevin's code
End Sub

OnPaintBackground is only an overriden method.
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
If Me.BackgroundImage Is Nothing Then Exit Sub
e.Graphics.DrawImage(Me.BackgroundImage, Me.ClientRectangle)
End Sub

The paint event (Paint or OnPaint) & OnPaintBackground are automatically
called by the form itself when the form needs to be painted (when you or
windows calls Form.Update either directly or indirectly). This should also
have been explained in the link I gave.

You should only use Paint event or OnPaintBackground routine, not both.

Again the link I gave should explain when to use on or the other.

Hope this helps
Jay
 
G

Guest

Thanks Kevin and Jay,
I finally get it. After reading the articles that Jay refered me to, It all
makes sense.
Kevins code makes sense as well as the code supplied by Jay.
You both are extremely knowledgeable.
When I become more knowledgeable, I think I will also share my hard learned
skills with others as you both, and others, are doing. I will take a lesson
from Jay, and direct the programmer to MSDN articles that describe in debth,
the principles involved. It brings me to mind, the old saying "You can give a
man a fish and feed him for a day, or you can teach him to fish and feed him
forever". (or something to that efect) smile...

Thanks guys.
 
K

Kevin Yu [MSFT]

You're welcome. Thanks for sharing your experience with all the people
here. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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