搞了一个星期终于把符号管理器写好了。用 IStyleGallery ,IStyleGalleryStorage 接口更新符号时总是提示com错误,即使释放资源也不行,后来才发现原来是用arcgis的symbocontrol控件会一直点用样式文件,导致更新时出错,没办法,只能不用arcgis的控件了,只能把style生成图片显示在listview中。
/// <summary>
/// 由symbol创建图片
/// </summary>
/// <param name="pSymbol"></param>
/// <param name="iwidth"></param>
/// <param name="iheight"></param>
/// <param name="styleclass">styleclass类型集名称</param>
/// <returns></returns>
public System.Drawing.Bitmap SymbolToBitmp(ESRI.ArcGIS.Display.ISymbol pSymbol, int iwidth, int iheight, string styleclass)
{
//根据高宽创建图象
Bitmap bmp = new Bitmap(iwidth, iheight);
Graphics gImage = Graphics.FromImage(bmp);
gImage.Clear(Color.White);
double dpi = gImage.DpiX;
IEnvelope pEnvelope = new EnvelopeClass();
pEnvelope.PutCoords(0, 0, (double)bmp.Width, (double)bmp.Height);
tagRECT deviceRect;
deviceRect.left = 0;
deviceRect.right = bmp.Width;
deviceRect.top = 0;
deviceRect.bottom = bmp.Height;
IDisplayTransformation pDisplayTransformation = new DisplayTransformationClass();
pDisplayTransformation.VisibleBounds = pEnvelope;
pDisplayTransformation.Bounds = pEnvelope;
pDisplayTransformation.set_DeviceFrame(ref deviceRect);
pDisplayTransformation.Resolution = dpi;
IGeometry pGeo = CreateSymShape(pSymbol, pEnvelope, styleclass);
System.IntPtr hdc = new IntPtr();
hdc = gImage.GetHdc();
ISimpleTextSymbol symbol1 = pSymbol as ISimpleTextSymbol;
if (symbol1 != null)
{
string text1 = symbol1.Text;
bool flag1 = symbol1.Clip;
symbol1.Text = "AaBbYyZz";
}
//将符号的形状绘制到图象中
pSymbol.SetupDC((int)hdc, pDisplayTransformation);
pSymbol.Draw(pGeo);
pSymbol.ResetDC();
gImage.ReleaseHdc(hdc);
gImage.Dispose();
return bmp;
}
/// <summary>
///
/// </summary>
/// <param name="pSymbol"></param>
/// <param name="pEnvelope"></param>
/// <param name="styleclass"></param>
/// <returns></returns>
private ESRI.ArcGIS.Geometry.IGeometry CreateSymShape(ISymbol pSymbol, IEnvelope pEnvelope, string styleclass)
{
switch (styleclass)
{
case "Marker Symbols":
IArea pArea;
pArea = pEnvelope as IArea;
return pArea.Centroid as IGeometry;
case "Line Symbols":
case "Text Symbols":
//判断是否为“线”符号
ESRI.ArcGIS.Geometry.IPolyline IpLine;
IpLine = new PolylineClass();
IPoint fpt = new PointClass();
IPoint tpt = new PointClass();
fpt.PutCoords(pEnvelope.LowerLeft.X, (pEnvelope.LowerLeft.Y + pEnvelope.UpperRight.Y) / 2);
tpt.PutCoords(pEnvelope.UpperRight.X, (pEnvelope.LowerLeft.Y + pEnvelope.UpperRight.Y) / 2);
IpLine.FromPoint = fpt;
IpLine.ToPoint = tpt;
return IpLine as IGeometry;
case "Fill Symbols":
//直接返回一个IEnvelope矩形区域
return pEnvelope as IGeometry;
default:
return pEnvelope as IGeometry;
}
}