How to change\set background image of a button in C# WPF code? -
i'm trying change background image of button other image , encountered errors. code have on xaml:
<button x:name="button1" width="200" height="200" content="button1" margin="0,0,0,400"> <button.background> <imagebrush **imagesource ="images/aero.png"** ></imagebrush> </button.background> </button>
and cs:
private void button1_click_1(object sender, routedeventargs e) { var brush = new imagebrush(); brush.imagesource = new bitmapimage(new uri("images/aero.png")); button1.background = brush; }
the error have on xaml " file 'images\logo.png' not part of project or 'build action' property not set 'resource'. can me explain, thanks
in build action, can mark image file content or resource. syntax use image in imagebrush different depending on 1 choose.
here image file marked content.
to set button background image use following code.
var brush = new imagebrush(); brush.imagesource = new bitmapimage(new uri("images/contentimage.png",urikind.relative)); button1.background = brush;
here image file marked resource.
to set button background resource image use following code.
uri resourceuri = new uri("images/resourceimage.png", urikind.relative); streamresourceinfo streaminfo = application.getresourcestream(resourceuri); bitmapframe temp = bitmapframe.create(streaminfo.stream); var brush = new imagebrush(); brush.imagesource = temp; button1.background = brush;
Comments
Post a Comment