Can I save the information from common dialog
Save as Jpg or Bmp
format to an INI file and retrive it when the program starts.

Now I have this line

Image4.Picture = LoadPicture(App.Path & "\test.bmp")

to load the picture.
and it's not good.

I would prefer to have that the ini file
loading the last saved picture to
image4.picture.
and how to do it?

please

Dani AI

Generated

Good approach: persist the full file path that the Save dialog returns and restore it at startup. A small, robust pattern is: (1) save the chosen full path to a tiny settings file placed under the user AppData folder (not the program folder), (2) optionally copy the image into that app folder so it cannot be moved or deleted by the user, and (3) read that settings file during form initialization and only call LoadPicture when the saved file actually exists. This avoids permission problems when the EXE is in Program Files and avoids brittle relative-path issues.

Example (VB6-style) — write the path, then read it on startup:

' Persist the full filename (call after the Save dialog completes)
Sub SaveLastImagePath(sFullPath As String)
    Dim sDir As String: sDir = Environ$("APPDATA") & "\MyVBApp"
    If Dir$(sDir, vbDirectory) = "" Then MkDir sDir
    Dim sCfg As String: sCfg = sDir & "\lastimage.txt"
    Open sCfg For Output As #1
    Print #1, sFullPath
    Close #1
End Sub

' Read the saved path (call from Form_Load) and load into an Image control
Sub LoadLastImage(imgControl As Image)
    Dim sCfg As String: sCfg = Environ$("APPDATA") & "\MyVBApp\lastimage.txt"
    Dim sPath As String
    If Dir$(sCfg) = "" Then Exit Sub
    Open sCfg For Input As #1
    Line Input #1, sPath
    Close #1
    If sPath <> "" And Dir$(sPath) <> "" Then
        On Error Resume Next
        Set imgControl.Picture = LoadPicture(sPath)
        On Error GoTo 0
    End If
End Sub

Troubleshooting and notes (addresses points raised by and , and the "doesn't load at startup" symptom from ):

  • Ensure the saved string is the full path including extension, not just a filename or a header line; if a header was written (e.g. a section label) parse it out before using it.
  • Read the file from Form_Load (or after the form and control exist). Reading too early can fail silently.
  • Verify the file exists with Dir$ before calling LoadPicture.
  • On modern Windows avoid writing to App.Path — use Environ$("APPDATA") or registry (SaveSetting/GetSetting) or the INI APIs for multiple keys.
  • If the image still won't load, check for stray punctuation/extra characters in the saved line (common cause of failures).

This pattern is lightweight, avoids extra dependencies, and makes the startup load deterministic.

Recommended Answers

All 8 Replies

I have not tested this.. but this should work..

Dim ojbIni As New FileSystemObject
Dim IniStream As TextStream

If ojbIni.FileExists(App.Path & "\inifilename.ini") Then    'checking if ini file is present on the root of your vb project, if not it will automatically create
    Set IniStream = ojbIni.OpenTextFile(App.Path & "\inifilename.ini", ForWriting, False, TristateUseDefault) 'for writing ini to the file
Else
    Set IniStream = ojbIni.CreateTextFile(App.Path & "\inifilename.ini", True) 'for creating ini file
End If

'Write on the first 2 lines of the .ini file (first line is header, second is your picture path
IniStream.WriteLine "[Last_Browse_Picture]" 'the parent header of the .ini file
IniStream.WriteLine commondialog.filename   'your commondialog filename

IniStream.Close
Set IniStream = Nothing

You must enable "Microsoft Scripting Runtime" in reference..

Hi

I will test it and replay to you how it work's

Blocker

It work's just fine
but how can I retrive
the info from Ini file to

Image4 = app.path & "\inifilename.ini"
is this the right way

You need to read lines of your ini stream just like this (not tested yet):

Dim ojbIni As New FileSystemObject
Dim IniStream As TextStream
Dim filenamex As String

If ojbIni.FileExists(App.Path & "\inifilename.ini") Then
    Set IniStream = ojbIni.OpenTextFile(App.Path & "\inifilename.ini", ForReading, False, TristateUseDefault)  'for reading ini to the file

    While Not IniStream.AtEndOfStream
        filenamex = ""
        filenamex = IniStream.ReadLine
    Wend

   Picture1.Picture = LoadPicture(filenamex)
End If

This code assumes that the last line of your .ini stream is the filename.

Hi

If this is a true ini file that could contain more than one value or section then you should consider using the API's that were created for managing ini files such as GetPrivateProfileString and WritePrivateProfileString.

Check out this tutorial on for more information and examples.

HTH

Blocker

It don't load at start up.
the inifile is correct

how can we know...? show your code...

I missed a punktation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.