drawing - Windows Phone - Serialize/Save InkPresenter control -


i have inkpresenter , add strokes onto using inkpresenter.strokes.add(stroke). after that, want serialize/save inkpresenter file system or database. ink.strokes type of strokecollection , msdn says there save(stream) method issue. on windows phone, there seems lack of functionality.

maybe, there's way serialize , deserialize inkpresenter control.

as in jeff's post, on windows phone way have own type stores strokes , can save isolated storage can load , "replay" strokes. or can save strokecollection formatted string.

basically, each stroke consists of color , bunch of points. way can example store strokecollection collection of strings in simple format this:

color|x1,y1$x2,y2$x3,y3 -- represents single stroke

with can write function accepts stroke collection , file name , can save strokecollection

public void savestrokes(string filename, strokecollection strokecollection) {   var isf = isolatedstoragefile.getuserstoreforapplication();   var stream = isf.createfile(filename);    using (var writer = new streamwriter(stream)) {     foreach (var stroke in strokecollection) {       writer.writeline(string.format("{0}|{1}",            stroke.drawingattributes.color.tostring(),            string.join("$", stroke.styluspoints.select(p => string.format("{0},{1}", p.x, p.y)))));     }   } } 

now need function loads , displays stroke. loading function load stroke collection saved above method this:

public void loadstrokes(string filename, inkpresenter inkpresenter) {   var isf = isolatedstoragefile.getuserstoreforapplication();   var stream = isf.openfile(filename, filemode.open);    using (var reader = new streamreader(stream)) {     var strokes = reader.readtoend().split(new char[] { '\r', '\n' }, stringsplitoptions.removeemptyentries);     foreach (var stroke in strokes) {       var strokeparams = stroke.split('|');        var mystroke = new stroke();       mystroke.drawingattributes.color = hextocolor(strokeparams[0].tostring());        var pointlist = strokeparams[1].split('$');       foreach (var pointpair in pointlist) {         var pointpairlist = pointpair.split(',');         var x = convert.todouble(pointpairlist[0]);         var y = convert.todouble(pointpairlist[1]);          mystroke.styluspoints.add(new styluspoint(x, y));       }        inkpresenter.strokes.add(mystroke);     }   } } 

since colors going saved in hex format you'll need function convert color enum:

public system.windows.media.color hextocolor(string hexstring) {   string cleanstring = hexstring.replace("-", string.empty).substring(1);    var bytes = enumerable.range(0, cleanstring.length)                  .where(x => x % 2 == 0)                  .select(x => convert.tobyte(cleanstring.substring(x, 2), 16))                  .toarray();    return system.windows.media.color.fromargb(bytes[0], bytes[1], bytes[2], bytes[3]); } 

now can call save strokes

savestrokes("filename", myinkpresenter.strokecollection); 

and call redraw saved strokes

loadstrokes("filename", myinkpresenter); 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -