2,142
社区成员
发帖
与我相关
我的任务
分享
public void ReloadMap()
{
if (MapConfig != null)
{
if (MapConfig.DefaultLevel == (int)ScaleLevel.Value)
{
#region
MapExtend extend = new MapExtend();
extend.XMin = MapConfig.Extend[0];
extend.XMax = MapConfig.Extend[2];
extend.YMin = MapConfig.Extend[1];
extend.YMax = MapConfig.Extend[3];
if (MapConfig.X < extend.XMin)
{
MapConfig.X = extend.XMin;
}
if (MapConfig.Y < extend.YMin)
{
MapConfig.Y = extend.YMin;
}
if (MapConfig.X > extend.XMax)
{
MapConfig.X = extend.XMax;
}
if (MapConfig.Y > extend.YMax)
{
MapConfig.Y = extend.YMax;
}
int level = MapConfig.DefaultLevel;
double Resolution = MapConfig.Resolutions[level];
int startc = MapConfig.GetColumn(MapConfig.X, Resolution);
int startr = MapConfig.GetRow(MapConfig.Y, Resolution);
int endc = MapConfig.GetColumn(MapConfig.X + 10 * 256 * Resolution, Resolution);
int endr = MapConfig.GetRow(MapConfig.Y - 10 * 256 * Resolution, Resolution);
if (endc > MapConfig.GetColumn(extend.XMax, Resolution))
{
endc = MapConfig.GetColumn(extend.XMax, Resolution);
}
if (endr > MapConfig.GetRow(extend.YMin, Resolution))
{
endr = MapConfig.GetRow(extend.YMin, Resolution);
}
#endregion
Bitmap bitmap = new Bitmap(640, 640);
for (int i = startc; i <= endc; i++)
{
int column = i - startc;
for (int j = startr; j <= endr; j++)
{
Image image = null;
HttpWebRequest req = null;
HttpWebResponse res = null;
System.IO.Stream stream = null;
Graphics targetgraphic = null;
try
{
int row = j - startr;
string picurl = string.Format(MapConfig.MapUrl, MapConfig.DefaultLevel, j, i);
req = HttpWebRequest.Create(picurl) as System.Net.HttpWebRequest;
req.AllowAutoRedirect = true;
req.KeepAlive = false;
req.Timeout = 10000;
req.ServicePoint.ConnectionLeaseTimeout = 10000;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
req.Headers.Add("Accept-Encoding: gzip, deflate, sdch");
req.Headers.Add("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4");
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0";
using (res = req.GetResponse() as System.Net.HttpWebResponse)
{
using (stream = res.GetResponseStream())
{
image = Image.FromStream(stream);
targetgraphic = Graphics.FromImage(bitmap);
targetgraphic.DrawImage(image, column * 64, row * 64, 64, 64);
if (image != null)
{
image.Dispose();
image = null;
}
if (targetgraphic != null)
{
targetgraphic.Flush();
targetgraphic.Dispose();
targetgraphic = null;
}
stream.Close();
stream.Dispose();
stream = null;
}
res.Close();
res = null;
}
}
catch
{
}
finally
{
if (targetgraphic != null)
{
targetgraphic.Dispose();
targetgraphic = null;
}
if (image != null)
{
image.Dispose();
image = null;
}
if (stream != null)
{
stream.Close();
stream.Dispose();
stream = null;
}
if (res != null)
{
res.Close();
res = null;
}
if (req != null)
{
req.Abort();
req = null;
}
}
MapContainer.Image = Image.FromHbitmap(bitmap.GetHbitmap());
}
}
if (bitmap != null)
{
bitmap.Dispose();
bitmap = null;
}
}
GC.Collect();
}
}