How to store elements of IMap(IGraphicsContainer) as BinaryData format in SQL ?

1697
7
10-16-2012 11:06 PM
Highlighted
by
Occasional Contributor II
Hi,

I am having an IMap on PageLayoutControl that includes elements: Lines, Text,... (Please see attached screenshot).

[ATTACH=CONFIG]18473[/ATTACH]

How I can stores these elements as Binary Data format (Image type) in a SQL table ? And, I can load data from database into IMap, then still can move these elements.

Thanks and regards,
Reply
0 Kudos
7 Replies
Highlighted
by
Occasional Contributor II
Are there anyone can help me ?
Reply
0 Kudos
Highlighted
Occasional Contributor
I belive BinaryData in SQL is nothing but BLOB esri field format.
If so, the elements can be stored into BLOB fields using IPersistStream interface. You can look for an article to find a way to store the data using

IMemoryBlobStream and

IPersistStream interfaces.
Reply
0 Kudos
Highlighted
by
Occasional Contributor II
I belive BinaryData in SQL is nothing but BLOB esri field format.
If so, the elements can be stored into BLOB fields using IPersistStream interface. You can look for an article to find a way to store the data using

IMemoryBlobStream and

IPersistStream interfaces.


Thank you. Could you please give a code example for this ?
Reply
0 Kudos
Highlighted
by
Occasional Contributor II
Hi, I tried to use the below codes to save and load elements of IMap, but I got the error: Error HRESULT E_FAIL has been returned from a call to a COM component
when calling the load function.

Do have anyone know how to fix it ?

public class Test 
{
  IStream stream;
  IMap myMap;
  void Save()
  {
    IPersistStream perStream;
    IMemoryBlobStream memoryStream = new MemoryBlobStreamClass();
    stream = memoryStream as IStream;
    perStream = (IPersistStream ) myMap ;
    perStream.Save(stream, 0); 
  }
  void Load()
  {
     IMemoryBlobStream memoryStream = new MemoryBlobStreamClass();
     IPersistStream pstm = new MapClass(); 
     pstm.Load(stream); **// throw error: Error HRESULT E_FAIL has been returned from a call to a COM component.**

     myMap = (IMap)pstm;    
  }
}
Reply
0 Kudos
Highlighted
by
Occasional Contributor II
Hi,

I found out the way to store elements (of a MapFrame) into Image and load again by using the below codes:

void Save (AxPageLayoutControl pageControl)
{
                IPersistStream perStream;
                IMemoryBlobStream memoryStream = new MemoryBlobStreamClass();
              
                IGraphicsContainer graphicsContainer = pageControl.ActiveView.GraphicsContainer;
                graphicsContainer.Reset();
                IElement pElement = graphicsContainer.Next();
                while (pElement != null)
                {
                    if (pElement is IMapFrame)
                    {
                        perStream = (IPersistStream)pElement ;
                        perStream.Save(memoryStream, 0);
                      
                        memoryStream.SaveToFile("C:/image.bmp");
                        break;
                  
                    }
                    pElement = graphicsContainer.Next();
                }
}

void Load (AxPageLayoutControl pageControl)
{
            IPersistStream perStream;
            IMemoryBlobStream memoryStream = new MemoryBlobStreamClass();
            IGraphicsContainer graphicsContainer = pageControl.ActiveView.GraphicsContainer;
            graphicsContainer.Reset();
            IElement pElement = graphicsContainer.Next();
            while (pElement != null)
            {
                if (pElement is IMapFrame)
                {
                  
                    memoryStream.LoadFromFile("C:/image.bmp");
                    perStream = (IPersistStream)pElement;
                    perStream.Load(memoryStream);
                    IMapFrame frame = new MapFrameClass();
                    frame   = (IMapFrame)perStream;
                    pElement = (IElement)frame;
                    break;
                  
                }               
                pElement = graphicsContainer.Next();
            }
            pageControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}


But, I am finding a way to convert IPersistStream or IMemoryBlobStream to byte[] (binary format), so I can store in the SQL Server and load it later.

Are there anyone know this ?

Thanks and regards,
Reply
0 Kudos
Highlighted
Occasional Contributor III
Hi,

I found out the way to store elements (of a MapFrame) into Image and load again by using the below codes:

void Save (AxPageLayoutControl pageControl)
{
                IPersistStream perStream;
                IMemoryBlobStream memoryStream = new MemoryBlobStreamClass();
              
                IGraphicsContainer graphicsContainer = pageControl.ActiveView.GraphicsContainer;
                graphicsContainer.Reset();
                IElement pElement = graphicsContainer.Next();
                while (pElement != null)
                {
                    if (pElement is IMapFrame)
                    {
                        perStream = (IPersistStream)pElement ;
                        perStream.Save(memoryStream, 0);
                      
                        memoryStream.SaveToFile("C:/image.bmp");
                        break;
                  
                    }
                    pElement = graphicsContainer.Next();
                }
}

void Load (AxPageLayoutControl pageControl)
{
            IPersistStream perStream;
            IMemoryBlobStream memoryStream = new MemoryBlobStreamClass();
            IGraphicsContainer graphicsContainer = pageControl.ActiveView.GraphicsContainer;
            graphicsContainer.Reset();
            IElement pElement = graphicsContainer.Next();
            while (pElement != null)
            {
                if (pElement is IMapFrame)
                {
                  
                    memoryStream.LoadFromFile("C:/image.bmp");
                    perStream = (IPersistStream)pElement;
                    perStream.Load(memoryStream);
                    IMapFrame frame = new MapFrameClass();
                    frame   = (IMapFrame)perStream;
                    pElement = (IElement)frame;
                    break;
                  
                }               
                pElement = graphicsContainer.Next();
            }
            pageControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}


But, I am finding a way to convert IPersistStream or IMemoryBlobStream to byte[] (binary format), so I can store in the SQL Server and load it later.

Are there anyone know this ?

Thanks and regards,


this is another kind of situation, I have been struggling with something like that, but i cannot save the map directly into the database, so i first save the map to a file, and then save the file into the database, it was the workaround that i found to solve it.
Reading your code, you are trying to save a bitmap of the map (a graphic representation) not the map per se....aren't you?
Reply
0 Kudos
Highlighted
by
Occasional Contributor II
Hi,

so i first save the map to a file, and then save the file into the database, it was the workaround that i found to solve it.

Could you show me the codes that you used for this (I tried it, but not successful):


but i cannot save the map directly into the database


For saving directly into the database, I found out a solution (using IXMLStream to save to byte[], then store in database). For example:
IPersistStream perStream;
IXMLStream xmlStream = new XMLStreamClass();
perStream = (IPersistStream)pElement;
IStream stre = (IStream)xmlStream;
perStream.Save(stre, 0);
byte[] map = xmlStream.SaveToBytes();
// insert "map" into database
//.....


Reading your code, you are trying to save a bitmap of the map (a graphic representation) not the map per se....aren't you?


I want to save all elements of IGraphicsContainer into database, then can load later.
Reply
0 Kudos