I creating a small program which basically reads Electronic identity cards and stores the information in a SQL database.
There is more to the program of course, but this is where i need help with.

The EID has a picture from the person also.
I decided not to save the image directly into the SQL database, but store it in the application directory.
However i cannot seem to figure out how i can save it with a filename taken from one or more textboxes.

This is what i am using right now, but as you can see it saves it as Test.Jpeg

        Dim file_name As String = Application.ExecutablePath
        file_name = file_name.Substring(0, _
            file_name.LastIndexOf("\bin")) & "\test."
        Dim bm As Bitmap = EidPic.Image
        bm.Save(file_name & "jpg", _
            System.Drawing.Imaging.ImageFormat.Jpeg)

I am thinking it would be the easiest if i stored the picture with a filename which is less common.
For example the ID card number or social security number, since these are always unique.

Any help would be appreciated.

Thanks

Dani AI

Generated

Short answer: ’s approach (use the EID card number / the textbox value as the filename) is fine and it’s what fixed the immediate problem for . Below are practical, production‑ready refinements and common pitfalls to avoid when saving images with a name from a textbox.

  • Build paths with Path.Combine rather than manual string concatenation so you don’t accidentally mangle separators. Sanitize the textbox value: remove or replace characters returned by Path.GetInvalidFileNameChars before using it as a file name. Ensure the target folder exists (Directory.CreateDirectory) before you call Image.Save. Prefer a writable data folder (e.g., per‑user AppData) instead of writing into Program Files or the app’s install folder. Avoid embedding highly sensitive PII (SSNs) in filenames — store an internal ID or a GUID and map that to the PII in a protected database instead. (learn.microsoft.com)

  • Example VB.NET pattern (keeps the filename safe, makes the folder if needed, and avoids collisions):

' sanitize textbox, choose folder, ensure unique name, save as JPEG
Dim raw = EIDCardNr.Text.Trim()
If String.IsNullOrEmpty(raw) Then raw = Guid.NewGuid().ToString()

Dim invalid = System.IO.Path.GetInvalidFileNameChars()
For Each c As Char In invalid
    raw = raw.Replace(c, "_"c)
Next

Dim storeDir = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApp", "Images")
System.IO.Directory.CreateDirectory(storeDir)

Dim candidate = System.IO.Path.Combine(storeDir, raw & ".jpg")
Dim i As Integer = 1
While System.IO.File.Exists(candidate)
    candidate = System.IO.Path.Combine(storeDir, raw & "_" & i.ToString() & ".jpg")
    i += 1
End While

Try
    Using bmp As Bitmap = CType(EidPic.Image, Bitmap)
        bmp.Save(candidate, System.Drawing.Imaging.ImageFormat.Jpeg)
    End Using
Catch ex As Exception
    ' log or show a meaningful error (permissions, file locked, invalid image, etc.)
End Try
  • Quick troubleshooting notes: Image.Save will throw if you try to overwrite the same source file the image was loaded from — save to a temp name and replace if needed. Watch for UnauthorizedAccessException when writing to protected folders; using AppData avoids that. Always validate and log exceptions so you can see file/permission errors quickly. (learn.microsoft.com)

These steps keep the simple textbox‑named filename idea that suggested, but make it robust, safe, and ready for real‑world deployment.

Recommended Answers

All 4 Replies

Assuming you have the card number stored in a variable named EIDCardNo, just replace your line

file_name.LastIndexOf("\bin")) & "\test."

with

file_name.LastIndexOf("\bin")) & "\" & EIDCardNo & "."

Well once i click the read button it will first collect all data from the card.
And puts it in the textboxes, at this point its still not saved.
So i want it to get the text/numbers from the EIDCard number textbox and use that as name to save the imageonce i go to the next step, which will also save to the database.
So the name is taken from the EIDCardNr.text.

In that case use

file_name.LastIndexOf("\bin")) & "\" & EIDCardNr.Text & "."

Works like a charm, thanks a lot.

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.