Friday 15 January 2010

c# - Displayed image in UI locks the source file -



c# - Displayed image in UI locks the source file -

i have datatemplate created here: comboboxes sharing observable collection keeps breaking

<usercontrol.resources> <datatemplate x:key="imageitemtemplate"> <stackpanel orientation="horizontal"> <image height="44" source="{binding path}"/> <label content="{binding name}" verticalalignment="center"/> </stackpanel> </datatemplate> </usercontrol.resources> <combobox x:name="image1" itemtemplate="{staticresource imageitemtemplate}"/>

code:

public observablecollection<imageitem> images = new observablecollection<imageitem>(); generic.importgrfx(tabid, image1, images); public static void importgrfx(string tabid, combobox combo, observablecollection<imageitem> items) { items.clear(); seek { string root = system.io.path.getdirectoryname(system.reflection.assembly.getexecutingassembly().location); var files = directory.getfiles(path.combine(root, "input\\" + tabid), "*.png"); foreach (var file in files) { imageitem item = new imageitem(); item.path = file; item.name = path.getfilename(file).remove(path.getfilename(file).length - 4); items.add(item); } } grab (exception) { } combo.itemssource = items; } public class imageitem { public string path { get; set; } public string name { get; set; } }

the problem having binding these images datatemplate, "locks" image source. meaning can't edit images while programme running... error stating image in use. there way prepare this?

here's skeleton of image updated whenever modify outside.

you set source, copies temp path , loads image there it watches changes in initial image , updates again rinse , repeat :d

you can, not utilize auto-update feature if need ability edit image outside.

it's basic sense free improve ...

code:

using system; using system.io; using system.windows; using system.windows.controls; using system.windows.media.imaging; namespace wpfapplication4 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); loaded += mainwindow_loaded; } private void mainwindow_loaded(object sender, routedeventargs e) { dynamicimage1.setsource(@"d:\untitled.png"); } } internal class dynamicimage : image { private string _name; private filesystemwatcher _watcher; public void setsource(string filename) { if (filename == null) throw new argumentnullexception("filename"); if (_watcher != null) { _watcher.changed -= watcher_changed; _watcher.dispose(); } string path = path.getdirectoryname(filename); _watcher = new filesystemwatcher(path); _watcher.enableraisingevents = true; _watcher.changed += watcher_changed; _name = filename; string tempfilename = path.gettempfilename(); file.copy(filename, tempfilename, true); source = new bitmapimage(new uri(tempfilename)); } private void watcher_changed(object sender, filesystemeventargs e) { bool b = string.equals(e.fullpath, _name, stringcomparison.invariantcultureignorecase); if (b) { string tempfilename = path.gettempfilename(); file.copy(e.fullpath, tempfilename, true); dispatcher.begininvoke((action) (() => { source = new bitmapimage(new uri(tempfilename)); })); _name = e.fullpath; } } } }

xaml:

<window x:class="wpfapplication4.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfapplication4="clr-namespace:wpfapplication4" title="mainwindow" height="350" width="525"> <grid> <wpfapplication4:dynamicimage x:name="dynamicimage1" /> </grid> </window>

c# wpf image locking datatemplate

No comments:

Post a Comment