Looking into opening an image on a blazor page’s @code section, but I keep getting the following error.
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\img\Account\Pamphlet Design.png'
Here is the code that I have tried among other:
var path = Path.Combine(Environment.ContentRootPath, @"/img/Account/Pamphlet Design.png");
var plampletBitmap = new Bitmap(path); // Error Here
>Solution :
I’m not sure that Blazor can access an ASP.NET content root path, maybe in server mode, almost certainly not in web assembly mode.
Normally to access images you can create a /images folder under the Blazor Client’s wwwroot and load the images from there via: images/filename. I just compute the path in the model and bind it directly to an <img src="@Model.ImagePath">
For Blazor server you could try Path.Combine(Environment.ContentRootPath, "wwwroot", "img/Account", "Pamphlet Design.png"); I think even in Blazor server it sandboxes file access, though the leading slash in "/img" might also just be the problem if not.