Form Background Image Disappears

G

Guest

I am using the following code to get a background image for my form

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBgroundImage.Clic
If OpenFileDialog1.ShowDialog() = DialogResult.OK The
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName
Me.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName
sr.Close(
End I
End Su

This works great. However, this is on frmMain and when I switch to form2 and then return to frmMain the background image is gone. What am I missing here

Thank you
John
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?amNyb3VzZQ==?= said:
I am using the following code to get a background image for my form:

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBgroundImage.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
Me.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
sr.Close()
End If
End Sub

This works great. However, this is on frmMain and when I switch to form2 and then return to frmMain the background image is gone. What am I missing here?

How do you "switch and return"? Are the two forms visible at the same
time or one after the other?
 
K

Ken Tucker [MVP]

Hi,

Why are you creating a stream reader if you are not using it? Why don't
you try something like this?

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal
e
As System.EventArgs) Handles mnuBgroundImage.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
Dim bm as new Bitmap(sr)
sr.Close()
Me.BackgroundImage = bm
End If
End Sub

Ken
-------------
 
G

Guest

Probably because I have no idea what I am doing. When I use your code

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuBgroundImage.Clic

If OpenFileDialog1.ShowDialog() = DialogResult.OK The
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName
Dim bm as new Bitmap(sr
sr.Close(
Me.BackgroundImage = b
End I
End Su

It doesn't like the line "Dim bm as New Bitmap(sr)". It underlines the "sr" and says a lot about can't convert from ... to ..

Any more ideas
Joh
 
G

Guest

I switch from form1 to form2 with this code

Dim frmLC as new frmLabelControl
frmlc.Show(
Me.Hide(

I then switch back with this code

Dim frm1 As New Form
frm1.Show(
Me.Hide(

I also have something very weird going on now. I have a loop created somehow in the declaring of my frm statements. I am totally lost. It errors out with a stack overflow. I'm not even sure where the heck i have to declare things anymore

more to come,
John
 
K

Ken Tucker [MVP]

Hi,

Sorry should have used a filestream instead of a streamreader.

Dim fs As New System.IO.FileStream("C:\Camera.bmp",
IO.FileMode.Open)
Dim bm As New Bitmap(fs)
fs.Close()

Ken
---------------
 
G

Guest

Well Ken...The code seems to work but I keep getting an Out of Memory Exception error. I'll try it in an hour or so when I get home and then post back

John
 
G

Guest

Ken
Here's my code

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBgroundImage.Clic
If OpenFileDialog1.ShowDialog() = DialogResult.OK The
Dim fs As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open
Dim bm As New Bitmap(fs
fs.Close(
Me.BackgroundImage = b
End I
End Su

Any idea why I get an out of memory error

John
 
Y

yEaH rIgHt

Try getting your bitmap like this:

Dim bm as New Bitmap(OpenFileDialog1.FileName)




jcrouse said:
Ken,
Here's my code:

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles mnuBgroundImage.Click
 
Y

yEaH rIgHt

Did you know by using the code below you're actually creating a new instance
of Form1? That's why the image disappears.

I switch from form1 to form2 with this code:

Dim frmLC as new frmLabelControls
frmlc.Show()
Me.Hide()

I then switch back with this code:

'---------Creates a New Form1 WITHOUT the image!
Dim frm1 As New Form1
frm1.Show()
Me.Hide()




jcrouse said:
Ken,
Here's my code:

Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles mnuBgroundImage.Click
 
G

Guest

No workie. What next?

John

yEaH rIgHt said:
Try getting your bitmap like this:

Dim bm as New Bitmap(OpenFileDialog1.FileName)





e As System.EventArgs) Handles mnuBgroundImage.Click
 
Y

yEaH rIgHt

Try these two pieces of code. This should do what you want.


'-----------In Form1
Option Explicit On
Option Strict On

Public Class Form1
Inherits System.Windows.Forms.Form


Dim WithEvents fm As Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If fm Is Nothing OrElse fm.IsDisposed Then
fm = New Form2()
End If
fm.Show()
Me.Hide()
End Sub

Private Sub fm_VisibleChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles fm.VisibleChanged
Me.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim bm As New Bitmap(OpenFileDialog1.FileName)
Me.BackgroundImage = bm
End If
End Sub


Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
fm = Nothing
End Sub


End Class
'-----------End Form1

'-----------In Form2

Option Explicit On
Option Strict On

Public Class Form2
Inherits System.Windows.Forms.Form


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
End Class
 
C

Cor Ligthert

Hi YEaHRight,

It is almost the same code I would have showed for this.

However one thing, why are you doing this?
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
fm = Nothing
End Sub
And not
fm.dispose

A class object should in my opinon never be set to "nothing" in VB.net only
properties.

Cor
 
Y

yEaH rIgHt

It removes the event handler. Actually, I should have done this instead:

Private Sub fm_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles fm.Closing
fm = Nothing
End Sub

And not the Form1.Closing.
 
G

Guest

Worked great!. I had to add some logic incase the users didn't enter frm2 because then there was nothing to dispose.

Thanks guys,
John
 
C

Cor Ligthert

Private Sub fm_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles fm.Closing
fm = Nothing
End Sub

If it is right, that should do nothing, you are telling that there is no
reference anymore to fm so you cannot use it anymore, however it still
exist.

What needs to be done is the clean up from the unmanaged resources which
seems to be in a dialogbox, so after a frm.showdialog, there should as well
always be a frm.dispose.

(I am telling only what I have read, not what I have tested in this)

Cor
 
Y

yEaH rIgHt

Read Page 255 in Applied Microsoft .Net Framework Programming in Microsoft
Visual Basic .Net. By Jeffery Richter and Francesco Balena.
 
C

Cor Ligthert

Hi yEaHricht,

Intresting that it is in a book.

However, do it as you wish, I tried only to make you attent on it.

Cor
 
G

Guest

The following code works great:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim bm As New Bitmap(OpenFileDialog1.FileName)
Me.BackgroundImage = bm
End If
End Sub

Now, how do i remove the background Image or set it to null or nothing?

Thanks,
John
 

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