BitmapImage的为byte [] [英] BitmapImage to byte[]
问题描述
我有一个的BitmapImage
,我使用的一个WPF应用程序,后来我想把它保存到数据库中的字节数组(我想这是最好的办法),我怎么能完成这种转换?
I have a BitmapImage
that I'm using in a WPF application, I later want to save it to a database as a byte array (I guess it's the best way), how can I perform this conversion?
或者,或者,有没有更好的方式来节省的BitmapImage
(或任何其基类,的BitmapSource
或的ImageSource
)到数据存储库?
Or, alternatively, is there a better way to save a BitmapImage
(or any of its base classes, BitmapSource
or ImageSource
) to a data repository?
推荐答案
要转换为一个byte []你可以用一个MemoryStream:
To convert to a byte[] you can use a MemoryStream:
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
相反的JpegBitmapEn codeR您可以使用任何BitmapEn codeR你喜欢casperOne说。
Instead of the JpegBitmapEncoder you can use whatever BitmapEncoder you like as casperOne said.
如果您使用的是MS SQL,你也可以使用图片
-Column为MS SQL支持的数据类型,但你仍然需要到莫名其妙的BitmapImage转换。
If you are using MS SQL you could also use a image
-Column as MS SQL supports that datatype, but you still would need to convert the BitmapImage somehow.
这篇关于BitmapImage的为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!