我需要使用bitmapImage序列化自定義類(現在由xmlIgnore標記)。 我正在使用xmlSerialization,但我認爲多數民衆贊成bad.Do你有一些想法我如何序列化我的類??可能你可以提供一些簡單的例子??需要序列化位圖圖像silverlight
class X
{
private BitmapImage someImage;
public BitmaImage{get;set}
}
實際上後來我會使用WCF服務。 謝謝)
我需要使用bitmapImage序列化自定義類(現在由xmlIgnore標記)。 我正在使用xmlSerialization,但我認爲多數民衆贊成bad.Do你有一些想法我如何序列化我的類??可能你可以提供一些簡單的例子??需要序列化位圖圖像silverlight
class X
{
private BitmapImage someImage;
public BitmaImage{get;set}
}
實際上後來我會使用WCF服務。 謝謝)
可以暴露image as a byte array,例如:
public byte[] ImageAsBytes
{
get { return BytesFromImage(someImage); }
set { someImage = ImageFromBytes(value); }
}
當然你也可以轉換回使用流和StreamSource
財產。
您可以將圖像轉換爲Base64
字符串。從here例子:
//Convert image to the string
public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
//when deserializing, convert the string back to an image
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
但是Silverlight中的圖像類沒有「保存」方法! – alexkovelsky 2015-02-23 08:11:41
但是我無法在BitmapImage上找到StreameSource – 2012-01-02 18:42:40
@ThomasWingfield - 這可能是因爲您正在使用Silverlight(下次,使用正確的標籤標記您的問題!!) – 2012-01-03 13:41:08