2017-02-15 3 views
0

Wie kann ich nur serialisierbare DataPoints? Ich möchte DataPoints in einer Datei speichern.Datenpunkt Serialisierung

[Serializable] 
class CIE 
{ 
    public List<DataPoint> CieDataPoint = new List<DataPoint>() ; 
} 
List<DataPoint> GetCieColorPoints(string filename, int count) 
{ 
     CIE cie = new CIE(); 
     var data = new List<DataPoint>(); 

     float cr = (float)Math.Sqrt(count); 
     using (Bitmap bmp = new Bitmap(filename)) 
     { 
      int sx = (int)(bmp.Width/cr); 
      int sy = (int)(bmp.Height/cr); 

      float scx = 0.8f/bmp.Width; 
      float scy = 0.9f/bmp.Height; 

      for (int x = 0; x < bmp.Width; x += sx) 
       for (int y = 0; y < bmp.Height; y += sy) 
       { 
        Color c = bmp.GetPixel(x, y); 
        float b = c.GetBrightness(); 
        if (b > 0.01f && b < 0.99f) 
        { 
         var dp = new DataPoint(x * scx, 0.9f - y * scy); 
         dp.Color = c;       
         dp.MarkerColor = dp.Color; 
         dp.MarkerStyle = MarkerStyle.Circle ; 
         data.Add(dp); 
        } 
       } 
     } 
     return data; 
    } 

Cie.CieDataPoint = GetCieColorPoints("E:\\CIExy1931_T2.png", 125000);; 
IFormatter formatter = new BinaryFormatter(); 
FileStream seryalization = new FileStream("CIEdata", FileMode.Create, FileAccess.Write); 
formatter.Serialize(seryalization, Cie); 
seryalization.Close(); 

Fehler Zusätzliche Informationen: Type 'System.Windows.Forms.DataVisualization.Charting.DataPoint' in Versammlung System.Windows.Forms.DataVisualization, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 'ist nicht als serialisierbar gekennzeichnet.

+0

zeigen Bitte geben Sie die Definition von 'DataPoint'. Darüber hinaus bietet [MSDN] (https://msdn.microsoft.com/de-de/library/system.serializableattribute (v = vs.110) .aspx) ein Beispiel, wie das Attribut "Serializable" verwendet werden kann. – Codor

+0

@Codor Es ist ein Framework-Typ - 'System.Windows.Forms.DataVisualization.Charting.DataPoint'. –

Antwort

0

DataPoints enthalten mehrere Eigenschaften, die nicht (direkt) serialisierbar sind, wie Colors, Fonts und dann einige. Entweder Sie erstellen serialisierbare Typen für diese und eine vollständig serialisierbare DataPoint Klasse oder, wenn Sie nur eine Teilmenge benötigen, erstellen Sie eine serialisierbare Klasse, die nur etwa ein int für die Farbe und zwei Doppel für die x & y-Werte enthält, vielleicht eine Zeichenfolge für den Namen oder einem Tooltip ..

Hier ist ein Beispiel für eine serializable Klasse mit einer minimalen Teilmenge eines DataPoint ‚s Eigenschaften:

[Serializable] 
public class SPoint 
{ 
    public int PointColor { get; set; } 
    public double XValue { get; set; } 
    public double YValue { get; set; } 
    public string Name { get; set; } 

    public SPoint()  {  } 

    public SPoint(int c, double xv, double yv, string n) 
    { 
     PointColor = c; XValue = xv; YValue = yv; Name = n; 
    } 

    static public SPoint FromDataPoint(DataPoint dp) 
    { 
     return new SPoint(dp.Color.ToArgb(), dp.XValue, dp.YValues[0], dp.Name); 
    } 

    static public DataPoint FromSPoint(SPoint sp) 
    { 
     DataPoint dp = new DataPoint(sp.XValue, sp.YValue); 
     dp.Color = Color.FromArgb(sp.PointColor); 
     dp.Name = sp.Name; 
     return dp; 
    } 
} 

es wie folgt verwendet:

using System.Xml.Serialization; 
... 
... 
var points = chart.Series[0].Points; 

List<SPoint> sPoints = points.Cast<DataPoint>() 
          .Select(x => SPoint.FromDataPoint(x)) 
          .ToList(); 

XmlSerializer xs = new XmlSerializer(sPoints.GetType()); 
using (TextWriter tw = new StreamWriter(@"yourfilename.xml")) 
{ 
    xs.Serialize(tw, sPoints); 
    tw.Close(); 
} 

Natürlich Deserialisieren tut die gleichen rückwärts:

using (TextReader tw = new StreamReader(@"yourfilename.xml")) 
{ 
    //chart.Series[0].Points.Clear(); 
    //sPoints.Clear(); 
    sPoints = (List<SPoint>)xs.Deserialize(tw); 
    tw.Close(); 
} 
foreach (var sp in sPoints) s.Points.Add(SPoint.FromSPoint(sp)); 
Verwandte Themen