how to load image with relative path in bitmap

∥☆過路亽.° 提交于 2019-12-08 19:51:35

问题


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.


回答1:


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"));



回答2:


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"));



回答3:


You can use server.MapPath, pass Url string as given below.

 Server.MapPath("../images/image.gif")


来源:https://stackoverflow.com/questions/7773660/how-to-load-image-with-relative-path-in-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!