自己写了个加载瓦片地图的例子,资源释放出现问题了,各位能帮忙看下么

a49990913 2016-03-24 04:28:08
自己写了个加载瓦片地图的例子,纯粹通过httprequst请求瓦片跟bitmap重绘瓦片实现的。但是发现在实际跑起来后,内存铛铛铛得往上涨啊,也不晓得问题出在哪了?哪位高人指点下...这是主要的加载瓦片的代码段

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();
}
}

这是例子地址http://download.csdn.net/detail/a49990913/9471596
...全文
959 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
a49990913 2016-03-28
  • 打赏
  • 举报
回复
发现原因了,原因是使用了Image.FromHbitmap(bitmap.GetHbitmap()),不晓得什么原因使用该方法内存越来越大,并没有被释放。修改后的例子连接http://download.csdn.net/detail/a49990913/9474220,结贴了。

2,142

社区成员

发帖
与我相关
我的任务
社区描述
它是一种特定的十分重要的空间信息系统。它是在计算机硬、软件系统支持下,对整个或部分地球表层(包括大气层)空间中的有关地理分布数据进行采集、储存、管理、运算、分析、显示和描述的技术系统。
社区管理员
  • 地理信息系统
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧