2016-09-22 59 views
0

我正在使用VB與ASP。我有一個ASP Image控件,其中包含一個圖像,在這種情況下,它是從Google Maps導入的靜態地圖。我想使用VB代碼將圖像下載到圖像(bmp,jpg或其他)。該圖像位於客戶端的asp:Image對象中。只需要使用服務器端的代碼下載圖像。如果有必要,我可以在客戶端使用JS來做到這一點。在那種情況下,我仍然想看看有沒有人知道如何做到這一點。將我的網頁中的圖像對象保存到文件

這裏是我的JavaScript代碼加載頁面上顯示的地圖到asp:image對象。這部分工作很好。只需要將圖像保存爲文件即可。有在之前的代碼預定義變量在頁面上,此功能是使用包括「地圖」,「地圖選項」,「邊界」和「標記」

function Export() { 
    map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 

    //URL of Google Static Maps. 
    var staticMapUrl = "https://maps.googleapis.com/maps/api/staticmap"; 

    //Set the Google Map Center. 
    staticMapUrl += "?center=" + mapOptions.center.G + "," + mapOptions.center.K; 

    //Set the Google Map Size. 
    staticMapUrl += "&size=220x350"; 

    //Set the Google Map Zoom. 
    staticMapUrl += "&zoom=" + mapOptions.zoom; 

    //Set the Google Map Type. 
    staticMapUrl += "&maptype=" + mapOptions.mapTypeId; 

    //Loop and add Markers. 
    for (var i = 0; i < markers.length; i++) { 
     var data = markers[i]; 

     if (data.pointNumber !== null) { 
      var labelNumber = data.pointNumber; 
      var labelString = labelNumber.toString(); 
      var iconName = 'm' + labelString + '.png'; 
      var roundLat = data.latitude; // + .00003; 
      var roundLon = data.longitude; // + .000005; 

      var myLatlng = new google.maps.LatLng(roundLat, roundLon); 

      var image = 
      { 
       url: 'ImagesForPoints/' + iconName, 
       scaledSize: new google.maps.Size(35, 48), // scaled size 
       //size: new google.maps.Size(53, 73), 
       //origin: new google.maps.Point(0,0), 
       //anchor: new google.maps.Point(30, 69) 
       anchor: new google.maps.Point(19, 45) 
      }; 


      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
       icon: image, 

      }); 

      bounds.extend(marker.position); 
      map.fitBounds(bounds); 

     } 
    } 

    //Display the Image of Google Map. 
    var imgMap = document.getElementById("imgMap"); 
    imgMap.src = staticMapUrl; 
    imgMap.style.display = "block"; 
} 
+0

你只需要在asp:image對象中顯示圖像嗎? – toto

+0

您可以添加一些示例代碼來演示您的問題以及迄今爲止的內容嗎?這會讓你更加清楚你被困在哪個部分。 – Adrian

+0

我可以在頁面上的圖像對象中顯示圖像。我想要做的就是將圖像保存爲某個文件。另外,如果我可以把它作爲文件流,我可以用它做任何我需要的。我似乎無法做到這一點。我將發佈代碼,瞭解它如何進入屏幕上的圖片對象。 @toto – WhiteBearMike

回答

0

由於您使用谷歌地圖靜態API,它生成一個來自URL的圖像,您的服務器端VB ASP.NET代碼只需要用於生成圖像的URL,因爲它可以下載它。

例如,從谷歌地圖靜態API以下網址將提供集中在布魯克林大橋的圖像,紐約:

https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap 
&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.
&markers=color:red%7Clabel:C%7C40.718217,-73.998284 

你的服務器端代碼,因此需要這個URL能夠下載完全相同的圖像。您可能可能通過以下幾種方式將此URL鏈接到您的服務器應用程序:

  1. 將URL作爲HTML表單提交(回發)發回。
  2. 編寫一個接受URL下載的Web服務。

由於您似乎正在使用ASP.NET Web窗體,第一個選項將是您現在最容易的選擇。所有你需要做的是一個ASP補充:HiddenField控件到你的頁面

<asp:HiddenField ID="MapURL" runat="server" ClientIDMode="static" /> 

,然後設置字段的值在你的JavaScript導出功能:

var mapUrlField = document.getElementById('<%= MapURL.ClientID %>'); 
if (mapUrlField) 
{ 
    mapUrlField.value = staticMapUrl; 
} 

,然後在上回發服務器端,您可以從隱藏字段中檢索URL,然後使用WebClient下載圖像。

Dim SaveFilePath as String = "c:\folder\mymap.png" 
Dim Client as new WebClient 
Client.DownloadFile(MapURL.Value, SaveFilePath) 
Client.Dispose 
+0

謝謝。明天我可以試試我的代碼。請注意,我確實嘗試了一個提醒來查看來自Google的網址,但沒有按照我的預期顯示整個網址。不過,我會在明天嘗試使用你的確切代碼,看看這是怎麼回事。 – WhiteBearMike

+0

好的,我試了一下,這裏有趣。谷歌傳遞的「staticMapUrl」最初在頁面上完美顯示圖像,但是當我捕獲發送的字符串時,它是不正確的。另外,在網站本身,右鍵點擊被禁用(不是由我),因此不保存爲。 。 。選項。我得到的網址是:https://maps.googleapis.com/maps/api/staticmap?center=undefined,undefined&size=220x350&zoom=19.38886&maptype=satellite。下載的圖像是帶有味精的世界地圖:地圖錯誤:g.co/staticmaperror。你怎麼看? – WhiteBearMike

+0

對於初學者來說,看看你提供的URL。中心參數有'未定義,未定義',它解釋了'Map error:g.co/staticmaperror'消息。這表明你沒有正確設置'mapOptions.center.G'或'mapOptions.center.K'。你甚至定義過mapOptions嗎?使用瀏覽器的開發者工具來調試你的JavaScript代碼。 – Adrian