c# - Loading image from resource to image control -


i've got folder structure images in i'm trying load , change in background of code using c# (not xaml).

i'm having no luck , either getting exceptions thrown or preloaded image going invisible when should change. have tried code samples various questions on here i'm still having no luck.

folder structure -> resources/images/themes/{mytheme}.png build action - > resource (all have been added resource resources.resx too)

the code have is..

var themeimage = new bitmapimage(); var filename = string.format("{0}{1}.png", cmbbasethemes.selectedvalue.tostring(), cmbaccentcolors.selectedvalue.tostring()); themeimage.begininit(); themeimage.urisource = new uri(@"/zapp;component/resources/images/themes/" + filename, urikind.relativeorabsolute); themeimage.endinit(); imgthemestyle.source = themeimage; 

but code gives me exception "cannot locate resource 'resources/images/themes/lightindigo.png'."

in code behind you'll have use qualified resource file pack uri

themeimage.urisource = new uri(     "pack://application:,,,/zapp;component/resources/images/themes/" + filename); 

you may avoid begininit/endinit calls using bitmapimage constructor takes uri argument:

imgthemestyle.source = new bitmapimage(new uri(     "pack://application:,,,/zapp;component/resources/images/themes/" + filename)); 

Comments