How to load image with relative path in bitmap

I want to load an int image into a raster object from asp.net, the image is in

/uploadedimages/sampleimage.jpg 

whenever I use the code below to load an image into a bitmap, I get an "Parameter is invalid" error message.

 Bitmap b = new Bitmap("/uploadedimages/sampleimage.jpg") // this path is coming from database holded in variable 

I tried replacing the slashes in the path with "\", but this does not work.

can someone tell me what could be causing the error and possible resolution.

+6
source share
3 answers

If the uploadedimages directory is in your App_Data folder, then you should add the absolute App_Data path to your path:

 Bitmap b = new Bitmap(Path.Combine(Server.MapPath("~/App_Data"), "/uploadedimages/sampleimage.jpg")); 
+2
source

Use Server.MapPath . And it is good practice to use char ~ tilde to indicate the root of the web application.

 Bitmap b = new Bitmap(Server.MapPath("~/uploadedimages/sampleimage.jpg")); 
+7
source

You can use server.MapPath to pass the Url string as below.

  Server.MapPath("../images/image.gif") 
+2
source

Source: https://habr.com/ru/post/899330/


All Articles