Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. i want to save a image form serialization....

i want to save a image form serialization....

Scheduled Pinned Locked Moved C#
json
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maifs
    wrote on last edited by
    #1

    i want to save a image form serialization....

    hghghgh

    A 1 Reply Last reply
    0
    • M maifs

      i want to save a image form serialization....

      hghghgh

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      Thats nice. Not really a question though is it? So, where about are you? Do you have a stream which represents the image? If so, how did you get that image into the stream in the first place? You've got to give at least a little bit of information to get any help.

      My current favourite word is: Nipple!

      -SK Genius

      Game Programming articles start -here[^]-

      M 1 Reply Last reply
      0
      • A Anthony Mushrow

        Thats nice. Not really a question though is it? So, where about are you? Do you have a stream which represents the image? If so, how did you get that image into the stream in the first place? You've got to give at least a little bit of information to get any help.

        My current favourite word is: Nipple!

        -SK Genius

        Game Programming articles start -here[^]-

        M Offline
        M Offline
        maifs
        wrote on last edited by
        #3

        [Serializable] public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { draw(); } public void draw() { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 23, 23, 78, 78); } using System.IO; using System.Xml.Serialization; public partial class Form2 : Form { public Form2() { InitializeComponent(); } private static void Serialize() { Form1 obj = new Form1(); XmlSerializer mySerializer = new XmlSerializer(typeof(Form1)); StreamWriter myWriter = new StreamWriter("C:\\myDrawing.xml"); mySerializer.Serialize(myWriter, obj); myWriter.Close(); } private static void DeSerialize() { Form1 obj = null; XmlSerializer mySerializer = new XmlSerializer(typeof(Form1)); FileStream myFileStream = new FileStream("C:\\myDrawing.xml", FileMode.Open); obj= (Form1)mySerializer.Deserialize(myFileStream); obj.Draw(); } private void Form2_Load(object sender, EventArgs e) { Serialize(); // Form1 f = new Form1(); // putIt objDele = new putIt(f.toi); //objDele(); } private void button1_Click(object sender, EventArgs e) { DeSerialize(); } } now i think its enough for understanding ?

        hghghgh

        C M 2 Replies Last reply
        0
        • M maifs

          [Serializable] public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { draw(); } public void draw() { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 23, 23, 78, 78); } using System.IO; using System.Xml.Serialization; public partial class Form2 : Form { public Form2() { InitializeComponent(); } private static void Serialize() { Form1 obj = new Form1(); XmlSerializer mySerializer = new XmlSerializer(typeof(Form1)); StreamWriter myWriter = new StreamWriter("C:\\myDrawing.xml"); mySerializer.Serialize(myWriter, obj); myWriter.Close(); } private static void DeSerialize() { Form1 obj = null; XmlSerializer mySerializer = new XmlSerializer(typeof(Form1)); FileStream myFileStream = new FileStream("C:\\myDrawing.xml", FileMode.Open); obj= (Form1)mySerializer.Deserialize(myFileStream); obj.Draw(); } private void Form2_Load(object sender, EventArgs e) { Serialize(); // Form1 f = new Form1(); // putIt objDele = new putIt(f.toi); //objDele(); } private void button1_Click(object sender, EventArgs e) { DeSerialize(); } } now i think its enough for understanding ?

          hghghgh

          C Offline
          C Offline
          c2423
          wrote on last edited by
          #4

          Here you are serialising form1 - i.e. serialising its properties (text, size, position etc). This does not serialise the graphics. I suggest that if you want to save the graphics drawn at runtime you look at putting them into a bitmap, and saving that instead of using serialisation. Chris

          M 1 Reply Last reply
          0
          • C c2423

            Here you are serialising form1 - i.e. serialising its properties (text, size, position etc). This does not serialise the graphics. I suggest that if you want to save the graphics drawn at runtime you look at putting them into a bitmap, and saving that instead of using serialisation. Chris

            M Offline
            M Offline
            maifs
            wrote on last edited by
            #5

            i have to save through serialization..

            hghghgh

            C D 2 Replies Last reply
            0
            • M maifs

              i have to save through serialization..

              hghghgh

              C Offline
              C Offline
              c2423
              wrote on last edited by
              #6

              If you really, really must I think there is something deep within the graphics object for serialising it, but I couldn't tell you what off the top of my head. I suggest you have a look with Reflector at what is inside that might help you. Chris

              1 Reply Last reply
              0
              • M maifs

                i have to save through serialization..

                hghghgh

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                Do your drawing to a bitmap as suggested. Have a property of byte array. Save the bitmap to a memory stream and use the stream's ToArray method to set the data in your property. It should then serialize OK.

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                1 Reply Last reply
                0
                • M maifs

                  [Serializable] public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { draw(); } public void draw() { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 23, 23, 78, 78); } using System.IO; using System.Xml.Serialization; public partial class Form2 : Form { public Form2() { InitializeComponent(); } private static void Serialize() { Form1 obj = new Form1(); XmlSerializer mySerializer = new XmlSerializer(typeof(Form1)); StreamWriter myWriter = new StreamWriter("C:\\myDrawing.xml"); mySerializer.Serialize(myWriter, obj); myWriter.Close(); } private static void DeSerialize() { Form1 obj = null; XmlSerializer mySerializer = new XmlSerializer(typeof(Form1)); FileStream myFileStream = new FileStream("C:\\myDrawing.xml", FileMode.Open); obj= (Form1)mySerializer.Deserialize(myFileStream); obj.Draw(); } private void Form2_Load(object sender, EventArgs e) { Serialize(); // Form1 f = new Form1(); // putIt objDele = new putIt(f.toi); //objDele(); } private void button1_Click(object sender, EventArgs e) { DeSerialize(); } } now i think its enough for understanding ?

                  hghghgh

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Your code shows nothing related to an image, and as the others stated, you'll need to create an image to serialize (although your question is really hard to understand). System.Drawing.Image is serializable = here's a simple example:

                  Bitmap bm = new Bitmap(320, 240, PixelFormat.Format24bppRgb);
                  Graphics g = Graphics.FromImage(bm);
                  Pen pen = new Pen(Color.Azure);
                  g.DrawEllipse(pen, 10, 10, 100, 100);
                  g.DrawRectangle(pen, 10, 10, 200, 200);
                  //FileStream fs = new FileStream("c:\\serialized_bitmap.soap", FileMode.Create);
                  FileStream fs = new FileStream("c:\\serialized_bitmap.dat", FileMode.Create);
                  //SoapFormatter formatter = new SoapFormatter();
                  BinaryFormatter formatter = new BinaryFormatter();
                  try
                  {
                  formatter.Serialize(fs, bm);
                  }
                  catch (SerializationException )//e)
                  {
                  //Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
                  throw;
                  }
                  finally
                  {
                  fs.Close();
                  }

                  Of course, you'll have to create your Image/Bitmap the way you need to.

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  M 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Your code shows nothing related to an image, and as the others stated, you'll need to create an image to serialize (although your question is really hard to understand). System.Drawing.Image is serializable = here's a simple example:

                    Bitmap bm = new Bitmap(320, 240, PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(bm);
                    Pen pen = new Pen(Color.Azure);
                    g.DrawEllipse(pen, 10, 10, 100, 100);
                    g.DrawRectangle(pen, 10, 10, 200, 200);
                    //FileStream fs = new FileStream("c:\\serialized_bitmap.soap", FileMode.Create);
                    FileStream fs = new FileStream("c:\\serialized_bitmap.dat", FileMode.Create);
                    //SoapFormatter formatter = new SoapFormatter();
                    BinaryFormatter formatter = new BinaryFormatter();
                    try
                    {
                    formatter.Serialize(fs, bm);
                    }
                    catch (SerializationException )//e)
                    {
                    //Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
                    throw;
                    }
                    finally
                    {
                    fs.Close();
                    }

                    Of course, you'll have to create your Image/Bitmap the way you need to.

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    M Offline
                    M Offline
                    maifs
                    wrote on last edited by
                    #9

                    thx dear! now i understand.....

                    hghghgh

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups