V
V
Vyacheslav Shabunin2021-09-07 19:52:58
WPF
Vyacheslav Shabunin, 2021-09-07 19:52:58

How to properly set Bitmap.UriSource to Image.Source?

Hello!
You need to set an image for Image.Source.
I do it like this:

Image img = new();

string path = Directory.GetCurrentDirectory();
path = path + "\\.(тут остальной путь).\\image.png";

BirmapImage bitmapImg = new();
bitmapImg.UriSource = new Uri(path);

img.Source = bitmapImg;

But when debugging UriSource shows - null.
I know that Uri accepts local files as initial path: "://".
But I need the path to refer to an already compiled directory. Tell me how can I do this please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-09-07
@vabka

1. UriSource accepts a path relative to the working directory, not an absolute path
2. The separator is a normal slash (/), not a backslash (\)

But I need the path to refer to an already compiled directory

Just set it in the properties for the picture so that it is copied to the compiled directory.
https://docs.microsoft.com/en-us/dotnet/api/system...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question