changing background image during runtime-VBForums
Results 1 to 9 of 9

Thread: changing background image during runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    39

    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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    May 2008
    Posts
    240

    Re: changing background image during runtime

    vb.net Code:
    1. ofd.ShowDialog()
    2. Dim bgimage As String = ofd.FileName.ToString()
    3. 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:
    1. My.Settings.setBGImage = bgimage
    2. 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:
    1. If My.Settings.setBGImage = "" Then
    2. '
    3. Else
    4. Me.BackgroundImage = New System.Drawing.Bitmap(My.Settings.setBGImage)
    5. End If



    Hope that helps!

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    39

    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?

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    39

    Re: changing background image during runtime

    or also, if the background image is deleted, it prevents the program from running.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    Re: changing background image during runtime

    Quote Originally Posted by Bull View Post
    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    39

    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

  8. #8
    Addicted Member
    Join Date
    May 2008
    Posts
    240

    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    39

    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 . 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 . i'm going to look into preloading the images like you mentioned

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.