Did I find a bug in WriteableBitmap when using string literals

Posted by liserdarts on Stack Overflow See other posts from Stack Overflow or by liserdarts
Published on 2010-05-08T19:18:00Z Indexed on 2010/05/08 19:28 UTC
Read the original article Hit count: 529

Filed under:

For performance reasons I'm converting a large list of images into a single image. This code does exactly what I want.

Private Function FlattenControl(Control As UIElement) As Image
    Control.Measure(New Size(1000, 1000))
    Control.Arrange(New Rect(0, 0, 1000, 1000))

    Dim ImgSource As New Imaging.WriteableBitmap(1000, 1000)
    ImgSource.Render(Control, New TranslateTransform)
    ImgSource.Invalidate

    Dim Img As New Image
    Img.Source = ImgSource
    Return Img
End Function

I can add all the images into a canvas pass the canvas to this function and I get back one image. My code to load all the images looks like this.

Public Function BuildTextures(Layer As GLEED2D.Layer) As FrameworkElement
    Dim Container As New Canvas

    For Each Item In Layer.Items
        If TypeOf Item Is GLEED2D.TextureItem Then
            Dim Texture = CType(Item, GLEED2D.TextureItem)
            Dim Url As New Uri(Texture.texture_filename, UriKind.Relative)
            Dim Img As New Image
            Img.Source = New Imaging.BitmapImage(Url)
            Container.Children.Add(Img)
        End If
    Next

    Return FlattenControl(Container)
End Function

The GLEED2D.Layer and GLEED2D.TextureItem classes are from the free level editor GLEED2D (http://www.gleed2d.de/). The texture_filename on every TextureItem is "Images/tree_clipart_pine_tree.png"

This works just fine, but it's just a proof of concept. What I really need to do (among other things) is have the path to the image hard coded. If I replace Texture.texture_filename in the code above with the string literal "Images/tree_clipart_pine_tree.png" the images do not appear in the final merged image. I can add a breakpoint and see that the WriteableBitmap has all of it's pixels as 0 after the call to Invalidate.

I have no idea how this could cause any sort of difference, but it gets stranger. If I remove the call to FlattenControl and just return the Canvas instead, the images are visible. It's only when I use the string literal with the WriableBitmap that the images do not appear.

I promise you that the value in the texture_filename property is exactly "Images/tree_clipart_pine_tree.png". I'm using Silverlight 3 and I've also reproduced this in Silverlight 4.

Any ideas?

© Stack Overflow or respective owner

Related posts about Silverlight