changing background image during runtime
ok, so i have a program i've made a while, and one of the users wants to be able to load his own images to the background instead of the plain grey that is there now. i've searched and searched, and basically can get this working
Code:
BackgroundImage = Image.FromFile("C:\Users\Me\FileName.png")
i just slapped that onto a button just to get it to work as i was experimenting. but that only works for a file of that name, on my computer, in that folder. what i'm looking to do is get it so a user can browse his own directories for images and upload them as the background on the form. and be able to save the settings as well. but so far i have only seen different ways to load preset images that you can load. if it's even possible? i'd assume that you'd use the openfiledialog command, but i'm not that well versed in vb as of yet. i may need to search more
thanks for any help :)
Re: changing background image during runtime
All you need is a String containing the file path. If you want the user to select a file path then you use an OpenFileDialog. There are lots of examples around. You can store the path in My.Settings, via the Settings page of the project properties.
Re: changing background image during runtime
vb.net Code:
ofd.ShowDialog()
Dim bgimage As String = ofd.FileName.ToString()
Me.BackgroundImage = New System.Drawing.Bitmap(bgimage)
ofd = OpenFileDialog
You would want to also set the settings for the ofd such as title, filter, initial directory, ect.
As jmcilhinney said, you would simply create a setting in the project's properties and save that setting after the user selects the image or on form close.
vb.net Code:
My.Settings.setBGImage = bgimage
My.Settings.Save()
With that code the setting that I created is named setBGImage
This would be an example of a form load event:
vb.net Code:
If My.Settings.setBGImage = "" Then
'
Else
Me.BackgroundImage = New System.Drawing.Bitmap(My.Settings.setBGImage)
End If
Hope that helps!
Re: changing background image during runtime
awesome! that's exactly what i was looking for. i only took one semester of vb, and that was like a year or longer ago and it was an online class haha so i don't know much after functions and arrays. still trying to get into my second semester of it.
but thanks a lot. got it to load and save just fine. now my other issue that arises, which isn't that major, but when i open the program with the custom image, all my textboxes and labels are blank white, then after a couple moments they all start to pop up the way they should. kinda lagging behind after i open it. there any way to speed this or anything?
Re: changing background image during runtime
or also, if the background image is deleted, it prevents the program from running.
Re: changing background image during runtime
Quote:
Originally Posted by
Bull
or also, if the background image is deleted, it prevents the program from running.
Only if you just use it without checking first, so check first.
Re: changing background image during runtime
yeah, guess i just expected it to save it in the program for whatever reason.
but any way to speed the lag problem up? i can't figure that one out
Re: changing background image during runtime
That can depend on several things...
processor speed
graphics processing speed
image size
ect.
You could look into pre-loading the image when the application initializes. I don't have VS installed on this computer or I'd tell you the exact event for that...
As for the image being deleted, instead of using a link to the image you could copy the image to the Resources of the solution and use it from there. If it is a server based program, copy it to a different directory where it won't be deleted and use that link.
Re: changing background image during runtime
i was hoping to have it so the user could load any custom image he/she may want instead of picking from predetermined images. but...we'll see i guess. does running from the .exe make a difference? i don't even know how to publish a program :blush:. i've just been uploading up the .exe and have people download and run right off of that. i've tried to publish it, but it's either online or to a server (which i can't do), or on a cd/dvd. i'd want it in a package,upload that, then the user downloads and installs and uses.
sorry for my extreme noobness :blush:. i'm going to look into preloading the images like you mentioned