Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I used below codes to save IMap into byte[] (to store in my database), but I can't load it again, are there anyone know what's wrong ?

public byte[] SaveMap(AxPageLayoutControl pageLayOut)
{
    IGraphicsContainer graphicsContainer = pageLayOut.GraphicsContainer;
    graphicsContainer.Reset();
    IElement pElement = graphicsContainer.Next();
    while (pElement != null)
    {
           if (pElement is IMapFrame)
           {
               IMapDocument mapDocument = new MapDocumentClass();
               if (File.Exists(@"D:\test.mxd")) 
                        File.Delete (@"D:\test.mxd");

               mapDocument.New(@"D:\test.mxd");
               mapDocument.ReplaceContents((IMxdContents)pageLayoutTo2_3.ActiveView.FocusMap);
               mapDocument.Save(true, true);

               System.IO.FileStream objFileStream = new System.IO.FileStream(@"D:\test.mxd", FileMode.Open);
               byte[] result = new byte[objFileStream.Length];
               objFileStream.Read(result, 0, (int)objFileStream.Length);

               return result;
            }
            pElement = graphicsContainer.Next();
      }
}

public void LoadMap(AxPageLayoutControl pageLayOut, byte[] result)
{
    IGraphicsContainer graphicsContainer = pageLayoutTo2_3.GraphicsContainer;
    graphicsContainer.Reset();
    IElement pElement = graphicsContainer.Next();
    while (pElement != null)
    {
           if (pElement is IMapFrame)
           {

               System.IO.FileStream oFileStream = new System.IO.FileStream(@"D:\test.mxd", FileMode.Append);
               oFileStream.Write(result, 0, result.Length);

               IMapControl2 map = new MapControlClass();
               map.LoadMxFile(@"D:\test.mxd", Type.Missing, Type.Missing );

               IMapFrame frame = new MapFrameClass();
               frame.Map = map.Map;
               pElement = (IElement)frame;

               pageLayOut.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

               return;
            }
            pElement = graphicsContainer.Next();
      }
}
share|improve this question
    
Did you try using IMapDocument.Open to get the IMap in Loadmap? Also, did you try adding IMapDocument.Close after saving? –  Kirk Kuykendall Apr 5 '13 at 14:50

1 Answer 1

You seem going to serialize and deserialize Map object. The class which implement IPersistStream interface can serialize and deserialize to byte array, string or text file. Here's a sample code.

//serialize Map object to byte array
IMap pMap = axMapControl1.Map;
IXMLStream pXMLStream = new XMLStreamClass();
IObjectStream pObjStream = new ObjectStreamClass();
pObjStream.Stream = pXMLStream as IStream;
pObjStream.SaveObject(pMap);
byte[] bytes = pXMLStream.SaveToBytes();

...

//deserialize byte array to Map object
IXMLStream pXMLStream = new XMLStreamClass();
pXMLStream.LoadFromBytes(ref bytes);
IObjectStream pObjStream = new ObjectStreamClass();
pObjStream.Stream = pXMLStream as IStream;
Guid guid = typeof(IMap).GUID;
IMap pMap = pObjStream.LoadObject(ref guid, null) as IMap;
axMapControl1.Map = pMap;
share|improve this answer
    
Thanks Darksanta. I have ever tried your way, but I am want to use another way. I resoved this problem –  taibc Apr 15 '13 at 10:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.