0

I create images on the fly by using the following code:

Public Function ToBitmapImage(byteArray As Byte()) As BitmapImage
    Dim image As New BitmapImage()
    Dim stream As New MemoryStream(byteArray)
    image.BeginInit()
    image.StreamSource = stream
    image.EndInit()
    image.Freeze()
    Return image
End Function

Then I tried to clone one of the objects containing the image with the following code:

Public Function Clone(Of T As DependencyObject)(orginal As T) As T
    If (orginal Is Nothing) Then
        Return Nothing
    End If

    Using stream As New MemoryStream()
        XamlWriter.Save(orginal, stream)
        stream.Position = 0
        Return CType(XamlReader.Load(stream), T)
    End Using
End Function

The images are serialized into:

...xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"...
<av:Image ...>
    <av:Image.Source>
        <av:BitmapImage BaseUri="{x:Null}" />
    </av:Image.Source>
</av:Image>

The BaseUri="{x:Null}" causes Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception. rownumber 1 row position xxx. Is there any way to force it so serialize the data in the BitmapImage instead of baseUrl?

Peter
  • 37,042
  • 39
  • 142
  • 198

1 Answers1

0

I found a ugly solution, instead of using image.StreamSource = stream I write them to a temporary file and set BaseUri and UriSource to the file location.

This means I have to delete the files when I'm done with them, so if there is a better solution I'm interested!

Peter
  • 37,042
  • 39
  • 142
  • 198