示例#1
0
        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog
            {
                Filter          = "空间参考文件 (*.prj)|*.prj",
                OverwritePrompt = true
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = dialog.FileName;
                ESRI.ArcGIS.esriSystem.IPersistStream stream =
                    (ESRI.ArcGIS.esriSystem.IPersistStream) this.ispatialReference_0;
                IXMLStream stream2 = new XMLStreamClass();
                stream.Save((ESRI.ArcGIS.esriSystem.IStream)stream2, 1);
                stream2.SaveToFile(fileName);
                string str2  = stream2.SaveToString();
                int    index = str2.IndexOf("[");
                str2  = str2.Substring(index - 6);
                index = str2.LastIndexOf("]");
                str2  = str2.Substring(0, index + 1);
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                using (System.IO.FileStream stream3 = File.Create(fileName))
                {
                    this.method_2(stream3, str2);
                }
            }
        }
示例#2
0
        private void method_5()
        {
            ESRI.ArcGIS.esriSystem.IPersistStream stream =
                (ESRI.ArcGIS.esriSystem.IPersistStream) this.ispatialReference_0;
            IXMLStream stream2 = new XMLStreamClass();

            stream.Save((ESRI.ArcGIS.esriSystem.IStream)stream2, 1);
            string str   = stream2.SaveToString();
            int    index = str.IndexOf("[");

            str   = str.Substring(index - 6);
            index = str.LastIndexOf("]");
            str   = str.Substring(0, index + 1);
        }
        unsafe public static void Save(System.Runtime.InteropServices.ComTypes.IStream stream, object data)
        {
            // Exit if Stream or DataTable is NULL
            if (stream == null)
            {
                return;
            }
            if (data == null)
            {
                return;
            }

            //COM objects needs special care...
            if (Marshal.IsComObject(data))
            {
                //*** Create XmlWriter ***
                IXMLWriter xmlWriter = new XMLWriterClass();

                //*** Create XmlStream ***
                IXMLStream xmlStream = new XMLStreamClass();

                //*** Write the object to the stream ***
                xmlWriter.WriteTo(xmlStream as IStream);

                //*** Serialize object ***
                IXMLSerializer xmlSerializer = new XMLSerializerClass();
                xmlSerializer.WriteObject(xmlWriter, null, null, "arcobject", "http://www.esri.com/schemas/ArcGIS/9.2", data);

                string str = xmlStream.SaveToString();

                data = (object)str;
                if (null == data)
                {
                    return;
                }
            }

            //make sure that the object is serializable
            if (!data.GetType().IsSerializable)
            {
                throw new Exception("Object is not serializable.");
            }

            // Convert the string into a byte array
            MemoryStream    memoryStream    = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(memoryStream, data);
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

            // Get Memory Pointer to Int32
            int  cb;
            int *pcb = &cb;

            // write the object to the structured stream
            byte[] arrLen = BitConverter.GetBytes(bytes.Length);  // Get Byte Length
            stream.Write(arrLen, arrLen.Length, new IntPtr(pcb)); // Write Byte Length
            stream.Write(bytes, bytes.Length, new IntPtr(pcb));   // Write Btye Array

            if (bytes.Length != cb)
            {
                throw new Exception("Error writing object to stream");
            }
        }
    unsafe public static void Save(System.Runtime.InteropServices.ComTypes.IStream stream, object data)
    {
      // Exit if Stream or DataTable is NULL
      if (stream == null) { return; }
      if (data == null) { return; }

      //COM objects needs special care...
      if (Marshal.IsComObject(data))
      {
        //*** Create XmlWriter ***
        IXMLWriter xmlWriter = new XMLWriterClass();

        //*** Create XmlStream ***
        IXMLStream xmlStream = new XMLStreamClass();

        //*** Write the object to the stream ***
        xmlWriter.WriteTo(xmlStream as IStream);

        //*** Serialize object ***
        IXMLSerializer xmlSerializer = new XMLSerializerClass();
        xmlSerializer.WriteObject(xmlWriter, null, null, "arcobject", "http://www.esri.com/schemas/ArcGIS/9.2", data);

        string str = xmlStream.SaveToString();

        data = (object)str;
        if (null == data)
          return;
      }

      //make sure that the object is serializable
      if (!data.GetType().IsSerializable)
        throw new Exception("Object is not serializable.");
      
      // Convert the string into a byte array
      MemoryStream memoryStream = new MemoryStream();
      BinaryFormatter binaryFormatter = new BinaryFormatter();
      binaryFormatter.Serialize(memoryStream, data);
      byte[] bytes = memoryStream.ToArray();
      memoryStream.Close();

      // Get Memory Pointer to Int32
      int cb;
      int* pcb = &cb;

      // write the object to the structured stream
      byte[] arrLen = BitConverter.GetBytes(bytes.Length);  // Get Byte Length
      stream.Write(arrLen, arrLen.Length, new IntPtr(pcb));   // Write Byte Length
      stream.Write(bytes, bytes.Length, new IntPtr(pcb));     // Write Btye Array

      if (bytes.Length != cb)
        throw new Exception("Error writing object to stream");
    }