如何生成IStyleGalleryItem和ISymbol对象的预览图(转载)

http://blog.csdn.net/giselite/article/details/8451877

来看一下ArcMap的符号选择器:

ArcMap的符号选择器都提供了符号的预览图,另一个预览图的位置是在按钮上,比如设置MapGrid的格网交点符号和格网线符号。

本文给出生成这里的符号预览图的代码。

方法1:通过ISymbologyStyleClass的Preview方法生成预览图

[csharp]  view plain copy
  1. private string routin_ReadRegistry(string sKey)  
  2. {  
  3.     Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey, true);  
  4.     if (rk == null) return "";  
  5.     return (string)rk.GetValue("InstallDir");  
  6. }  
  7.   
  8.   
  9. public void LoadStyleGalleryItems()  
  10. {  
  11.     string sInstall = routin_ReadRegistry("SOFTWARE\\ESRI\\CoreRuntime");  
  12.     if (sInstall == "")  
  13.     {  
  14.         return;  
  15.     }  
  16.   
  17.     ISymbologyControl pSymbologyControl = new SymbologyControlClass();  
  18.     pSymbologyControl.LoadStyleFile(sInstall + "\\Styles\\ESRI.ServerStyle");  
  19.     m_pSymbologyStyleClass = pSymbologyControl.GetStyleClass(esriSymbologyStyleClass.esriStyleClassScaleBars);  
  20.   
  21.     //Clean items in ImageComboBox and images in ImageCollection.  
  22.     this.imageComboBoxEdit1.Properties.Items.Clear();  
  23.     this.imageCollection1.Images.Clear();  
  24.   
  25.     int nWidth, nHeight;  
  26.     nWidth = this.imageCollection1.ImageSize.Width;  
  27.     nHeight = this.imageCollection1.ImageSize.Height;  
  28.   
  29.     Image pImage = null;  
  30.   
  31.     //Load StyleGalleryItems into he ImageComboBox's drop down list.  
  32.     IStyleGalleryItem pStyleGalleryItem = null;  
  33.     IPictureDisp pPicture = null;  
  34.     for (int i = 0; i < m_pSymbologyStyleClass.get_ItemCount(m_pSymbologyStyleClass.StyleCategory); i++)  
  35.     {  
  36.         pStyleGalleryItem = m_pSymbologyStyleClass.GetItem(i);  
  37.         pPicture = m_pSymbologyStyleClass.PreviewItem(pStyleGalleryItem, nWidth, nHeight);  
  38.         pImage = System.Drawing.Image.FromHbitmap(new IntPtr(pPicture.Handle));  
  39.         AddItem(pStyleGalleryItem.Name, pImage, pStyleGalleryItem, i);  
  40.     }  
  41.     this.imageComboBoxEdit1.SelectedIndex = 0;  
  42. }  
  43.   
  44. private void AddItem(string sDescription, Image pImage, IStyleGalleryItem pStyleGalleryItem, int nImageIndex)  
  45. {  
  46.     this.imageComboBoxEdit1.Properties.Items.Add(new ImageComboBoxItem(sDescription, pStyleGalleryItem, nImageIndex));  
  47.     this.imageCollection1.Images.Add(pImage);  
  48. }  
  49. //其中m_pSymbologyStyleClass是ISymbologyStyleClass的实例。  
  50.   
  51. //上面的代码演示如何使用ISymbologyControl接口在下拉列表控件中加载符号集,其中包含生成预览图的代码,对于符号选择器,使用SymbologyControl,可以直接参考AE范例里的符号选择器的例子,下面是符号选择器里更为精简的代码:  
  52. protected void PreviewImage()  
  53. {  
  54.     ISymbologyStyleClass pSymbologyStyleClass = wndSymbologyControl.GetStyleClass(wndSymbologyControl.StyleClass);  
  55.     IPictureDisp pPicture = pSymbologyStyleClass.PreviewItem(m_StyleGalleryItem, wndPictureEdit.Width - 10, wndPictureEdit.Height - 10);  
  56.     Image pImage = Image.FromHbitmap(new IntPtr(pPicture.Handle));  
  57.     wndPictureEdit.Image = pImage;  
  58. }  
  59.   
  60. protected void LoadStyleCalss(esriSymbologyStyleClass styleClass)  
  61. {  
  62.     string sInstall = routin_ReadRegistry("SOFTWARE\\ESRI\\CoreRuntime");  
  63.     wndSymbologyControl.LoadStyleFile(sInstall + "\\Styles\\ESRI.ServerStyle");  
  64.     wndSymbologyControl.StyleClass = styleClass;  
  65.     m_SymbologyStyleClass = wndSymbologyControl.GetStyleClass(wndSymbologyControl.StyleClass);  
  66.     m_SymbologyStyleClass.SelectItem(0);  
  67. }  

方法2:通过IStyleGalleryClass的Preview方法获取符号的预览图

[csharp]  view plain copy
  1. private Image PreViewMarkerSymbol(ISymbol pSymbol, int width, int height)  
  2. {  
  3.     IStyleGalleryClass pStyleGalleryClass = new MarkerSymbolStyleGalleryClass();  
  4.     Image img = new Bitmap(width, height);  
  5.     Graphics gc = Graphics.FromImage(img);  
  6.     IntPtr hdc = gc.GetHdc();  
  7.     tagRECT rect = new tagRECT();  
  8.     rect.left = 0;  
  9.     rect.top = 0;  
  10.     rect.right = width;  
  11.     rect.bottom = height;  
  12.     pStyleGalleryClass.Preview(pSymbol, hdc.ToInt32(), ref rect);  
  13.     gc.ReleaseHdc(hdc);  
  14.     gc.Dispose();  
  15.     return img;  
  16. }  
  17.   
  18.   
  19. private void checkEdit2_CheckedChanged(object sender, EventArgs e)  
  20. {  
  21.     if (m_bLoading) return;  
  22.     if (mMapGrid != null)  
  23.     {  
  24.         if (checkEdit2.Checked)  
  25.         {  
  26.             mMapGrid.LineSymbol = null;  
  27.             ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbol();  
  28.             pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCross;  
  29.             IMarkerSymbol pMarkerSymbol = pSimpleMarkerSymbol as IMarkerSymbol;  
  30.             mMapGrid.TickMarkSymbol = pMarkerSymbol;  
  31.             mMapGrid.SetLabelVisibility(true, true, true, true);  
  32.             simpleButton1.Image = PreViewMarkerSymbol(pMarkerSymbol as ISymbol, simpleButton1.Height, simpleButton1.Width);  
  33.             if (ValueChanged != null) ValueChanged();  
  34.         }  
  35.     }  
  36. }  

使用第二种方法时,要注意ISymbol对象的类型。因为所有符号集类都实现了IStyleGalleryClass接口,如MarkerSymbolStyleGalleryClass、LineSymbolStyleGalleryClass、ScaleBarStyleGalleryClass等。生成预览图时,各符号的预览图只能由与其对应的StyleGalleryClass来生成,也就是说NorthArrow类型的符号只能用NorthArrowStyleGalleryClass来预览,而不能用其它类型,像下面这样是会发生异常的:

[csharp]  view plain copy
  1. private void checkEdit3_CheckedChanged(object sender, EventArgs e)  
  2. {  
  3.     if (m_bLoading) return;  
  4.     if (mMapGrid != null)  
  5.     {  
  6.         if (checkEdit3.Checked)  
  7.         {  
  8.             ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();  
  9.             pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;  
  10.             ILineSymbol pLineSymbol = pSimpleLineSymbol as ILineSymbol;  
  11.             mMapGrid.LineSymbol = pLineSymbol;  
  12.             mMapGrid.TickMarkSymbol = null;  
  13.             mMapGrid.SetLabelVisibility(true, true, true, true);  
  14.             simpleButton1.Image = PreViewMarkerSymbol(pLineSymbol as ISymbol, simpleButton1.Height, simpleButton1.Width);  
  15.             if (ValueChanged != null) ValueChanged();  
  16.         }  
  17.     }  
  18. }  

这里,传入的是LineSymbol对象,在PreViewMarkerSymbol中,用MarkerSymbolStyleGalleryClass的事例来预览一个LineSymbol,程序在调用Preview时,会直接崩溃,正确的用法是:

[csharp]  view plain copy
  1. private Image PreViewLineSymbol(ISymbol pSymbol, int width, int height)  
  2. {  
  3.     IStyleGalleryClass pStyleGalleryClass = new LineSymbolStyleGalleryClass();  
  4.     Image img = new Bitmap(width, height);  
  5.     Graphics gc = Graphics.FromImage(img);  
  6.     IntPtr hdc = gc.GetHdc();  
  7.     tagRECT rect = new tagRECT();  
  8.     rect.left = 0;  
  9.     rect.top = 0;  
  10.     rect.right = width;  
  11.     rect.bottom = height;  
  12.     pStyleGalleryClass.Preview(pSymbol, hdc.ToInt32(), ref rect);  
  13.     gc.ReleaseHdc(hdc);  
  14.     gc.Dispose();  
  15.     return img;  
  16. }  
  17.   
  18.   
  19. private void checkEdit3_CheckedChanged(object sender, EventArgs e)  
  20. {  
  21.     if (m_bLoading) return;  
  22.     if (mMapGrid != null)  
  23.     {  
  24.         if (checkEdit3.Checked)  
  25.         {  
  26.             ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();  
  27.             pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;  
  28.             ILineSymbol pLineSymbol = pSimpleLineSymbol as ILineSymbol;  
  29.             mMapGrid.LineSymbol = pLineSymbol;  
  30.             mMapGrid.TickMarkSymbol = null;  
  31.             mMapGrid.SetLabelVisibility(true, true, true, true);  
  32.             simpleButton1.Image = PreViewLineSymbol(pLineSymbol as ISymbol, simpleButton1.Height, simpleButton1.Width);  
  33.             if (ValueChanged != null) ValueChanged();  
  34.         }  
  35.     }  
  36. }  

实际上IStyleGalleryItem的Item成员,都是ISymbol成员,因此一个ISymbol对象,除了可以直接用IStyleGalleryClass的Previe预览以外,还可以转换成IStyleGalleryItem然后使用ISymbologyStyleClass的Preview方法来预览,比如:

IStyleGalleryItem pStyleGalleryItem = new ServerStyleGalleryItemClass(); IClone pClone = pScaleBar as IClone; pStyleGalleryItem.Item = pClone.Clone();

之后就可以使用第一种方法来生成预览图了。

下面是自制的符号选择器:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值