C#在做AE自定义工具时的一个错误——自定义放大工具
2009-08-30 23:51
344 查看
需要动态加载一张图片,程序如下:
base.m_bitmap = new System.Drawing.Bitmap
(GetType().Assembly.GetManifestResourceStream(GetType(), "zoomout.bmp"));
但是调试时总会抛出异常“未处理的“System.ArgumentException”类型的异常出现在 system.drawing.dll 中。其他信息: “null”不是“stream”的有效值。”,显然GetTyp().Assembly.GetManifestResourceStream()方法 并没有找到需要加载的图片。难道是命名空间的问题,
实在让人不得其解,寻思半天。
查了一下资料,发现有人提到Build Action" property should be set to " Embedded Resourse",我突然想到Assembly.GetManifestResourceStream 是从当前程序集加载指定的清单资源,呵呵,我要加载的图片仅仅是放在项目文件夹中并包含在项目中,这个不能算是程序集中的资源阿。赶紧从项目文件列表中找到相应图片,点击图片,修改其操作属性为嵌入的资源,那可。
以下是自定义放大工具
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using System.Drawing;
namespace 自定义工具程序
{
public sealed class ZoomOut : BaseTool
{
IMapControl3 mapControl;
private IHookHelper m_HookHelper = new HookHelperClass();
public ZoomOut()
{
//Set command properties
base.m_caption = "Zoom Out";
base.m_toolTip = "Zoom Out";
base.m_message = "Zoom Out on the Display";
base.m_name = "Generic_ZoomOut";
base.m_bitmap = new System.Drawing.Bitmap
(GetType().Assembly.GetManifestResourceStream(GetType(), "zoomout.bmp"));
}
public override void OnCreate(object hook)
{
m_HookHelper.Hook = hook;
if (hook is IToolbarControl)
{
IToolbarControl toolbarControl = (IToolbarControl)hook;
mapControl = (IMapControl3)toolbarControl.Buddy;
}
}
public override bool Enabled
{
get
{
if (m_HookHelper.FocusMap == null) return false;
return true;
}
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
//Track a rectangle on the active view
IEnvelope trackExtent = mapControl.TrackRectangle();
if (trackExtent == null) return;
//Get the current extent of the active view
IEnvelope currentExtent = mapControl.Extent;
IEnvelope newExtent = null;
//If the tracked envelope is empty
if (trackExtent.IsEmpty)
{
//Expand the current extent
newExtent = currentExtent;
newExtent.Expand(2.0, 2.0, true);
newExtent.CenterAt(mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
else
{
//Create coordinates for the new extent
double dWidth = currentExtent.Width * (currentExtent.Width /
trackExtent.Width);
double dHeight = currentExtent.Height * (currentExtent.Height /
trackExtent.Height);
double dXmin = currentExtent.XMin - ((trackExtent.XMin -
currentExtent.XMin) * (currentExtent.Width / trackExtent.Width));
double dYmin = currentExtent.YMin - ((trackExtent.YMin -
currentExtent.YMin) * (currentExtent.Height / trackExtent.Height));
double dXmax = (currentExtent.XMin - ((trackExtent.XMin -
currentExtent.XMin) * (currentExtent.Width / trackExtent.Width))) + dWidth;
double dYmax = (currentExtent.YMin - ((trackExtent.YMin -
currentExtent.YMin) * (currentExtent.Height / trackExtent.Height))) + dHeight;
//Set the extent coordinates
newExtent = new EnvelopeClass();
newExtent.PutCoords(dXmin, dYmin, dXmax, dYmax);
}
//Set the new extent
mapControl.Extent = newExtent;
}
}
}
base.m_bitmap = new System.Drawing.Bitmap
(GetType().Assembly.GetManifestResourceStream(GetType(), "zoomout.bmp"));
但是调试时总会抛出异常“未处理的“System.ArgumentException”类型的异常出现在 system.drawing.dll 中。其他信息: “null”不是“stream”的有效值。”,显然GetTyp().Assembly.GetManifestResourceStream()方法 并没有找到需要加载的图片。难道是命名空间的问题,
实在让人不得其解,寻思半天。
查了一下资料,发现有人提到Build Action" property should be set to " Embedded Resourse",我突然想到Assembly.GetManifestResourceStream 是从当前程序集加载指定的清单资源,呵呵,我要加载的图片仅仅是放在项目文件夹中并包含在项目中,这个不能算是程序集中的资源阿。赶紧从项目文件列表中找到相应图片,点击图片,修改其操作属性为嵌入的资源,那可。
以下是自定义放大工具
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using System.Drawing;
namespace 自定义工具程序
{
public sealed class ZoomOut : BaseTool
{
IMapControl3 mapControl;
private IHookHelper m_HookHelper = new HookHelperClass();
public ZoomOut()
{
//Set command properties
base.m_caption = "Zoom Out";
base.m_toolTip = "Zoom Out";
base.m_message = "Zoom Out on the Display";
base.m_name = "Generic_ZoomOut";
base.m_bitmap = new System.Drawing.Bitmap
(GetType().Assembly.GetManifestResourceStream(GetType(), "zoomout.bmp"));
}
public override void OnCreate(object hook)
{
m_HookHelper.Hook = hook;
if (hook is IToolbarControl)
{
IToolbarControl toolbarControl = (IToolbarControl)hook;
mapControl = (IMapControl3)toolbarControl.Buddy;
}
}
public override bool Enabled
{
get
{
if (m_HookHelper.FocusMap == null) return false;
return true;
}
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
//Track a rectangle on the active view
IEnvelope trackExtent = mapControl.TrackRectangle();
if (trackExtent == null) return;
//Get the current extent of the active view
IEnvelope currentExtent = mapControl.Extent;
IEnvelope newExtent = null;
//If the tracked envelope is empty
if (trackExtent.IsEmpty)
{
//Expand the current extent
newExtent = currentExtent;
newExtent.Expand(2.0, 2.0, true);
newExtent.CenterAt(mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
else
{
//Create coordinates for the new extent
double dWidth = currentExtent.Width * (currentExtent.Width /
trackExtent.Width);
double dHeight = currentExtent.Height * (currentExtent.Height /
trackExtent.Height);
double dXmin = currentExtent.XMin - ((trackExtent.XMin -
currentExtent.XMin) * (currentExtent.Width / trackExtent.Width));
double dYmin = currentExtent.YMin - ((trackExtent.YMin -
currentExtent.YMin) * (currentExtent.Height / trackExtent.Height));
double dXmax = (currentExtent.XMin - ((trackExtent.XMin -
currentExtent.XMin) * (currentExtent.Width / trackExtent.Width))) + dWidth;
double dYmax = (currentExtent.YMin - ((trackExtent.YMin -
currentExtent.YMin) * (currentExtent.Height / trackExtent.Height))) + dHeight;
//Set the extent coordinates
newExtent = new EnvelopeClass();
newExtent.PutCoords(dXmin, dYmin, dXmax, dYmax);
}
//Set the new extent
mapControl.Extent = newExtent;
}
}
}
相关文章推荐
- 一个很简单的SqlServer生成常用C#语句工具的诞生
- c#保存文件的一个自定义方法
- 第三周C#技术支持训练(二)自定义一个整数,输出该数分别于1-10相乘的结果
- HOW TO:使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- 错误 4 自定义工具错误: 无法生成服务引用“DepartMentService”的代码。请检查其他错
- 我写的一个简易文章抓取工具(C#)
- 记录C#错误日志工具
- 开发一个小工具重温C#经典问题
- 今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
- 一个自定义LABEL组件的C#源代码
- Facebook创建了一个基于人工智能的工具来自动修复错误
- 中国PHP程序员都犯的一个错误:使用错误的自定义insert_id函数
- C#软件开发实例.私人订制自己的屏幕截图工具(九)使用自定义光标,QQ截图时的光标
- 发现一个超强的C#反编译工具 Reflector
- c# 写的一个xml配置工具,自我感觉很强大(wpf)
- ArcEngine (C#)自定义命令和工具
- C#自定义一个时间类
- AStyle 2.02版本 AStyle(全称Artistic Style)是一个C、C++、C#和Java源代码缩进、格式化和美化工具
- AE自定义工具时通过hook获取axMapControl
- C#Winform基础 自己做一个自定义的button控件