c# - Use user selected image as canvas background -
deleted old question make more specific one. using code http://www.c-sharpcorner.com/uploadfile/mahesh/image-viewer-in-wpf/ basis. lets user browse image file open , display. want display image , let user make marks on it. decided want use canvas this. right now, can't figure out how user selected image background. i'm getting error says "system.windows.shapes.path not contain definition 'background' , no extension method 'background' accepting first argument of type 'system.windows.shapes.path' found..." line says 'canvas1.background = brush;". i've looked ways set background of canvas, involving using xaml code, other errors.
xaml:
<window x:class="canvasstuff.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="main window" height="409" width="574"> <grid > <label content="image" height="32" horizontalalignment="left" margin="11,10,0,0" name="selectedfilename" verticalalignment="top" width="393" background="lightgray" borderbrush="gray" borderthickness="1"/> <button content="browse file" height="34" horizontalalignment="left" margin="410,8,0,0" name="browsebutton" verticalalignment="top" width="119" foreground="maroon" fontsize="16" fontfamily="georgia" click="browsebutton_click" /> <canvas> <path canvas.left="61" canvas.top="28" width="133" height="98" fill="blue" stretch="fill" data="m61,125 l193,28" name="canvas1"/> </canvas> </grid> </window>
code behind:
namespace canvasstuff { public partial class mainwindow { public mainwindow() { initializecomponent(); } private void browsebutton_click(object sender, routedeventargs e) { openfiledialog dlg = new openfiledialog(); dlg.initialdirectory = "c:\\"; dlg.filter = "image files (*.jpg)|*.jpg|all files (*.*)|*.*"; dlg.restoredirectory = true; if (dlg.showdialog() == system.windows.forms.dialogresult.ok) { string selectedfilename = dlg.filename; imagebrush brush = new imagebrush(); brush.imagesource = new bitmapimage(new uri(selectedfilename, urikind.relative)); canvas1.background = brush; #error here bitmapimage bitmap = new bitmapimage(); } } } }
the "canvas1" element path, therefore has fill property rather background property, replace canvas1.background canvas1.fill. not give background path has small size. want window have background can using encompassing border.
<window x:class="canvasstuff.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="main window" height="409" width="574"> <border x:name="bgborder" borderthickness="0"> <!-- insert current content here --> </border> </window>
then replace
canvas1.background = brush;
with
bgborder.background = brush;
Comments
Post a Comment