Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load image with relative path in bitmap

i want to upload image int the bitmap object from asp.net, the image is location under

/uploadedimages/sampleimage.jpg

whenever i use below code to load image in bitmap, i gets error saying Parameter not valid.

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

i tried to replace the slashes in path to "\" still that does not work.

can anyone tell me what could be the reason for the error and the possible resolution.

like image 363
Abbas Avatar asked Oct 14 '11 21:10

Abbas


2 Answers

Use Server.MapPath. And it's a good practice to use the tilde char ~ to specify the web application root.

Bitmap b = new Bitmap(Server.MapPath("~/uploadedimages/sampleimage.jpg"));
like image 103
Oleg Grishko Avatar answered Oct 22 '22 17:10

Oleg Grishko


if uploadedimages directory is in your App_Data folder then you should append the App_Data absolute path to your path:

Bitmap b = new Bitmap(Path.Combine(Server.MapPath("~/App_Data"), "/uploadedimages/sampleimage.jpg"));
like image 24
fardjad Avatar answered Oct 22 '22 19:10

fardjad