Hi
I have an image converted to binay and saved in SQL database.
Below is my line of code where application is throwing above exception that im using for showing image

Dim mem As New IO.MemoryStream(CType(io.File.ReadAllBytes(imgfilepath), Byte()))

Does anybody knows how can i fix this?
Thanks and regards,
f

Dani AI

Generated

Quick addendum based on the thread: the exception "Could not find a part of the path ..." is the runtime throwing a System.IO.DirectoryNotFoundException. As pointed out, an incorrect filename often causes this (and confirmed that was the case). Below are practical checks and safer patterns to avoid (or quickly find) this error in future.

Checklist and debugging steps

  • Immediately log the actual string you pass to IO before opening the file. That shows missing drive letters, stray whitespace, or wrong folders.
  • Normalize and validate the path: trim it, call IO.Path.GetFullPath to resolve relative names, and check IO.File.Exists before trying to open it.
  • In web apps use Server.MapPath to convert virtual paths to physical paths and ensure the account running the app has read permission.
  • Watch for invalid path characters, very long paths, or UNC/share permissions. Wrap file access in try/catch and log ex.Message plus the path.

Safer patterns

  • Verify the path first and fail with a clear log message rather than letting the low-level exception bubble up:
Dim path As String = If(imgFilePath Is Nothing, String.Empty, imgFilePath).Trim()
If String.IsNullOrEmpty(path) Then
    ' log: empty path
Else
    Dim fullPath As String = IO.Path.GetFullPath(path)
    If IO.File.Exists(fullPath) Then
        ' safe to open
    Else
        ' log: missing file fullPath
    End If
End If
  • If the image is already stored as varbinary in SQL (as mentioned), read the bytes from the DB and use a MemoryStream directly to avoid filesystem dependency:
Dim imgBytes() As Byte = CType(reader("ImageColumn"), Byte())
Using ms As New IO.MemoryStream(imgBytes)
    Dim img = System.Drawing.Image.FromStream(ms)
    ' use image
End Using

Useful references: DirectoryNotFoundException, File.Exists, Path.GetFullPath, Server.MapPath.

Recommended Answers

All 3 Replies

Hi

You can compare the Path Stored in DB with physical path of the file along with their file extension eg .bmp/.jpeg etc...

If the variable 'imgfilepath' has a invalid path or file name, it will throw
System.IO.DirectoryNotFoundException with error message 'Could not find a part of the path .......'.

Otherwise post the complete exception message. It will help to identity the exact issue.

Actually filename was incorrect .
Thanks Ramesh for pointing out.

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.