我做了一个小应用程序来序列化和反序列化xml中的图像。我尝试做的是序列化,然后将其反序列化为位图类型并返回它。然后我想把这个图像放到一个列表框中,所以我以编程方式创建了一个pictureBox但是当我尝试将内存流转换为Bitmap时,我得到以下错误public JsonResult GenericName_Bind(string ProductID)
{
PrescriptionBind generic = new PrescriptionBind();
DataSet ds = generic.Get_GenericName(ProductID);
List<SelectListItem> statelist = new List<SelectListItem>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
statelist.Add(new SelectListItem { Text = dr["GenericName"].ToString(), Value = dr["GenericID"].ToString() });
}
return Json(statelist, JsonRequestBehavior.AllowGet);
}
#endregion
public DataSet Get_GenericName(string ProductID)
{
SqlCommand com = new SqlCommand("Select * from ProductMaster where ProductID=@ProductID", product);
com.Parameters.AddWithValue("@ProductID", ProductID);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
,这是一个Additional information: Unable to translate bytes [FF] at index 0 from specified code page to Unicode
。我不确定我应该给XMLSerializedr类DescoderFallbackException
什么,但我假设它是Bitmap。
我的代码如下
调用Serializer和Deserializer方法并将返回的图像放入picturebox的代码
GetType
序列化方法
Dim mem As MemoryStream
Serialize()
Dim pic As Bitmap = Deserialize(mem)
ListBoxTemplates.Items.Clear()
Dim selectedCatID = AppSettings.PowerPoint.TemplateCategories.SingleOrDefault(Function(t) t.Name = ComboBoxNewLevel1.Text).ID
Dim selectedCat2ID = AppSettings.PowerPoint.TemplateCategories.SingleOrDefault(Function(t) t.Name = ComboBoxNewLevel2.Text).ID
Dim level3Cat = From Temp In AppSettings.PowerPoint.PSLTemplates
Join cat In AppSettings.PowerPoint.TemplateCategories On Temp.CategoryLevel1ID Equals cat.ID
Where Temp.CategoryLevel1ID = selectedCatID And selectedCat2ID = Temp.CategoryLevel2ID
Select Temp.Name
For Each t In level3Cat
Dim pb As PictureBox = New PictureBox()
pb.Location = Location()
pb.Name = ""
pb.BackgroundImage = pic
ListBoxTemplates.Items.Add(pb)
Next
反序列化方法(问题出在哪里)
Public Sub Serialize()
Dim corolla As New Bitmap("C:\xmltext.png", True)
Dim ms As New MemoryStream()
corolla.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim sw As New StringWriter()
Dim xw As New XmlTextWriter(sw)
xw.Formatting = Formatting.Indented
Dim ser As New XmlSerializer(ms.GetBuffer().[GetType]())
ser.Serialize(xw, ms.GetBuffer())
Debug.WriteLine("Serialized Content::")
Dim sSerializedContent As String = sw.GetStringBuilder().ToString()
Debug.WriteLine(sSerializedContent)
mem = ms
xw.Close()
End Sub