Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            _path = txtPath.Text;
            if (_path.Equals(""))
            {
                MessageBox.Show("请选择存储路径!");
                return;
            }

            int zoom   = (int)numZoom.Value;
            int thread = (int)numThread.Value;

            PointF point1 = MapHelper.GetLocationByLatLng(p1, zoom); //将第一个点经纬度转换成平面2D坐标
            PointF point2 = MapHelper.GetLocationByLatLng(p2, zoom); //将第二个点经纬度转换成平面2D坐标

            int startX = (int)point1.X / 256;                        //起始列
            int endX   = (int)point2.X / 256;                        //结束列

            if (endX == Math.Pow(2, zoom))                           //结束列超出范围
            {
                endX--;
            }
            int startY = (int)point1.Y / 256; //起始行
            int endY   = (int)point2.Y / 256; //结束行

            if (endY == Math.Pow(2, zoom))    //结束行超出范围
            {
                endY--;
            }

            _totalwidth  = (endX - startX + 1) * 256; //合并图的宽度
            _totalheight = (endY - startY + 1) * 256; //合并图的高度

            int threadId = 0;

            _waittodownload.Clear();
            for (int y = startY; y <= endY; y++)
            {
                for (int x = startX; x <= endX; x++)
                {
                    RectInfo ri = new RectInfo();
                    ri.threadId  = threadId; //分别由不同的线程下载
                    ri.x         = x;
                    ri.y         = y;
                    ri.z         = zoom;
                    ri.bComplete = false;
                    _waittodownload.Add(ri);            //将每个小方块放入待下载集合
                    threadId = (threadId + 1) % thread; //由thread个不同线程下载图片
                }
            }

            if (MessageBox.Show("共有" + _waittodownload.Count + "张图片需要下载,确定下载吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
            {
                _thread      = thread;
                _zoom        = zoom;
                _downloadnum = 0;

                _normal         = radioButton1.Checked;
                button2.Enabled = false;
                rchOuput.Clear();
                groupBox2.Text = "输出(" + _waittodownload.Count + "张)";
                _startTime     = DateTime.Now;
                for (int i = 1; i <= thread; ++i)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(DownloadThreadProc));
                    t.Start(i);  //开启下载线程
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 下载线程方法
        /// </summary>
        /// <param name="param"></param>
        public void DownloadThreadProc(object param)
        {
            int        threadId   = (int)param; //当前线程Id
            MapService mapService = new MapService();

            this.Invoke((Action) delegate()  //输出
            {
                rchOuput.SelectionColor = Color.Blue;
                rchOuput.AppendText(DateTime.Now.ToLongTimeString() + " 第" + threadId + "号线程开始执行\r\n");
            });
            for (int i = 0; i < _waittodownload.Count; i++)
            {
                RectInfo ri = (RectInfo)_waittodownload[i];
                if ((!ri.bComplete) && (ri.threadId + 1 == threadId))
                {
                    try
                    {
                        Bitmap map  = mapService.LoadMapTile(ri.x, ri.y, ri.z, _normal ? MapMode.Normal : MapMode.Satellite, LoadMapMode.CacheServer);
                        string file = _path + "\\" + ri.z.ToString() + "\\" + ri.z.ToString() + "_" + ri.x.ToString() + "_" + ri.y.ToString() + ".jpg";
                        ri.Bitmap = map;
                        //文件保存格式 “缩放级别_列_行.jpg”
                        if (!Directory.Exists(_path + "\\" + ri.z.ToString()))
                        {
                            Directory.CreateDirectory(_path + "\\" + ri.z.ToString());
                        }
                        map.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg);

                        this.Invoke((Action) delegate()  //输出
                        {
                            rchOuput.SelectionColor = Color.Green;
                            rchOuput.AppendText(DateTime.Now.ToLongTimeString() + " 第" + threadId + "号线程下载图片" + ri.z.ToString() + "_" + ri.x.ToString() + "_" + ri.y.ToString() + ".jpg\r\n");
                        });
                        _downloadnum++;

                        ri.bComplete = true;
                    }
                    catch
                    {
                        this.Invoke((Action) delegate()  //输出
                        {
                            rchOuput.SelectionColor = Color.Red;
                            rchOuput.AppendText(DateTime.Now.ToLongTimeString() + " 第" + threadId + "号线程下载图片" + ri.z.ToString() + "_" + ri.x.ToString() + "_" + ri.y.ToString() + ".jpg失败!\r\n");
                        });
                    }
                }
            }
            this.Invoke((Action) delegate()  //输出
            {
                rchOuput.SelectionColor = Color.Blue;
                rchOuput.AppendText(DateTime.Now.ToLongTimeString() + " 第" + threadId + "号线程执行完毕\r\n");
            });
            _thread--;                          //工作线程数目减一
            if (_thread == 0)                   //所有线程均结束
            {
                this.Invoke((Action) delegate() //输出
                {
                    rchOuput.SelectionColor = Color.Blue;
                    rchOuput.AppendText(DateTime.Now.ToLongTimeString() + " 图片下载结束!共下载" + _downloadnum + "张,共耗时" + (DateTime.Now - _startTime).TotalSeconds + "秒");
                    button2.Enabled = true;
                });
            }
        }