c# - WPF button using System.Windows.Forms.ImageList -


is there nice way following. wpf button , windows.forms.imagelist , use them together. here code:

    <button name="something" content="button" height="23" verticalalignment="top" width="75"/>      system.windows.forms.imagelist imglist = new system.windows.forms.imagelist();      string str_directory = system.io.path.getdirectoryname(system.io.path.getdirectoryname(system.io.directory.getcurrentdirectory()));     this.imglist.images.add(system.drawing.image.fromfile(str_directory + "\\resources\\aboutimage.png"));     wpfbutton.background = imglist.images[0]; 

i tyring windows.forms imagelist , use on wpf button. there nice way fix it?

there no need imagelist. shown in code below, assumes path "resources\aboutimage.png" relative application's current working directory.

apparently you've called path.getdirectoryname 2 times cut off "bin\debug\" or "bin\release\" part of path , access image file directly visual studio project structure. not work when application deployed somewhere else. instead, set build action of image file content, , copy output directory copy always or copy if newer.

var path = path.combine(directory.getcurrentdirectory(), "resources", "aboutimage.png"); var bitmap = new bitmapimage(new uri(path)); wpfbutton.background = new imagebrush(bitmap); 

however, far better approach load image resource directly. set build action resource , load image wpf pack uri:

var bitmap = new bitmapimage(     new uri("pack://application:,,,/resources/aboutimage.png")); wpfbutton.background = new imagebrush(bitmap); 

besides that, set background in xaml, like:

<button ...>     <button.background>         <imagebrush imagesource="/resources/aboutimage.png"/>     </button.background> </button> 

Comments