You need to look at the value in
imagePath
- it should contain a valid absolute URI.
If you look at MSDN:
Constructor: URI(string)[
^] it lists all the reasons it can throw the exception:
uriString is empty.
-or-
The scheme specified in uriString is not correctly formed. See CheckSchemeName.
-or-
uriString contains too many slashes.
-or-
The password specified in uriString is not valid.
-or-
The host name specified in uriString is not valid.
-or-
The file name specified in uriString is not valid.
-or-
The user name specified in uriString is not valid.
-or-
The host or authority name specified in uriString cannot be terminated by backslashes.
-or-
The port number specified in uriString is not valid or cannot be parsed.
-or-
The length of uriString exceeds 65519 characters.
-or-
The length of the scheme specified in uriString exceeds 1023 characters.
-or-
There is an invalid character sequence in uriString.
-or-
The MS-DOS path specified in uriString must start with c:\\.
Without access to the variable content, we can't tell you which is causing the problem. Either use the debugger to put a breakpoint on the line:
var bitmap = new BitmapImage(new Uri(imagePath)) ;
And manually examine the content, or log the content to a file before you execute the line and look at it later.