如何在 Silverlight 3.0 中使用 SaveFileDialog 保存 BitmapImage/WriteableBitmap? [英] How to save BitmapImage / WriteableBitmap using SaveFileDialog in Silverlight 3.0?
问题描述
如何将 Silverlight 中的 WriteableBitmap 保存到文件系统中,我不确定如何使用 FileStream 来完成这项工作,它可以是 Bitmap、PNG、Jpeg 格式等,只要不是商业库需要.
可以这样做吗?
这是我对 SaveDialog 的调用,如下:
Dim SaveDialog As New SaveFileDialog如果 SaveDialog.ShowDialog 那么尝试使用 FileStream 作为 Stream = SaveDialog.OpenFile' 在此处保存图像文件代码结束使用Catch ex 作为例外结束尝试万一
编辑
添加了对 WritableBitmap 的提及,因为这是我的 XAML 正在写入的内容,我只需要为了保存它,有一个 WriteableBitmap.Pixels 属性,它是一个一维整数像素数组,可以将其放入文件流中,如果可以,如何放入?
<小时>这是我的一些示例代码,用于将像素流写入文件,但是这有效它不会位图化"数据和结果输出无用,但它正在写入我的图像流,我只是需要知道如何将其标记为位图文件(或其他任何文件).我之前找到了 JPEG 和 PNG 示例,但它们都没有很好地解释它们是如何工作的,我可能不得不使用 PNGEncoding 方法,因为这是唯一可以集成到我的代码库中的方法.但是有没有简单的 BMP 文件版本?
Dim Image As Imaging.WriteableBitmap = _Style.ImageDim Area As Integer = Image.PixelWidth * Image.PixelHeight对于 Raster As Integer = 0 To Area - 1Dim Pixel As Integer = Image.Pixels(Raster)Dim Bytes() = BitConverter.GetBytes(Pixel And &HFF)FileStream.Write(Bytes, 0, 4)下一个
有几个开源编码器:
FJCore:http://code.google.com/p/fjcore/
Joe Stegman 的 PNG 编码器:http://blogs.msdn.com/jstegman/archive/2008/04/21/dynamic-image-generation-in-silverlight.aspx
FreeImage:http://freeimage.sourceforge.net/
您可以使用其中任何一种来编码各种格式.这是一篇关于在烘焙后保存 WriteableBitmap 的帖子:
更新:在进行更多搜索之后,由于安全问题,似乎将 WriteableBitmap 保存到文件在 SL3 中受到限制.有一个解决方法:
看起来您必须将 Canvas 的内容保存到文件中.您应该能够修改示例以保存 JPEG 或其他格式.
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
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:
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:
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.
这篇关于如何在 Silverlight 3.0 中使用 SaveFileDialog 保存 BitmapImage/WriteableBitmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!