【问题标题】:Save an Image object from my Webpage to File将图像对象从我的网页保存到文件
【发布时间】:2016-09-22 22:34:48
【问题描述】:

我正在使用带有 ASP 的 VB。我有一个 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";
}

【问题讨论】:

  • 您只需要在 asp:image 对象中显示图像吗?
  • 您能否添加一些示例代码来演示您的问题以及您目前的情况?它会让你更清楚你被困在哪些部分。
  • 我可以在页面上的图像对象中显示图像。我要做的就是将图像保存为某个文件。或者,如果我可以将它作为文件流获取,我可以用它做任何我需要的事情。我似乎无法做到这一点。我将发布有关它如何进入屏幕上的图片对象的代码。 @toto
  • 您能否提供一个示例网址,您可以在其中获取图片谷歌地图?
  • 这是我的代码。 code

标签: javascript asp.net vb.net


【解决方案1】:

由于您使用从 URL 生成图像的 Google Maps Static API,因此您的服务器端 VB ASP.NET 代码只需要用于生成图像的 URL,就可以下载它。

例如,来自 Google Maps Static API 的以下 URL 将提供以纽约布鲁克林大桥为中心的图像:

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.012318
&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

【讨论】:

  • 谢谢。明天我可以得到我的代码时,我会试试这个。请注意,我确实尝试发出警报以查看来自 Google 的 URL,但它并没有像我预期的那样显示整个 URL。不过,明天我会用你的确切代码试试这个,看看效果如何。
  • 好的,我试过了,有趣的是。谷歌传递的“staticMapUrl”最初确实在页面上完美地显示了图像,但是当我捕获发送的字符串时它是不正确的。此外,在网站本身上,右键单击被禁用(不是我),因此没有另存为 . . .选项。我得到的 URL 是:maps.googleapis.com/maps/api/…。下载的图像是带有消息的世界地图:地图错误:g.co/staticmaperror。你怎么看?
  • 首先,请查看您提供的 URL。 center 参数中有undefined,undefined,它解释了Map error: g.co/staticmaperror 消息。这表明您没有正确设置mapOptions.center.GmapOptions.center.K。你有没有定义mapOptions?使用浏览器的开发者工具来调试你的 JavaScript 代码。
猜你喜欢
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2013-05-31
相关资源
最近更新 更多