This constructor is required by
ISerializable.
Syntax
Parameters
- info
- The data needed to serialize or deserialize an object.
- context
- The source and destination of a given serialized stream.
Example
Visual Basic |
Copy Code |
Public Sub MyRasterImageSerializationTest() RasterCodecs.Startup() Dim codecs As RasterCodecs = New RasterCodecs()
Dim img As RasterImage = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp")
Dim myImage As MyRasterImage = New MyRasterImage(img)
myImage.MyIntegerData = 10 myImage.MyStringData = "My string" Dim msg As String = String.Format("Before serialization. MyIntegerData = {0}, MyStringData = {1}", myImage.MyIntegerData, myImage.MyStringData) MessageBox.Show(msg)
img.Dispose()
Dim formatter As BinaryFormatter = New BinaryFormatter() Dim ms As MemoryStream = New MemoryStream() formatter.Serialize(ms, myImage)
myImage.Dispose() myImage = Nothing
ms.Position = 0 myImage = CType(IIf(TypeOf formatter.Deserialize(ms) Is MyRasterImage, formatter.Deserialize(ms), Nothing), MyRasterImage)
msg = String.Format("After serialization. MyIntegerData = {0}, MyStringData = {1}", myImage.MyIntegerData, myImage.MyStringData) MessageBox.Show(msg)
codecs.Save(myImage, LeadtoolsExamples.Common.ImagesPath.Path + "Image1_MySerialized.bmp", RasterImageFormat.Bmp, 24)
ms.Close() myImage.Dispose() codecs.Dispose() RasterCodecs.Shutdown() End Sub |
C# |
Copy Code |
public void MyRasterImageSerializationTest() { // Load an image RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); RasterImage img = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"); // create a new MyRasterImage instance out of this image MyRasterImage myImage = new MyRasterImage(img); // Set our custom data myImage.MyIntegerData = 10; myImage.MyStringData = "My string"; string msg = string.Format("Before serialization. MyIntegerData = {0}, MyStringData = {1}", myImage.MyIntegerData, myImage.MyStringData); MessageBox.Show(msg); // img is invalid now and should be disposed img.Dispose(); // Serialize myImage BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); formatter.Serialize(ms, myImage); // dispose myImage myImage.Dispose(); myImage = null; // Deserialize back from the stream ms.Position = 0; myImage = formatter.Deserialize(ms) as MyRasterImage; msg = string.Format("After serialization. MyIntegerData = {0}, MyStringData = {1}", myImage.MyIntegerData, myImage.MyStringData); MessageBox.Show(msg); // re-save the image codecs.Save(myImage, LeadtoolsExamples.Common.ImagesPath.Path + "Image1_MySerialized.bmp", RasterImageFormat.Bmp, 24); // Clean up ms.Close(); myImage.Dispose(); codecs.Dispose(); RasterCodecs.Shutdown(); } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also