c# - Wrap ImageSource for Base64 image serialization -
i have wpf control contains panels images. i'm trying serialize can loaded standalone without having have images in local folder.
i know can store images base64 string , possibly load up, wrap imagesource class accept base64 string source.
i looked imagesource class bit, , believe don't know enough how works. when implement imagesource in custom wrapper class 2 methods i'm unclear on:
metadata
createinstancecore
i wondering if shed light on these methods, or point me in direction doesn't lead me msdn documentation.
this class wraps bitmapimage initialized base64 string property:
public class base64bitmapimage : bitmapsource { private bitmapimage bitmap; private string base64source; public string base64source { { return base64source; } set { base64source = value; bitmap = new bitmapimage(); if (decodefailed != null) { bitmap.decodefailed += decodefailed; } using (var stream = new memorystream(convert.frombase64string(base64source))) { bitmap.begininit(); bitmap.cacheoption = bitmapcacheoption.onload; bitmap.streamsource = stream; bitmap.endinit(); } if (decodefailed != null) { bitmap.decodefailed -= decodefailed; } bitmap.freeze(); } } public override event eventhandler<exceptioneventargs> decodefailed; public override bool isdownloading { { return false; } } public override pixelformat format { { return bitmap != null ? bitmap.format : base.format; } } public override double dpix { { return bitmap != null ? bitmap.dpix : base.dpix; } } public override double dpiy { { return bitmap != null ? bitmap.dpiy : base.dpiy; } } public override double width { { return bitmap != null ? bitmap.width : base.width; } } public override double height { { return bitmap != null ? bitmap.height : base.height; } } public override int pixelwidth { { return bitmap != null ? bitmap.pixelwidth : base.pixelwidth; } } public override int pixelheight { { return bitmap != null ? bitmap.pixelheight : base.pixelheight; } } public override imagemetadata metadata { { return bitmap != null ? bitmap.metadata : base.metadata; } } public override void copypixels(array pixels, int stride, int offset) { if (bitmap != null) { bitmap.copypixels(pixels, stride, offset); } } public override void copypixels(int32rect sourcerect, array pixels, int stride, int offset) { if (bitmap != null) { bitmap.copypixels(sourcerect, pixels, stride, offset); } } public override void copypixels(int32rect sourcerect, intptr buffer, int buffersize, int stride) { if (bitmap != null) { bitmap.copypixels(sourcerect, buffer, buffersize, stride); } } protected override freezable createinstancecore() { var instance = new base64bitmapimage(); instance.bitmap = bitmap; instance.base64source = base64source; return instance; } }
it may used this:
<image> <image.source> <local:base64bitmapimage base64source="..."/> </image.source> </image>
Comments
Post a Comment