info
The SerializationInfo to populate with data.
context
The contextual information about the source or destination of the serialization.
This method implements ISerializable.GetObjectData.
The RasterImage class supports standard .NET serialization. Serialization a RasterImage is the process of converting the state of an RasterImage object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an RasterImage object. Together, these processes allow the image data to be easily stored and transferred.
When you serialize an RasterImage object, all the data inside the object are saved. This include the image data of all pages as well as the current page number, region information, low bit/high bit, palette data, metadata (tags, markers and comments), etc. In other words, the RasterImage can be constructed back to its original state from the stream.
For more information and examples regarding serialization of an RasterImage object, refer to RasterImage Serialization.
This method will use the value of RasterDefaults.CompressOnSerialize to determine whether to use compression when serializing the RasterImage object.
using Leadtools;using Leadtools.Codecs;public void MyRasterImageSerializationTest(){// Load an imageRasterCodecs codecs = new RasterCodecs();RasterImage img = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"));// create a new MyRasterImage instance out of this imageMyRasterImage myImage = new MyRasterImage(img);// Set our custom datamyImage.MyIntegerData = 10;myImage.MyStringData = "My string";string msg = string.Format("Before serialization. MyIntegerData = {0}, MyStringData = {1}", myImage.MyIntegerData, myImage.MyStringData);Console.WriteLine(msg);// img is invalid now and should be disposedimg.Dispose();// Serialize myImageBinaryFormatter formatter = new BinaryFormatter();MemoryStream ms = new MemoryStream();formatter.Serialize(ms, myImage);// dispose myImagemyImage.Dispose();myImage = null;// Deserialize back from the streamms.Position = 0;myImage = formatter.Deserialize(ms) as MyRasterImage;msg = string.Format("After serialization. MyIntegerData = {0}, MyStringData = {1}", myImage.MyIntegerData, myImage.MyStringData);Console.WriteLine(msg);// re-save the imagecodecs.Save(myImage, Path.Combine(LEAD_VARS.ImagesDir, "Image1_MySerialized.bmp"), RasterImageFormat.Bmp, 24);// Clean upms.Close();ms.Dispose();myImage.Dispose();codecs.Dispose();}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";}
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import org.junit.*;import org.junit.runner.JUnitCore;import org.junit.runner.Result;import org.junit.runner.notification.Failure;import static org.junit.Assert.*;import leadtools.*;import leadtools.codecs.*;public void MyRasterImageSerializationTest() {final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";RasterCodecs codecs = new RasterCodecs();RasterImage img = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "sample5.cmp"));// create a new MyRasterImage instance out of this imageSerializeRasterImage myImage = new SerializeRasterImage(img);// Set our custom datamyImage.setMyIntegerData(10);myImage.setMyStringData("My string");String msg = String.format("Before serialization. MyIntegerData = %s, MyStringData = %s%n",myImage.getMyIntegerData(), myImage.getMyStringData());System.out.println(msg);// img is invalid now and should be disposedimg.dispose();;// Serializationtry {// Saving of object in a fileFile serFile = new File(combine(LEAD_VARS_IMAGES_DIR, "Serialized.txt"));FileOutputStream fos = new FileOutputStream(serFile);ObjectOutputStream out = new ObjectOutputStream(fos);// Method for serialization of objectout.writeObject(myImage);out.close();fos.close();System.out.println("Object has been serialized");// dispose myImagemyImage = null;// Deserialize back from the stream// Reading the object from a fileFile deserFile = new File(combine(LEAD_VARS_IMAGES_DIR, "Serialized.txt"));FileInputStream fis = new FileInputStream(deserFile);ObjectInputStream in = new ObjectInputStream(fis);// Method for deserialization of objectmyImage = (SerializeRasterImage) in.readObject();in.close();fis.close();System.out.println("Object has been deserialized ");System.out.println("a = " + myImage.getMyIntegerData());System.out.println("b = " + myImage.getMyStringData());} catch (IOException | ClassNotFoundException ex) {System.out.println(ex);}msg = String.format("After serialization. MyIntegerData = %s, MyStringData = %s%n", myImage.getMyIntegerData(),myImage.getMyStringData());System.out.println(msg);// re-save the imagecodecs.save(myImage, combine(LEAD_VARS_IMAGES_DIR, "Image1_MySerialized.bmp"), RasterImageFormat.BMP, 24);assertTrue("Image unsuccessfully saved to " + combine(LEAD_VARS_IMAGES_DIR, "Image1_MySerialized.bmp"),(new File(combine(LEAD_VARS_IMAGES_DIR, "Image1_MySerialized.bmp"))).exists());System.out.println("Image is saved to " + combine(LEAD_VARS_IMAGES_DIR, "Image1_MySerialized.bmp"));// Clean upmyImage.dispose();codecs.dispose();}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
