views:

5244

answers:

2

How can a WriteableBitmap from Silverlight be Saved onto the File System, I am unsure what to do with the FileStream to make this work, it can be in Bitmap, PNG, Jpeg format etc, as long as a commercial library is not required.
Is it possible to do this?

Here is my call to SaveDialog, below:

    Dim SaveDialog As New SaveFileDialog
    If SaveDialog.ShowDialog Then
        Try
            Using FileStream As Stream = SaveDialog.OpenFile
               ' Save Image File Code Here
            End Using
        Catch ex As Exception

        End Try
    End If

Edit

Added mention of WritableBitmap as this is what my XAML is writing to, I just need to save it, there is a WriteableBitmap.Pixels property which is a 1-dimensional array of integer pixels, can this be put into a filestream, and if so, how?


Here is some example code I have that writes the Pixel Stream to a File, however this works it does not "bitmapify" the data and the resulting output is useless but it is writing my image stream, I just need to know how to mark-it-up so that it is a Bitmap file (or anything else). I have found the JPEG and PNG examples before, but none of them explain well how they work, I may have to use the PNGEncoding method as this is the only one that will integrate into my codebase. But is there a simple BMP file version out there?

        Dim Image As Imaging.WriteableBitmap = _Style.Image
        Dim Area As Integer = Image.PixelWidth * Image.PixelHeight
        For Raster As Integer = 0 To Area - 1
            Dim Pixel As Integer = Image.Pixels(Raster)
            Dim Bytes() = BitConverter.GetBytes(Pixel And &HFF)
            FileStream.Write(Bytes, 0, 4)
        Next
+1  A: 

There are a several open-source encoders out there:

FJCore: http://code.google.com/p/fjcore/

Joe Stegman's PNG Encoder: http://blogs.msdn.com/jstegman/archive/2008/04/21/dynamic-image-generation-in-silverlight.aspx

FreeImage: http://freeimage.sourceforge.net/

You can use any of these to encode various formats. Here's a post on saving a WriteableBitmap once it's baked:

http://community.irritatedvowel.com/blogs/pete%5Fbrowns%5Fblog/archive/2009/03/18/Silverlight-3-%5F1320%5F-The-Bitmap-API-%5F2F00%5F-WriteableBitmap.aspx

UPDATE: After doing some more searching it looks like saving a WriteableBitmap to a file is limited in SL3 due to security issues. There is a workaround:

http://geekswithblogs.net/braulio/archive/2009/07/12/export-canvas-to-png-and-save-it-in-your-local.aspx

Looks like you have to save the contents of a Canvas to a file. You should be able to modify the sample to save JPEG or other formats.

Dave Swersky
IOW, a considerable effort is needed and amongst that lot I'm still not clear that there is a solution to saving the WritabelBitmap.
AnthonyWJones
They don't make it easy. I gather from some older posts that this was identified as an issue in SL3 RC, and that Microsoft was going to "try" to resolve it in RTW. I'm not seeing any definitive answer as to whether they did, but the lack of any such guidance leads me to believe they did not.
Dave Swersky
I used the FJCore library and it worked just fine, plus this was the main format I was hoping for, the fact it is possible to stream to a file from an image - even if this is jibberish, means there may be a way - maybe better in Silverlight 4.0?
RoguePlanetoid
I do hope they make it easier in SL.Next. It would make Silverlight a powerful image-manipulation platform.
Dave Swersky
+1  A: 

I have previously written this post that goes through how to save XAML to a bitmap using a writeable bitmap, the save dialog box and FJCore.

http://blog.blueboxes.co.uk/2009/07/21/rendering-xaml-to-a-jpeg-using-silverlight-3/

John
That is a great example, I used this to actually create the Save As code for using FJCore, forgot to post it, thanks for doing that as it may help out others, plus it is very easy to convert that code to VB, which is what I used to develop my application that this was needed for.
RoguePlanetoid