阅读:1602回复:2
[求助]AE中存入图片的问题,谢谢了
<P> 大家好!</P>
<P> 初接触GIS,在应用中遇到这样一个问题,就是每个要素都需要一张图片,当用户点击要素的时候,显示图片,做数据库的在在FeatureClass表里建了个字段是Blob类型的,现在想问下,怎么把图片 存进去,怎么取出来 </P> <P>我的代码如下:</P> <P> void mapControl_OnMouseDown(int button, int shift, int X, int Y, double mapX, double mapY)<BR> {<BR> try<BR> {<BR> if (button == LEFT_BUTTON)<BR> {<BR> IPoint point = new PointClass();<BR> point.PutCoords(mapX, mapY);<BR> IFeature feature = featureGainer.GetFeatureByPositionFromMap(mapControl.Map<BR> , layers, point);<BR> int index = -1;<BR> if (feature != null<BR> ;; (index = feature.Fields.FindField(INNER_PICTURE_FIELD)) != -1)<BR> {<BR> // 上传图片<BR> object ob = feature.get_Value(index);</P> <P> MessageBox.Show("" + feature.get_Value(index) + ob.GetType());<BR> IWorkspaceEdit workspace = DataHelper.GetWorkspace(feature);<BR> if (workspace.IsBeingEdited() <BR> ;; feature.get_Value(index) == System.DBNull.Value)<BR> {<BR> if (MessageBox.Show("该分支箱没有内部接线图,是否上传?", "消息"<BR> , MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)<BR> {<BR> if (this.openFileDialog1.ShowDialog() == DialogResult.OK)<BR> {<BR> //FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);<BR> Stream fs = this.openFileDialog1.OpenFile();<BR> byte[] fileBuffer = new byte[fs.Length];<BR> fs.Read(fileBuffer, 0, fileBuffer.Length);<BR> //SqlBinary binary = new SqlBinary(fileBuffer);<BR> SqlBytes sbs = new SqlBytes(fileBuffer);<BR> workspace.StartEditOperation();<BR> feature.set_Value(index, sbs);<BR> feature.Store();<BR> workspace.StopEditOperation();<BR> MessageBox.Show("图片上传成功");<BR> }<BR> }<BR> return;<BR> }<BR> SqlBytes outsbs = (SqlBytes)(feature.get_Value(index));<BR> byte[] buffer = outsbs.Buffer;<BR> if (outsbs.Length == 0 || buffer.Length == 0)<BR> {<BR> MessageBox.Show("对不起,没有内部接线图");<BR> return;<BR> }<BR> MemoryStream ms = new MemoryStream(buffer);<BR> Bitmap bitmap = new Bitmap(ms);<BR> string featureInfo = "分支箱:" + feature.OID + " 内部接线图";<BR> this.ShowInfo(featureInfo, bitmap);<BR> }<BR> }<BR> }<BR> catch (COMException ex)<BR> {</P> <P> MessageBox.Show(ex.StackTrace + "\n" + ex.ErrorCode);<BR> }<BR> }</P> <P>点击获得要素是没问题的,但是就是存不进去,每次到 workspace.StopEditOperation();</P> <P>就抛出COMException,请问下,大家有解决方法么,谢谢了</P> |
|
1楼#
发布于:2007-07-19 15:10
<P>下面是个 例子,可以 参考 ,blog字段可以保存 图片,也可以 保存 任何 的 文件格式 的 文件 </P>
<P>The code below below shows how to save pictures to blobs ; how a picture in a blob can be used to populate a picture element on a layout. <BR> Sub Test()<BR> <BR> Dim pBlobTable As ITable<BR> ' (creates it if it doesn't already exist)<BR> Set pBlobTable = GetBlobTable("D:\arcgis\sampledata\MyGDB.mdb", "MyBlobs")<BR> If pBlobTable Is Nothing Then Exit Sub<BR> <BR> Dim pPictDisp As IPictureDisp<BR> Set pPictDisp = LoadPicture("D:\Pictureblobs.bmp")<BR> <BR> ' save the picture to the blob<BR> Picture2Blob pBlobTable, pPictDisp, "MyNewPicture"<BR> <BR> ' restore a previously saved picture<BR> Set pPictDisp = Blob2Picture(pBlobTable, "MyPicture")<BR> <BR> ' add the picture to the layout<BR> If pPictDisp Is Nothing Then<BR> Debug.Print "unable to load picture from blob"<BR> Else<BR> Dim pEnv As IEnvelope<BR> Set pEnv = New Envelope<BR> pEnv.PutCoords 1, 1, 2, 2 ' location on the layout for the element<BR> Dim pMxDoc As IMxDocument<BR> Set pMxDoc = ThisDocument<BR> AddPictureToLayout pPictDisp, pMxDoc.PageLayout, pEnv<BR> End If<BR> <BR>End Sub</P> <P>Sub AddPictureToLayout(pPictDisp As IPictureDisp, pGC As IGraphicsContainer, _<BR> pEnv As IEnvelope)<BR> ' there's probably a better way to do this where<BR> ' you don't write it to a file<BR> SavePicture pPictDisp, "C:\temp\junk.bmp"<BR> Dim pPictElement As IPictureElement<BR> Set pPictElement = New BmpPictureElement<BR> pPictElement.ImportPictureFromFile "C:\temp\junk.bmp"<BR> pPictElement.SavePictureInDocument = True<BR> pPictElement.MaintainAspectRatio = True<BR> Dim dEnvAspRatio As Double<BR> dEnvAspRatio = pEnv.Width / pEnv.Height<BR> <BR> If pPictElement.PictureAspectRatio > dEnvAspRatio Then<BR> pEnv.Expand pPictElement.PictureAspectRatio / dEnvAspRatio, 1#, True<BR> Else<BR> pEnv.Expand 1#, dEnvAspRatio / pPictElement.PictureAspectRatio, True<BR> End If</P> <P> Dim pElement As IElement<BR> Set pElement = pPictElement<BR> pElement.Geometry = pEnv<BR> pGC.AddElement pElement, 0<BR> Dim pAV As IActiveView<BR> Set pAV = pGC<BR> pAV.PartialRefresh esriViewGraphics, pElement, Nothing<BR>End Sub</P> <P>Function Blob2Picture(pBlobTable As ITable, strName As String) As IPictureDisp<BR> ' using Name as a key, retrieve the picture from the blob field<BR> <BR> Dim pRow As IRow<BR> Set pRow = GetBlobRow(pBlobTable, strName, False)<BR> If Not pRow Is Nothing Then<BR> Dim pMemBlobStream As IMemoryBlobStream<BR> Set pMemBlobStream = pRow.Value(pRow.Fields.FindField("Blob"))<BR> <BR> Dim pPersistStream As IPersistStream<BR> Set pPersistStream = New StdPicture<BR> <BR> pPersistStream.Load pMemBlobStream<BR> Set Blob2Picture = pPersistStream<BR> Else<BR> Debug.Print "no row found for : " ; strName<BR> Set Blob2Picture = Nothing<BR> End If<BR>End Function</P> <P>Function GetBlobTable(strPath As String, strName As String) As ITable<BR> Dim pWSF As IWorkspaceFactory<BR> Set pWSF = New AccessWorkspaceFactory<BR> <BR> Dim pFWS As IFeatureWorkspace<BR> Set pFWS = pWSF.OpenFromFile(strPath, 0)<BR> <BR> On Error Resume Next<BR> Set GetBlobTable = pFWS.OpenTable(strName)<BR> If GetBlobTable Is Nothing Then<BR> Set GetBlobTable = CreateBlobTable(strPath, strName)<BR> End If<BR>End Function</P> <P>Sub Picture2Blob(pBlobTable As ITable, pPictDisp As IPictureDisp, strName As String)<BR> ' saves the picture to a blob<BR> <BR> Dim pMemBlobStream As IMemoryBlobStream<BR> Set pMemBlobStream = New MemoryBlobStream<BR> <BR> Dim pPersistStream As IPersistStream<BR> Set pPersistStream = pPictDisp<BR> pPersistStream.Save pMemBlobStream, False<BR> <BR> Dim pRow As IRow<BR> Set pRow = GetBlobRow(pBlobTable, strName, True)<BR> pRow.Value(pRow.Fields.FindField("Name")) = strName<BR> pRow.Value(pRow.Fields.FindField("Blob")) = pMemBlobStream<BR> pRow.Store<BR> <BR>End Sub</P> <P>Function GetBlobRow(pBlobTable As ITable, strName As String, _<BR> Optional bCreate As Boolean = False) As IRow<BR> Dim pQF As IQueryFilter<BR> Set pQF = New QueryFilter<BR> pQF.WhereClause = "[Name] = '" ; strName ; "'"<BR> Set GetBlobRow = pBlobTable.Search(pQF, False).NextRow<BR> ' if no row was found ...<BR> If GetBlobRow Is Nothing And bCreate Then<BR> Set GetBlobRow = pBlobTable.CreateRow ' ... then create one<BR> End If<BR>End Function</P> <P>Function CreateBlobTable(strPath As String, strName As String) As ITable<BR> Dim pWSF As IWorkspaceFactory<BR> Set pWSF = New AccessWorkspaceFactory<BR> <BR> Dim pFWS As IFeatureWorkspace<BR> Set pFWS = pWSF.OpenFromFile(strPath, 0)<BR> <BR> On Error Resume Next<BR> If pFWS.OpenTable(strName) Is Nothing Then<BR> Dim pCLSID As New UID<BR> pCLSID.Value = "esriCore.Object"<BR> Set CreateBlobTable = pFWS.Createtable(strName, MakeFields, _<BR> pCLSID, Nothing, "")<BR> Else<BR> ' don't create it if it already exists, just open it<BR> Set CreateBlobTable = pFWS.OpenTable(strName)<BR> End If<BR>End Function</P> <P>Function MakeFields() As IFields<BR> Dim pFldsEdit As IFieldsEdit<BR> Set pFldsEdit = New Fields<BR> <BR> Dim pFldEdit As IFieldEdit<BR> ' make the OID field<BR> Set pFldEdit = New Field<BR> With pFldEdit<BR> .IsNullable = False<BR> .Name = "OBJECTID"<BR> .Type = esriFieldTypeOID<BR> .Editable = True<BR> .Required = True<BR> End With<BR> pFldsEdit.AddField pFldEdit<BR> <BR> ' make the Name field<BR> Set pFldEdit = New Field<BR> pFldEdit.Name = "Name"<BR> pFldEdit.Type = esriFieldTypeString<BR> pFldEdit.Length = 50<BR> pFldsEdit.AddField pFldEdit<BR> ' make the blob field<BR> Set pFldEdit = New Field<BR> pFldEdit.Name = "Blob"<BR> pFldEdit.Type = esriFieldTypeBlob<BR> 'pfldedit.Length = ???<BR> pFldsEdit.AddField pFldEdit<BR> Set MakeFields = pFldsEdit<BR>End Function</P> <P> <BR></P> |
|
2楼#
发布于:2007-07-20 11:36
要是C#的例子就好了
|
|