2013-04-18 23 views
0

我有一個包含圖像面板的WPF控件。我試圖序列化,以便它可以獨立加載,而不必在本地文件夾中有圖像。包裝BaseS圖像序列化ImageSource

我知道我可以將圖像存儲爲Base64字符串,然後可能加載備份,但我想要做的是包裝ImageSource類以接受Base64字符串作爲源。

我看了一下ImageSource類,我相信我對它的工作原理知之甚少。當我在我的自定義包裝類實現的ImageSource我得到2種方法,我很清楚的:

  1. CreateInstanceCore

我想知道如果有人能提供一些線索在這些方法上,或者指向一個不會讓我回到MSDN文檔的方向。

+0

爲什麼不只是在裝配時的圖像作爲嵌入資源你的可執行文件或控制庫? – Clemens

+0

由於持久化的唯一數據是序列化的XAML,並且XAML序列化不能很好地與圖像配合使用。 – Gianni

+0

您可能想要查看[實現自定義BitmapSource](http://blogs.msdn.com/b/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx)。 – Clemens

回答

3

該類包裝是從一個base64字符串屬性初始化的BitmapImage:

public class Base64BitmapImage : BitmapSource 
{ 
    private BitmapImage bitmap; 
    private string base64Source; 

    public string Base64Source 
    { 
     get { 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 
    { 
     get { return false; } 
    } 

    public override PixelFormat Format 
    { 
     get { return bitmap != null ? bitmap.Format : base.Format; } 
    } 

    public override double DpiX 
    { 
     get { return bitmap != null ? bitmap.DpiX : base.DpiX; } 
    } 

    public override double DpiY 
    { 
     get { return bitmap != null ? bitmap.DpiY : base.DpiY; } 
    } 

    public override double Width 
    { 
     get { return bitmap != null ? bitmap.Width : base.Width; } 
    } 

    public override double Height 
    { 
     get { return bitmap != null ? bitmap.Height : base.Height; } 
    } 

    public override int PixelWidth 
    { 
     get { return bitmap != null ? bitmap.PixelWidth : base.PixelWidth; } 
    } 

    public override int PixelHeight 
    { 
     get { return bitmap != null ? bitmap.PixelHeight : base.PixelHeight; } 
    } 

    public override ImageMetadata Metadata 
    { 
     get { 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; 
    } 
} 

它可以使用這樣的:

<Image> 
    <Image.Source> 
     <local:Base64BitmapImage Base64Source="..."/> 
    </Image.Source> 
</Image>