레이블이 WPF인 게시물을 표시합니다. 모든 게시물 표시
레이블이 WPF인 게시물을 표시합니다. 모든 게시물 표시

WPF Relative Image Path Problem

While I was diving into developing my application, ThePen, I encountered a significant issue with loading images using relative paths in both XAML and code-behind. I suggest a solution for this issue. I cannot guarantee that it will work for you, but I hope it helps. The processes that I couldn't easily resolve, and that you might easily disregard, will be highlighted in red.


In case of XAML, if you want to show static images (not changed in runtime).

1. Place your image on your project directory. The image should truly exist, and also add your project. You can make folder and place them in there.

2. Normally add your image element like:

<Image Source="/Images/pen.png"/>


3. Double click the project name in Solution Explorer, you will see a xml document for project settiong . add following in the middle of the xml:

<ItemGroup>

<Resource Include="Images/pen.png" />

                   .... (add more images as you want)

</ItemGroup>

I should add all images like this and it worked fine.


In case of code behind, if you want to show dynamic images (changed in runtime).

1. Place your image next to ***.exe file. It may be placed in

SolutionDir/PorjectDir/bin/Debug/TargetFramework/***.exe


2. Your code should be like:

var bitmapImage = new BitmapImage();

bitmapImage.BeginInit();

bitmapImage.UriSource = new Uri("pen.png", UriKind.RelativeOrAbsolute);

bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

bitmapImage.EndInit();

MyImage.Source = bitmapImage;



I spent hours to find these solution...... ah.

Read More

WPF .Net 5 이미지 보이지 않을 때 (리소스 추가)

닷넷 프레임워크에서는 프로젝트에 이미지를 포함시키면 자동으로 리소스에 추가가 됐다. 그런데 닷넷 5로 넘어오면서 이미지 리소스를 직접 추가해줘야 한다.



<ItemGroup>

<Resource Include="Images/hat.png" />

<Resource Include="Images/arrow.png" />

<Resource Include="Images/pen.png" />

<Resource Include="Images/eraser.png" />

<Resource Include="Images/clear.png" />

<Resource Include="Images/overlay.png" />

<Resource Include="Images/setting.png" />

<Resource Include="Images/exit.png" />

<Resource Include="Images/info.png" />

</ItemGroup>


솔루션 익스플로러에서 프로젝트를 더블클릭하면 xml 문서가 보인다.

여기에 위와 같이 추가하면 된다.


그래도 안 보이면 솔루션을 다시 빌드한다. 상단메뉴 [빌드] -> [솔루션 정리] 후 다시 실행.



그리고 코드 비하인드에서 동적으로 상대경로 이미지가 나타나지 않을때, https://ladofa.blogspot.com/2024/03/wpf-relative-image-path-problem.html 여기 참조.



Read More
Powered by Blogger.