- 积分
- 567
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2015-10-30 23:03:15
|
显示全部楼层
这是我的代码,王老师
MeteoInfoC.Data.GridData grdd = new MeteoInfoC.Data.GridData();
MeteoInfoC.Legend.LegendScheme aLs = new LegendScheme(MeteoInfoC.Shape.ShapeTypes.Image);
MeteoInfoC.Layer.RasterLayer rslyr = new MeteoInfoC.Layer.RasterLayer();
string imgFname = ".\\Map\\GLOBALeb3colshade.jpg";
Bitmap curBitmap = (Bitmap)Bitmap.FromFile(imgFname);
//read image
if (curBitmap != null)
{
int width = curBitmap.Width;
int height = curBitmap.Height;
BitmapData data = curBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
System.IntPtr Scan0 = data.Scan0;
int stride = data.Stride;
int length = height * 3 * width;
byte[] RGB = new byte[length];
int[,] R = new int[height, width];
int[,] G = new int[height, width];
int[,] B = new int[height, width];
int[, ,] tempColorarray = new int[256, 256, 256];
System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length);
grdd = new MeteoInfoC.Data.GridData(0, 360 / width, width, -90, 180 / height, height);//set grdd by image size
unsafe
{
byte* p = (byte*)Scan0;
//int offset = stride - width * 3;
int iPoint = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
B[i, j] = Convert.ToInt32(RGB[iPoint++]);
G[i, j] = Convert.ToInt32(RGB[iPoint++]);
R[i, j] = Convert.ToInt32(RGB[iPoint++]);
tempColorarray[R[i,j],G[i,j],B[i,j]]=R[i,j]+G[i,j]*256+B[i,j]*256*256;
grdd.Data[i,j]=tempColorarray[R[i,j],G[i,j],B[i,j]];
}
}
}
curBitmap.UnlockBits(data);
// get differect colors
List<MeteoInfoC.Legend.ColorBreak> lstColorB = new List<ColorBreak>();
int tempR, tempG, tempB;
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
{
for (int k = 0; k < 256; k++)
{
if (tempColorarray[i,j,k]!=0)
{
tempB = tempColorarray[i, j, k] / 256 / 256;
tempG = (tempColorarray[i, j, k] - tempB * 256 * 256) / 256;
tempR = (tempColorarray[i, j, k] - tempB * 256 * 256 - tempG * 256);
ColorBreak cb = new ColorBreak();
if (lstColorB.Count==0)
{
cb.StartValue = 0;
}
else
{
cb.StartValue = lstColorB[lstColorB.Count - 1].EndValue;
}
cb.EndValue = lstColorB.Count + 1;
cb.Color = Color.FromArgb(tempR, tempG, tempB);
lstColorB.Add(cb);
}
}
}
}
aLs.LegendBreaks=lstColorB;
List<Color> lstC=aLs.GetColors();
var tmp = new HashSet<Color>();
List<int> iC = new List<int>();
for (int i = 0; i < lstC.Count; i++)
{
iC.Add( (int)lstC.R + (int)(lstC.G) * 256 + (int)(lstC.B) * 256 * 256);
}
// set grdd.Data value
DateTime dt = DateTime.Now;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int value=Convert.ToInt32(grdd.Data[i, j]);
grdd.Data[i, j] = Convert.ToDouble(iC.IndexOf(value)+1);//这一步很耗时,需要后续优化
}
}
TimeSpan ts = DateTime.Now - dt;
rslyr = MeteoInfoC.Data.MeteoData.DrawMeteoData.CreateRasterLayer(grdd, "myMap", aLs);
G_LayerLegend.ActiveMapFrame.AddLayer(rslyr);
G_LayerLegend.ActiveMapFrame.MapView.PaintLayers();
}同时,检查了一下,从图片中读取出的颜色有380823种,不知道为何有这么多?如果真有这么多,那么想这样现实地图图片是不是不可行呀?
在执行到这一行rslyr = MeteoInfoC.Data.MeteoData.DrawMeteoData.CreateRasterLayer(grdd, "myMap", aLs);时出错了
错误信息为:
未处理System.NullReferenceException
Message=未将对象引用设置到对象的实例。
Source=MeteoInfoC
StackTrace:
在 MeteoInfoC.Layer.ImageLayer.SetPalette(List`1 colors)
在 MeteoInfoC.Layer.RasterLayer.SetPaletteByLegend()
在 MeteoInfoC.Layer.RasterLayer.set_LegendScheme(LegendScheme value)
在 MeteoInfoC.Data.MeteoData.DrawMeteoData.CreateRasterLayer(GridData GridData, String LName, LegendScheme aLS)
在 WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) 位置 d:\lzf\project\WeatherDislay\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:行号 155
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsFormsApplication1.Program.Main() 位置 d:\lzf\project\WeatherDislay\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
不知道何故?
|
|