GVKun编程网logo

F#FSharpMap与字典性能(字典format)

4

本文将带您了解关于F#FSharpMap与字典性能的新内容,同时我们还将为您解释字典format的相关知识,另外,我们还将为您提供关于asp.net-mvc–在FSharp.Core旁边找不到FSha

本文将带您了解关于F#FSharpMap与字典性能的新内容,同时我们还将为您解释字典format的相关知识,另外,我们还将为您提供关于asp.net-mvc – 在FSharp.Core旁边找不到FSharp.Core.sigdata、C# SharpMap的简单使用、C# 中SharpMap的简单使用实例详解、C#(csharp)用CefSharp开发实现一个浏览器,抓取网站任意资源的实用信息。

本文目录一览:

F#FSharpMap与字典性能(字典format)

F#FSharpMap与字典性能(字典format)

我想知道F#不可变Map如何针对标准Dictionary执行。

我正在编写一个计数函数(针对每个行查找增量集),以用于具有数百万行和数千个元素的大型文件。

内存使用情况如何?

谢谢

答案1

小编典典

我想知道F#不可变Map如何针对标准Dictionary执行。

请参见Visual F#2010 for Technical
Computing第241-242页“优化:使用可变数据结构”。在非常类似的根据唯一键数量计算int频率的设置中,其显示Dictionary速度比a快5-40倍Map

通常,纯功能数据结构Map不仅在串行情况下非常慢,而且会破坏整个并行程序的可伸缩性。我最近对此做了一次演讲。

顺便说一句,别忘了有一个Seq.countBy功能!

asp.net-mvc – 在FSharp.Core旁边找不到FSharp.Core.sigdata

asp.net-mvc – 在FSharp.Core旁边找不到FSharp.Core.sigdata

我正在尝试将F#用于ASP.NET MVC应用程序.一个我的控制器动作向视图发送一个F#列表,所以我写道:
<%@ Page Language="C#" Inherits="ViewPage<FSharpList<int>>" %>

当然,要使其工作,我必须将Microsoft.FSharp.Collections添加到我的web.config中的namespaces元素:

<add namespace="Microsoft.FSharp.Collections"/>

并在assemblies元素中添加对FSharp.Core的引用:

<add assembly="FSharp.Core,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>

只要我添加此程序集引用,每个视图(无论是否使用F#类型)都会失败,并显示以下错误:

error FS1221: FSharp.Core.sigdata not found alongside FSharp.Core

我可以通过在我的视图中没有任何F#特定类型来解决这个问题,但是这个错误的原因是什么?另外,FSharp.Core.sigdata在哪里?它不在我的GAC中,我无法在任何地方找到它.

解决方法

您可以使用参考装配体找到它,因为sigdata和optdata是设计时间的东西(但我想CodeDom也需要它们?),例如:
C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v2.0\FSharp.Core.dll
C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v2.0\FSharp.Core.optdata
C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v2.0\FSharp.Core.sigdata

如果您复制应用程序正在使用的FSharp.Core旁边的那些,它可能会起作用.

C# SharpMap的简单使用

C# SharpMap的简单使用

本文是利用ShapMap实现GIS的简单应用的小例子,以供学习分享使用。关于sharpmap的说明,网上大多是以ShapeFile为例进行简单的说明,就连官网上的例子也不多。本文是自己参考了源代码进行整理的,主要是WinForm的例子。原理方面本文也不过多论述,主要是实例演示,需要的朋友还是以sharpmap源码进行深入研究。

什么是sharpmap ?

sharpmap是一个基于.net 2.0使用C#开发的Map渲染类库,可以渲染各类GIS数据(目前支持ESRI Shape和PostGIS格式),可应用于桌面和Web程序。代码行数近10000行,可以算是一个实现了最基本功能的GIS系统,有利于研究学习使用。

涉及知识点:

  • sharpmap的基本概念:Layer(图层,常用图层:VectorLayer,LabelLayer) , IProvider(数据提供者,常用数据源:Ogr(对应MapInfo),ShapFile,DataTablePoint(对应DataSet))
  • 坐标转换:主要用于经纬度和地图坐标的转换。

sharpmap知识结构图:

效果图如下:

(一)车辆轨迹图:数据源:Excel数据

(二)定点数据(数据源:Excel)将河南省十七个城市,全部插上小红旗

(三)使用MapInfo做背景文件(此处通过程序调整了比例尺)

(四)使用ShapFile做背景图

 

核心代码

  1 using BruTile.Predefined;
  2  GeoAPI.CoordinateSystems.Transformations;
  3  ProjNet.CoordinateSystems;
  4  ProjNet.CoordinateSystems.Transformations;
  5  sharpmap;
  6  sharpmap.Data.Providers;
  7  sharpmap.Layers;
  8  sharpmap.Rendering;
  9  sharpmap.Rendering.Thematics;
 10  sharpmap.Styles;
 11  System;
 12  System.Collections.Generic;
 13  System.Data;
 14  System.Data.OleDb;
 15  System.Drawing;
 16  System.Drawing.drawing2d;
 17  System.Drawing.Text;
 18  System.Linq;
 19  System.Text;
 20 using Point = GeoAPI.Geometries.Coordinate;
 21 namespace Demosharpmap
 22 {
 23     public class sharpmapHelper
 24     {
 25 
 26         private const string XlsConnectionString = "Provider={2};Data Source={0}\\{1};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
 27 
 28         static Map InitializeMap(MapType tt,float angle)
 29         {
 30             Map map = null 31             switch (tt)
 32             {
 33                 case MapType.RunLine:
 34                     map = InitializeMapOsmWithXls(angle);
 35                     break 36                  MapType.MapInfo:
 37                     map = InitializeMapinfo(angle);
 38                      39                  MapType.ShapeFile:
 40                     map = InitializeMapOrig(angle);
 41                      42                  MapType.Static:
 43                     map = InitializeMapOsmWithXls2(angle);
 44                      45                 default:
 46                     map = 47                      48             }
 49             return map;
 50         }
 51 
 52         /// <summary>
 53         /// MapInfo格式的地图文件
 54         </summary>
 55         <param name="angle"></param>
 56         <returns></returns>
 57         static Map InitializeMapinfo( 58  59             //Initialize a new map of size ''imagesize''
 60             Map map = new Map();
 61 
 62             Set up the countries layer
 63             VectorLayer layCountries = new VectorLayer(Countries);
 64             Set the datasource to a shapefile in the App_data folder
 65             try
 66  67                 layCountries.DataSource = new Ogr(GeoData/MapInfo/countriesMapInfo.tab 68  69             catch (TypeInitializationException ex)
 70  71                 if (ex.Message == The type initializer for ''OSGeo.OGR.Ogr'' threw an exception.)
 72                 {
 73                     throw  Exception(
 74                         String.Format(
 75                             The application threw a PINVOKE exception. You probably need to copy the unmanaged dll''s to your bin directory. They are a part of fwtools {0}. You can download it from: http://home.gdal.org/fwtools/, 76                             GdalRasterLayer.FWToolsversion));
 77                 }
 78                 throw 79  80 
 81             Set fill-style to green
 82             layCountries.Style.Fill =  SolidBrush(Color.Green);
 83             Set the polygons to have a black outline
 84             layCountries.Style.Outline = Pens.Black;
 85             layCountries.Style.EnableOutline = true 86             layCountries.SRID = 4326 87 
 88             Set up a river layer
 89             VectorLayer layRivers = Rivers 90              91             layRivers.DataSource = GeoData/MapInfo/riversMapInfo.tab 92             Define a blue 1px wide pen
 93             layRivers.Style.Line = new Pen(Color.Blue,1 94             layRivers.SRID =  95 
 96              97             VectorLayer layCities = Cities 98              99             layCities.DataSource = GeoData/MapInfo/citiesMapInfo.tab100             layCities.Style.SymbolScale = 0.8f101             layCities.MaxVisible = 40102             layCities.SRID = 103 
104             Set up a country label layer
105             LabelLayer layLabel = new LabelLayer(Country labels106             layLabel.DataSource = layCountries.DataSource;
107             layLabel.Enabled = 108             layLabel.LabelColumn = Name109             layLabel.Style =  LabelStyle();
110             layLabel.Style.ForeColor = Color.White;
111             layLabel.Style.Font = new Font(FontFamily.GenericSerif,1)">12112             layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128,1)">255,1)">0,1)">0));
113             layLabel.MaxVisible = 90114             layLabel.MinVisible = 30115             layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
116             layLabel.SRID = 117             layLabel.MultipartGeometryBehavIoUr = LabelLayer.MultipartGeometryBehavIoUrEnum.Largest;
118 
119             Set up a city label layer
120             LabelLayer layCityLabel = City labels121             layCityLabel.DataSource = layCities.DataSource;
122             layCityLabel.Enabled = 123             layCityLabel.LabelColumn = 124             layCityLabel.Style = 125             layCityLabel.Style.ForeColor = Color.Black;
126             layCityLabel.Style.Font = 11127             layCityLabel.MaxVisible = layLabel.MinVisible;
128             layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
129             layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
130             layCityLabel.Style.Offset = new PointF(3,1)">3131             layCityLabel.Style.Halo = new Pen(Color.Yellow,1)">2132             layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
133             layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
134             layCityLabel.SRID = 135             layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
136             layCityLabel.Style.CollisionDetection = 137 
138             Add the layers to the map object.
139             The order we add them in are the order they are drawn,so we add the rivers last to put them on top
140             map.Layers.Add(layCountries);
141             map.Layers.Add(layRivers);
142             map.Layers.Add(layCities);
143             map.Layers.Add(layLabel);
144             map.Layers.Add(layCityLabel);
145             增加Layers
146             var xlsPath = string.Format(XlsConnectionString,System.IO.Directory.GetCurrentDirectory(),1)">GeoData\\Henan.xls147             var ds = GetDataFromExcel(xlsPath,1)">148             var ct = GetCoordinateTransformation();
149             TransCoordinate(ds,ct);
150             string columeName = Rotation151             Add Rotation Column
152             AddColumetoDataSet(ds,columeName,-angle);
153 
154             var xlsLayer = GetLayerFromDataSet2(ds,Color.GreenYellow);Set up provider
155            
156             map.Layers.Add(xlsLayer); Add layer to map
157             map.Center = xlsLayer.Envelope.Centre; new Point(0,0);
158             map.MapScale = 350159             map.Center = new Point(0,0);
160 
161             _ogrSampleDataset = "MapInfo";
162 
163             Matrix mat = new Matrix();
164             mat.RotateAt(angle,map.WorldToImage(map.Center));
165             map.MapTransform = mat;
166 
167             map.ZoomToBox(xlsLayer.Envelope);
168             169 170 
171         172          ShapeFile
173         174         175         176         static Map InitializeMapOrig(177 178             179             Map map = 180 
181             182             VectorLayer layCountries = 183             184             layCountries.DataSource = new ShapeFile(GeoData/World/countries.shp",1)">185             186             layCountries.Style.Fill = 64187             188             layCountries.Style.Outline =189             layCountries.Style.EnableOutline = 190             layCountries.SRID = 191 
192             193             VectorLayer layRivers = 194             195             layRivers.DataSource = GeoData/World/rivers.shp196             197             layRivers.Style.Line = 198             layRivers.SRID = 199 
200             Set up a cities layer
201             VectorLayer layCities = 202             203             layCities.DataSource = GeoData/World/cities.shp204             layCities.Style.SymbolScale = 205             layCities.MaxVisible = 206             layCities.SRID = 207 
208             209             LabelLayer layLabel = 210             layLabel.DataSource =211             layLabel.Enabled = 212             layLabel.LabelColumn = 213             layLabel.Style = 214             layLabel.Style.ForeColor =215             layLabel.Style.Font = 216             layLabel.Style.BackColor = 217             layLabel.MaxVisible = 218             layLabel.MinVisible = 219             layLabel.Style.HorizontalAlignment =220             layLabel.SRID = 221             layLabel.MultipartGeometryBehavIoUr =222             layLabel.LabelFilter =223             layLabel.Style.CollisionDetection = 224             layLabel.LabelPositionDelegate = fdr => fdr.Geometry.InteriorPoint.Coordinate;
225             layLabel.PriorityColumn = POPDENS226 
227             228             LabelLayer layCityLabel = 229             layCityLabel.DataSource =230             layCityLabel.Enabled = 231             layCityLabel.LabelColumn = 232             layCityLabel.Style = 233             layCityLabel.Style.ForeColor =234             layCityLabel.Style.Font = 235             layCityLabel.MaxVisible =236             layCityLabel.Style.HorizontalAlignment =237             layCityLabel.Style.VerticalAlignment =238             layCityLabel.Style.Offset = 239             layCityLabel.Style.Halo = 240             layCityLabel.TextRenderingHint =241             layCityLabel.SmoothingMode =242             layCityLabel.SRID = 243             layCityLabel.LabelFilter =244             layCityLabel.Style.CollisionDetection = 245             layCityLabel.PriorityColumn = POPULATION246             layCityLabel.Theme = new GradientTheme(layCityLabel.PriorityColumn,1)">250000,1)">5000000247                  LabelStyle
248 249                     MaxVisible = 10250                     CollisionBuffer = new Size(),1)">251                     CollisionDetection = 252                     Enabled = 253                     ForeColor = Color.LightSlateGray,1)">254                     Halo = new Pen(Color.Silver,1)">255                     HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,1)">256                     VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,1)">257                     Font =  Font(GenericFontFamilies.SansSerif.ToString(),8f,FontStyle.Regular)
258                 },1)">259                 260 261                     MaxVisible = layLabel.MinVisible,1)">262                     CollisionBuffer = 263                     CollisionDetection = 264                     Enabled = 265                     ForeColor =266                     Halo = 5267                     HorizontalAlignment =268                     VerticalAlignment =269                     Font = 270                 });
271 
272             bool ignoreLength = false273 
274             var layRiverLabel = River labels275 276                 DataSource = layRivers.DataSource,1)">277                 Enabled = 278                 LabelColumn = 279                 TextRenderingHint = TextRenderingHint.AntiAlias,1)">280                 SmoothingMode = SmoothingMode.AntiAlias,1)">281                 SRID = 282                 LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection,1)">283                 MultipartGeometryBehavIoUr = LabelLayer.MultipartGeometryBehavIoUrEnum.CommonCenter,1)">284                 Style =
285                                            286                                            {
287                                                ForeColor = Color.DarkBlue,1)">288                                                Font = new Font(FontFamily.GenericSansSerif,1)">289                                                HorizontalAlignment =290                                                VerticalAlignment =291                                                CollisionDetection = true,
292                                                Halo = new Pen(Color.Azure,1)">293                                                IgnoreLength = ignoreLength,1)">294                                                Offset = 295 
296                                            },1)">297             };
298 
299             300             301             map.BackgroundLayer.Add(AsyncLayerProxyLayer.Create(layCountries));
302 303 304 305 306 307             map.Layers.Add(layRiverLabel);
308 
309             310             311             312             313             314             315             316             AddColumetoDataSet(ds,1)">317 
318             319 
320             map.Layers.Add(xlsLayer); Add layer to map
321                                       limit the zoom to 360 degrees width
322                                       map.MaximumZoom = 360;
323                                       map.BackColor = Color.LightBlue;
324 
325             map.Zoom = 360;
326             map.Center = xlsLayer.Envelope.Centre;327             map.MapScale = 328             329             330             331             332             333 334 
335         336          在线显示,圆点显示轨迹
337         338         339         340         static Map InitializeMapOsmWithXls(341 342             var map = 343 
344             var tileLayer =  TileAsyncLayer(
345                 KNownTileSources.Create(KNownTileSource.OpenStreetMap),1)">TileLayer - OSM with XLS346             tileLayer.SRID = 347             map.BackgroundLayer.Add(tileLayer);
348 
349             Get data from excel
350             GeoData\\Cities.xls351             352             var ds1 = GetDataFromExcel(xlsPath,1)">Cities2353             var ct = GetCoordinateTransformation();
354             TransCoordinate(ds,ct);
355             TransCoordinate(ds1,1)">356             357             358             AddColumetoDataSet(ds,1)">359             AddColumetoDataSet(ds1,1)">360 
361             var xlsLayer = GetLayerFromDataSet(ds,1)">362             map.Layers.Add(xlsLayer); 363 
364             var xlsLayer1 = GetLayerFromDataSet(ds1,Color.Red);
365             map.Layers.Add(xlsLayer1);
366 
367             var xlsLabelLayer = GetLabelLayerByVectorLayer(xlsLayer,1)">XLSLabel368 
369             xlsLabelLayer.Theme = new sharpmap.Rendering.Thematics.FontSizeTheme(xlsLabelLayer,map) { FontSizeScale = 1000f };
370             map.Layers.Add(xlsLabelLayer);
371             map.ZoomToBox(xlsLayer.Envelope.ExpandedBy(xlsLayer1.Envelope));
372             373 374 
375         376          在线显示,图标显示轨迹
377         378         379         380         static Map InitializeMapOsmWithXls2(381 382             383 
384             385                 KNownTileSources.Create(KNownTileSource.OpenStreetMap),1)">386             tileLayer.SRID = 387 388 
389             390             391             392             393 394             395             396             AddColumetoDataSet(ds,1)">397 
398             399             map.Layers.Add(xlsLayer); 400 
401             402 
403             xlsLabelLayer.Theme = new FontSizeTheme(xlsLabelLayer,1)">404 405             map.ZoomToBox(xlsLayer.Envelope);
406             407 408 
409         410          从Excel中读取数据
411         412         static DataSet GetDataFromExcel(string xlsPath,1)">string sheetName)
413 414             DataSet ds = new DataSet(XLS415             string sql = string.Format(SELECT * FROM [{0}$];416             using (var cn =  OleDbConnection(xlsPath))
417 418                 cn.open();
419                 var da = new OleDbDataAdapter( OleDbCommand(sql,cn)))
420 421                     da.Fill(ds);
422 423 424              ds;
425 426 
427         428          获取坐标转换对象
429         430         431         static ICoordinateTransformation GetCoordinateTransformation()
432 433             The SRS for this datasource is epsg:4326,therefore we need to transfrom it to OSM projection
434             var ctf =  CoordinateTransformationFactory();
435             var cf =  CoordinateSystemFactory();
436             var epsg4326 = cf.CreateFromWkt(GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"epsg\",\"7030\"]],\"6326\"]],PRIMEM[\"Greenwich\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,\"9122\"]],\"4326\"]]437             var epsg3857 = cf.CreateFromWkt(PROJCS[\"Popular Visualisation CRS / Mercator\",GEOGCS[\"Popular Visualisation CRS\",DATUM[\"Popular Visualisation Datum\",SPHEROID[\"Popular Visualisation Sphere\",\"7059\"]],TOwgs84[0,0],\"6055\"]],0.0174532925199433,\"9102\"]],AXIS[\"E\",EAST],AXIS[\"N\",norTH],\"4055\"]],PROJECTION[\"Mercator\"],ParaMETER[\"False_Easting\",ParaMETER[\"False_northing\",ParaMETER[\"Central_Meridian\",ParaMETER[\"Latitude_of_origin\",UNIT[\"metre\",1,\"9001\"]],AXIS[\"East\",AXIS[\"north\",\"3857\"]]438              ctf.CreateFromCoordinateSystems(epsg4326,epsg3857);
439              ct;
440 441 
442         443          转换地球经纬度到坐标
444         445         <param name="ds"></param>
446         <param name="ct"></param>
447         static void TransCoordinate(DataSet ds,ICoordinateTransformation ct)
448 449             foreach (System.Data.DaTarow row in ds.Tables[].Rows)
450 451                 if (row[X"] == dbnull.Value || row[Y"] == dbnull.Value) continue452                 var coords = new[] { Convert.Todouble(row["]),Convert.Todouble(row[]) };
453                 coords = ct.MathTransform.Transform(coords);
454                 row["] = coords[];
455                 row[456 457 458 
459         460          增加列
461         462         463         <param name="columeName"></param>
464         <param name="columeValue"></param>
465         void AddColumetoDataSet(DataSet ds,1)">string columeName,1)"> columeValue)
466 467             ds.Tables[0].Columns.Add(columeName,1)">typeof(468             469 470                 row["] = -columeValue;
471 472 473 
474         475          轨迹用点表示
476         477         478         <param name="c"></param>
479         480          VectorLayer GetLayerFromDataSet(DataSet ds,Color c)
481 482             var xlsProvider = new DataTablePoint(ds.Tables[0],1)">OID483             var xlsLayer = 484             { Style = new VectorStyle() { PointColor =  SolidBrush(c) } };
485              xlsLayer;
486 487 
488         489          获取带图标的图层
490         491         492         493         494          VectorLayer GetLayerFromDataSet2(DataSet ds,1)">495 496             497             498             { Style = { Symbol=Properties.Resources.redflag} };
499             500 501 
502         static LabelLayer GetLabelLayerByVectorLayer(VectorLayer xlsLayer,1)"> layerName)
503 504             var xlsLabelLayer =  LabelLayer(layerName)
505 506                 DataSource = xlsLayer.DataSource,1)">507                 LabelColumn = NAME508                 PriorityColumn = "Population",1)">509                 Style =
510                     {
511                         CollisionBuffer =  System.Drawing.Sizef(2f,2f),1)">512                         CollisionDetection = true
513                     },1)">514                 LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection
515 516              xlsLabelLayer;
517 518     }
519 
520     enum MapType {
521         ShapeFile = 522         MapInfo = 523         RunLine = 2,1)">运行轨迹
524         Static = 3 定点数据
525 
526 527 }
View Code

 

源码下载链接

 

备注:

1. 因用的MapInfo和Shape源文件为源码里面的,所有为英文显示。

 

总结

以上是小编为你收集整理的C# SharpMap的简单使用全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

C# 中SharpMap的简单使用实例详解

C# 中SharpMap的简单使用实例详解

本文是利用ShapMap实现GIS的简单应用的小例子,以供学习分享使用。关于SharpMap的说明,网上大多是以ShapeFile为例进行简单的说明,就连官网上的例子也不多。本文是自己参考了源代码进行整理的,主要是WinForm的例子。原理方面本文也不过多论述,主要是实例演示,需要的朋友还是以SharpMap源码进行深入研究。

什么是SharpMap ?

SharpMap是一个基于.net 2.0使用C#开发的Map渲染类库,可以渲染各类GIS数据(目前支持ESRI Shape和PostGIS格式),可应用于桌面和Web程序。代码行数近10000行,可以算是一个实现了最基本功能的GIS系统,有利于研究学习使用。

涉及知识点:

  • SharpMap的基本概念:Layer(图层,常用图层:VectorLayer,LabelLayer) , IProvider(数据提供者,常用数据源:Ogr(对应MapInfo),ShapFile,DataTablePoint(对应DataSet))
  • 坐标转换:主要用于经纬度和地图坐标的转换。

SharpMap知识结构图:

效果图如下:

(一)车辆轨迹图:数据源:Excel数据

(二)定点数据(数据源:Excel)将河南省十七个城市,全部插上小红旗

(三)使用MapInfo做背景文件(此处通过程序调整了比例尺)

(四)使用ShapFile做背景图

核心代码

using BruTile.Predefined;
using GeoAPI.CoordinateSystems.Transformations;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using SharpMap;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Rendering;
using SharpMap.Rendering.Thematics;
using SharpMap.Styles;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using Point = GeoAPI.Geometries.Coordinate;
namespace DemoSharpMap
{
 public class SharpMapHelper
 {
 private const string XlsConnectionString = "Provider={2};Data Source={0}\\{1};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
 public static Map InitializeMap(MapType tt,float angle)
 {
  Map map = null;
  switch (tt)
  {
  case MapType.RunLine:
   map = InitializeMapOsmWithXls(angle);
   break;
  case MapType.MapInfo:
   map = InitializeMapinfo(angle);
   break;
  case MapType.ShapeFile:
   map = InitializeMapOrig(angle);
   break;
  case MapType.Static:
   map = InitializeMapOsmWithXls2(angle);
   break;
  default:
   map = InitializeMapOsmWithXls(angle);
   break;
  }
  return map;
 }
 /// <summary>
 /// MapInfo格式的地图文件
 /// </summary>
 /// <param name="angle"></param>
 /// <returns></returns>
 private static Map InitializeMapinfo(float angle)
 {
  //Initialize a new map of size ''imagesize''
  Map map = new Map();
  //Set up the countries layer
  VectorLayer layCountries = new VectorLayer("Countries");
  //Set the datasource to a shapefile in the App_data folder
  try
  {
  layCountries.DataSource = new Ogr("GeoData/MapInfo/countriesMapInfo.tab");
  }
  catch (TypeInitializationException ex)
  {
  if (ex.Message == "The type initializer for ''OSGeo.OGR.Ogr'' threw an exception.")
  {
   throw new Exception(
   String.Format(
    "The application threw a PINVOKE exception. You probably need to copy the unmanaged dll''s to your bin directory. They are a part of fwtools {0}. You can download it from: http://home.gdal.org/fwtools/",
    GdalRasterLayer.FWToolsVersion));
  }
  throw;
  }
  //Set fill-style to green
  layCountries.Style.Fill = new SolidBrush(Color.Green);
  //Set the polygons to have a black outline
  layCountries.Style.Outline = Pens.Black;
  layCountries.Style.EnableOutline = true;
  layCountries.SRID = 4326;
  //Set up a river layer
  VectorLayer layRivers = new VectorLayer("Rivers");
  //Set the datasource to a shapefile in the App_data folder
  layRivers.DataSource = new Ogr("GeoData/MapInfo/riversMapInfo.tab");
  //Define a blue 1px wide pen
  layRivers.Style.Line = new Pen(Color.Blue, 1);
  layRivers.SRID = 4326;
  //Set up a river layer
  VectorLayer layCities = new VectorLayer("Cities");
  //Set the datasource to a shapefile in the App_data folder
  layCities.DataSource = new Ogr("GeoData/MapInfo/citiesMapInfo.tab");
  layCities.Style.SymbolScale = 0.8f;
  layCities.MaxVisible = 40;
  layCities.SRID = 4326;
  //Set up a country label layer
  LabelLayer layLabel = new LabelLayer("Country labels");
  layLabel.DataSource = layCountries.DataSource;
  layLabel.Enabled = true;
  layLabel.LabelColumn = "Name";
  layLabel.Style = new LabelStyle();
  layLabel.Style.ForeColor = Color.White;
  layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
  layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
  layLabel.MaxVisible = 90;
  layLabel.MinVisible = 30;
  layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
  layLabel.SRID = 4326;
  layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
  //Set up a city label layer
  LabelLayer layCityLabel = new LabelLayer("City labels");
  layCityLabel.DataSource = layCities.DataSource;
  layCityLabel.Enabled = true;
  layCityLabel.LabelColumn = "Name";
  layCityLabel.Style = new LabelStyle();
  layCityLabel.Style.ForeColor = Color.Black;
  layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
  layCityLabel.MaxVisible = layLabel.MinVisible;
  layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
  layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
  layCityLabel.Style.Offset = new PointF(3, 3);
  layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
  layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
  layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
  layCityLabel.SRID = 4326;
  layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
  layCityLabel.Style.CollisionDetection = true;
  //Add the layers to the map object.
  //The order we add them in are the order they are drawn, so we add the rivers last to put them on top
  map.Layers.Add(layCountries);
  map.Layers.Add(layRivers);
  map.Layers.Add(layCities);
  map.Layers.Add(layLabel);
  map.Layers.Add(layCityLabel);
  //增加Layers
  var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Henan.xls", Properties.Settings.Default.OleDbProvider);
  var ds = GetDataFromExcel(xlsPath, "Cities");
  //var ct = GetCoordinateTransformation();
  //TransCoordinate(ds, ct);
  string columeName = "Rotation";
  //Add Rotation Column
  AddColumeToDataSet(ds, columeName, -angle);
  var xlsLayer = GetLayerFromDataSet2(ds, Color.GreenYellow);//Set up provider
  map.Layers.Add(xlsLayer); //Add layer to map
  map.Center = xlsLayer.Envelope.Centre;// new Point(0, 0);
  map.MapScale = 350;
  //map.Center = new Point(0, 0);
  //_ogrSampleDataset = "MapInfo";
  //Matrix mat = new Matrix();
  //mat.RotateAt(angle, map.WorldToImage(map.Center));
  //map.MapTransform = mat;
  //map.ZoomToBox(xlsLayer.Envelope);
  return map;
 }
 /// <summary>
 /// ShapeFile
 /// </summary>
 /// <param name="angle"></param>
 /// <returns></returns>
 private static Map InitializeMapOrig(float angle)
 {
  //Initialize a new map of size ''imagesize''
  Map map = new Map();
  //Set up the countries layer
  VectorLayer layCountries = new VectorLayer("Countries");
  //Set the datasource to a shapefile in the App_data folder
  layCountries.DataSource = new ShapeFile("GeoData/World/countries.shp", true);
  //Set fill-style to green
  layCountries.Style.Fill = new SolidBrush(Color.FromArgb(64, Color.Green));
  //Set the polygons to have a black outline
  layCountries.Style.Outline = Pens.Black;
  layCountries.Style.EnableOutline = true;
  layCountries.SRID = 4326;
  //Set up a river layer
  VectorLayer layRivers = new VectorLayer("Rivers");
  //Set the datasource to a shapefile in the App_data folder
  layRivers.DataSource = new ShapeFile("GeoData/World/rivers.shp", true);
  //Define a blue 1px wide pen
  layRivers.Style.Line = new Pen(Color.Blue, 1);
  layRivers.SRID = 4326;
  //Set up a cities layer
  VectorLayer layCities = new VectorLayer("Cities");
  //Set the datasource to a shapefile in the App_data folder
  layCities.DataSource = new ShapeFile("GeoData/World/cities.shp", true);
  layCities.Style.SymbolScale = 0.8f;
  layCities.MaxVisible = 40;
  layCities.SRID = 4326;
  //Set up a country label layer
  LabelLayer layLabel = new LabelLayer("Country labels");
  layLabel.DataSource = layCountries.DataSource;
  layLabel.Enabled = true;
  layLabel.LabelColumn = "Name";
  layLabel.Style = new LabelStyle();
  layLabel.Style.ForeColor = Color.White;
  layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
  layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
  layLabel.MaxVisible = 90;
  layLabel.MinVisible = 30;
  layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
  layLabel.SRID = 4326;
  layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
  layLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
  layLabel.Style.CollisionDetection = true;
  layLabel.LabelPositionDelegate = fdr => fdr.Geometry.InteriorPoint.Coordinate;
  layLabel.PriorityColumn = "POPDENS";
  //Set up a city label layer
  LabelLayer layCityLabel = new LabelLayer("City labels");
  layCityLabel.DataSource = layCities.DataSource;
  layCityLabel.Enabled = true;
  layCityLabel.LabelColumn = "Name";
  layCityLabel.Style = new LabelStyle();
  layCityLabel.Style.ForeColor = Color.Black;
  layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
  layCityLabel.MaxVisible = layLabel.MinVisible;
  layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
  layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
  layCityLabel.Style.Offset = new PointF(3, 3);
  layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
  layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
  layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
  layCityLabel.SRID = 4326;
  layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
  layCityLabel.Style.CollisionDetection = true;
  layCityLabel.PriorityColumn = "POPULATION";
  layCityLabel.Theme = new GradientTheme(layCityLabel.PriorityColumn, 250000, 5000000,
  new LabelStyle
  {
   MaxVisible = 10,
   CollisionBuffer = new Size(0, 0),
   CollisionDetection = true,
   Enabled = true,
   ForeColor = Color.LightSlateGray,
   Halo = new Pen(Color.Silver, 1),
   HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
   VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
   Font = new Font(GenericFontFamilies.SansSerif.ToString(), 8f, FontStyle.Regular)
  },
  new LabelStyle
  {
   MaxVisible = layLabel.MinVisible,
   CollisionBuffer = new Size(3, 3),
   CollisionDetection = true,
   Enabled = true,
   ForeColor = Color.LightSlateGray,
   Halo = new Pen(Color.Silver, 5),
   HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
   VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
   Font = new Font(GenericFontFamilies.SansSerif.ToString(), 16f, FontStyle.Bold)
  });
  bool ignoreLength = false;
  var layRiverLabel = new LabelLayer("River labels")
  {
  DataSource = layRivers.DataSource,
  Enabled = true,
  LabelColumn = "Name",
  TextRenderingHint = TextRenderingHint.AntiAlias,
  SmoothingMode = SmoothingMode.AntiAlias,
  SRID = 4326,
  LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection,
  MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter,
  Style =
      new LabelStyle
      {
      ForeColor = Color.DarkBlue,
      Font = new Font(FontFamily.GenericSansSerif, 11),
      HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
      VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
      //CollisionDetection = true,
      Halo = new Pen(Color.Azure, 2),
      IgnoreLength = ignoreLength,
      Offset = new PointF(0, -10)
      },
  };
  //Add the layers to the map object.
  //The order we add them in are the order they are drawn, so we add the rivers last to put them on top
  //map.BackgroundLayer.Add(AsyncLayerProxyLayer.Create(layCountries));
  map.Layers.Add(layCountries);
  map.Layers.Add(layRivers);
  map.Layers.Add(layCities);
  map.Layers.Add(layLabel);
  map.Layers.Add(layCityLabel);
  map.Layers.Add(layRiverLabel);
  //增加Layers
  var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Henan.xls", Properties.Settings.Default.OleDbProvider);
  var ds = GetDataFromExcel(xlsPath, "Cities");
  //var ct = GetCoordinateTransformation();
  //TransCoordinate(ds, ct);
  string columeName = "Rotation";
  //Add Rotation Column
  AddColumeToDataSet(ds, columeName, -angle);
  var xlsLayer = GetLayerFromDataSet2(ds, Color.GreenYellow);//Set up provider
  map.Layers.Add(xlsLayer); //Add layer to map
     //limit the zoom to 360 degrees width
     //map.MaximumZoom = 360;
     //map.BackColor = Color.LightBlue;
  //map.Zoom = 360;
  map.Center = xlsLayer.Envelope.Centre;// new Point(0, 0);
  map.MapScale = 350;
  //Matrix mat = new Matrix();
  //mat.RotateAt(angle, map.WorldToImage(map.Center));
  //map.MapTransform = mat;
  //map.ZoomToBox(xlsLayer.Envelope);
  return map;
 }
 /// <summary>
 /// 在线显示,圆点显示轨迹
 /// </summary>
 /// <param name="angle"></param>
 /// <returns></returns>
 private static Map InitializeMapOsmWithXls(float angle)
 {
  var map = new Map();
  var tileLayer = new TileAsyncLayer(
  KnownTileSources.Create(KnownTileSource.OpenStreetMap), "TileLayer - OSM with XLS");
  tileLayer.SRID = 4326;
  map.BackgroundLayer.Add(tileLayer);
  //Get data from excel
  var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Cities.xls", Properties.Settings.Default.OleDbProvider);
  var ds = GetDataFromExcel(xlsPath, "Cities");
  var ds1 = GetDataFromExcel(xlsPath, "Cities2");
  var ct = GetCoordinateTransformation();
  TransCoordinate(ds, ct);
  TransCoordinate(ds1, ct);
  string columeName = "Rotation";
  //Add Rotation Column
  AddColumeToDataSet(ds, columeName, -angle);
  AddColumeToDataSet(ds1, columeName, -angle);
  var xlsLayer = GetLayerFromDataSet(ds, Color.GreenYellow);//Set up provider
  map.Layers.Add(xlsLayer); //Add layer to map
  var xlsLayer1 = GetLayerFromDataSet(ds1, Color.Red);
  map.Layers.Add(xlsLayer1);
  var xlsLabelLayer = GetLabelLayerByVectorLayer(xlsLayer, "XLSLabel");
  xlsLabelLayer.Theme = new SharpMap.Rendering.Thematics.FontSizeTheme(xlsLabelLayer, map) { FontSizeScale = 1000f };
  map.Layers.Add(xlsLabelLayer);
  map.ZoomToBox(xlsLayer.Envelope.ExpandedBy(xlsLayer1.Envelope));
  return map;
 }
 /// <summary>
 /// 在线显示,图标显示轨迹
 /// </summary>
 /// <param name="angle"></param>
 /// <returns></returns>
 private static Map InitializeMapOsmWithXls2(float angle)
 {
  var map = new Map();
  var tileLayer = new TileAsyncLayer(
  KnownTileSources.Create(KnownTileSource.OpenStreetMap), "TileLayer - OSM with XLS");
  tileLayer.SRID = 4326;
  map.BackgroundLayer.Add(tileLayer);
  //Get data from excel
  var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Henan.xls", Properties.Settings.Default.OleDbProvider);
  var ds = GetDataFromExcel(xlsPath, "Cities");
  var ct = GetCoordinateTransformation();
  TransCoordinate(ds, ct);
  string columeName = "Rotation";
  //Add Rotation Column
  AddColumeToDataSet(ds, columeName, -angle);
  var xlsLayer = GetLayerFromDataSet2(ds, Color.GreenYellow);//Set up provider
  map.Layers.Add(xlsLayer); //Add layer to map
  var xlsLabelLayer = GetLabelLayerByVectorLayer(xlsLayer, "XLSLabel");
  xlsLabelLayer.Theme = new FontSizeTheme(xlsLabelLayer, map) { FontSizeScale = 1000f };
  map.Layers.Add(xlsLabelLayer);
  map.ZoomToBox(xlsLayer.Envelope);
  return map;
 }
 /// <summary>
 /// 从Excel中读取数据
 /// </summary>
 private static DataSet GetDataFromExcel(string xlsPath, string sheetName)
 {
  DataSet ds = new DataSet("XLS");
  string sql = string.Format("SELECT * FROM [{0}$];", sheetName);
  using (var cn = new OleDbConnection(xlsPath))
  {
  cn.Open();
  using (var da = new OleDbDataAdapter(new OleDbCommand(sql, cn)))
  {
   da.Fill(ds);
  }
  }
  return ds;
 }
 /// <summary>
 /// 获取坐标转换对象
 /// </summary>
 /// <returns></returns>
 private static ICoordinateTransformation GetCoordinateTransformation()
 {
  //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection
  var ctf = new CoordinateTransformationFactory();
  var cf = new CoordinateSystemFactory();
  var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
  var epsg3857 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]");
  var ct = ctf.CreateFromCoordinateSystems(epsg4326, epsg3857);
  return ct;
 }
 /// <summary>
 /// 转换地球经纬度到坐标
 /// </summary>
 /// <param name="ds"></param>
 /// <param name="ct"></param>
 private static void TransCoordinate(DataSet ds, ICoordinateTransformation ct)
 {
  foreach (System.Data.DataRow row in ds.Tables[0].Rows)
  {
  if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) continue;
  var coords = new[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"]) };
  coords = ct.MathTransform.Transform(coords);
  row["X"] = coords[0];
  row["Y"] = coords[1];
  }
 }
 /// <summary>
 /// 增加列
 /// </summary>
 /// <param name="ds"></param>
 /// <param name="columeName"></param>
 /// <param name="columeValue"></param>
 private static void AddColumeToDataSet(DataSet ds, string columeName, float columeValue)
 {
  ds.Tables[0].Columns.Add(columeName, typeof(float));
  foreach (System.Data.DataRow row in ds.Tables[0].Rows)
  {
  row["Rotation"] = -columeValue;
  }
 }
 /// <summary>
 /// 轨迹用点表示
 /// </summary>
 /// <param name="ds"></param>
 /// <param name="c"></param>
 /// <returns></returns>
 private static VectorLayer GetLayerFromDataSet(DataSet ds, Color c)
 {
  var xlsProvider = new DataTablePoint(ds.Tables[0], "OID", "X", "Y");
  var xlsLayer = new VectorLayer("XLS", xlsProvider)
  { Style = new VectorStyle() { PointColor = new SolidBrush(c) } };
  return xlsLayer;
 }
 /// <summary>
 /// 获取带图标的图层
 /// </summary>
 /// <param name="ds"></param>
 /// <param name="c"></param>
 /// <returns></returns>
 private static VectorLayer GetLayerFromDataSet2(DataSet ds, Color c)
 {
  var xlsProvider = new DataTablePoint(ds.Tables[0], "OID", "X", "Y");
  var xlsLayer = new VectorLayer("XLS", xlsProvider)
  { Style = { Symbol=Properties.Resources.redflag} };
  return xlsLayer;
 }
 private static LabelLayer GetLabelLayerByVectorLayer(VectorLayer xlsLayer, string layerName)
 {
  var xlsLabelLayer = new LabelLayer(layerName)
  {
  DataSource = xlsLayer.DataSource,
  LabelColumn = "NAME",
  //PriorityColumn = "Population",
  Style =
   {
   CollisionBuffer = new System.Drawing.SizeF(2f, 2f),
   CollisionDetection = true
   },
  LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection
  };
  return xlsLabelLayer;
 }
 }
 public enum MapType {
 ShapeFile = 0,
 MapInfo = 1,
 RunLine = 2,//运行轨迹
 Static = 3 //定点数据
 }
}

备注:

1. 因用的MapInfo和Shape源文件为源码里面的,所有为英文显示。

源码下载:http://xiazai.jb51.net/201707/yuanma/DemoSharpMap_jb51.rar

总结

以上所述是小编给大家介绍的C# 中SharpMap的简单使用实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

您可能感兴趣的文章:
  • linux中 pmap 命令详解

C#(csharp)用CefSharp开发实现一个浏览器,抓取网站任意资源

C#(csharp)用CefSharp开发实现一个浏览器,抓取网站任意资源

前言:

不少网站会保护自己,防止别人下载图片。

因此下载器,先后改了好几次,不断技术升级。


早期 用WebClient 下载 HTML 分析 URL 下载。

后来 为了下载 手机网站的图片,用上了 User-Agent 特性,模拟手机浏览器。


之前遇到一个复杂些的 网站M ,页面用 JS 加载生成。无法直接获得HTML。

用Chrome一番分析后,发现其图片地址 有固定格式。文件名是 3位数数字,从1开始累加。

因此只要在浏览器 取到第一个图,后面的图片URL 生成下 也得到了。批量下载搞定。


前段时间 网站M 升级。图片展示使用 HTML5 canavs 显示,而且 图片路径 也加随机字符。难以生成了。

对于Chrome来说依然没有遮挡效果。依然可以取到 URL,可以手工保存图片, 然而 无法批量下载了。


问题来了:

无法取到 HTML分析出 图片路径,而且路径名称随机。那么如何批量下载图片?

既然 图片展示到我们面前,数据必然也已经到了 我们这边,就看我们这么取得了。


解决办法:

WebClient不行,于是想到了  Winform 的 WebBrowser 实现浏览器,能显示必然能取到数据。

WebBrowser对于大部分网站还是可以的。然而对于 HTML5的 或者 新特性的 网站就有些 不方便了。有时候不能正常显示网站。例如,网站M.

它依赖 操作系统的 IE浏览器,而且默认是系统里老版本的 IE。可能是 IE7 吧。

一番资料查找 发现CefSharp。这个是Chrome 的另一个开源项目, C# 调用。可以在Winform,Wpf 等中 嵌入浏览器。


CefSharp在网上有一些资料,不多。为了弄明白CefSharp里 取得 图片URL,图片数据。花了一番功夫,查资料,看源码。

倾情大放送,来一大波代码:


开发环境:VS2015  framework 4.5.2  x86模式   CefSharp 63.0.30 

winform:

有了上面的代码,便可以获取到 目标网站的 任意资源了。



END

一些推荐

  1. 【开源Blazor控件库】点击浏览

  2. 【开源B/S项目】WTM

  3. 【开源WPF控件库】点击浏览

  4. 【开源WPF项目】点击浏览

  5. 【开源Xamarin项目】全球优秀Xamarin.Forms项目收集

  6. 【开源Winform控件库】HZHControls、SunnyUI

  7. 【开源Winform项目】SiMay远程控制管理系统

  8. 【开源Qt控件库】QWidgetDemo、Material风格的Qt控件

  9. 【开源Qt项目】点击浏览

  10. 【更多分享】点击浏览


资源分享

  • 回复数字【01】:获取DotNet技术资料
  • 回复数字【02】:获取Java技术资料
  • 回复数字【03】:获取Android技术资料
  • 回复数字【04】:获取C++技术资料
  • 回复数字【05】:获取Qt技术资料
  • 回复数字【06】:获取React资源
  • 添加号主微信号【dotnet9】:备注【入群】加入与大佬们的技术交流

时间如流水,只能流去不流回。

  • 公众号:Dotnet9
  • 号主微信号:dotnet9
  • 文本转自公众号:新阁Net社群
  • 原文地址:https://mp.weixin.qq.com/s/724zSH1_ZwY3BhY2pywoIw
  • 编辑:沙漠之尽头的狼
  • 日期:2020-12-04
微信公众号:Dotnet9
-好东西要转发,设为"星标"★抢先看-

点击阅读原文,关注Dotnet9更多好文。

本文分享自微信公众号 - Dotnet9(dotnet9_com)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

关于F#FSharpMap与字典性能字典format的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net-mvc – 在FSharp.Core旁边找不到FSharp.Core.sigdata、C# SharpMap的简单使用、C# 中SharpMap的简单使用实例详解、C#(csharp)用CefSharp开发实现一个浏览器,抓取网站任意资源等相关知识的信息别忘了在本站进行查找喔。

本文标签:

上一篇连续添加char以获取字典中最长的单词(连续添加char以获取字典中最长的单词是什么)

下一篇使用OpenCV在哪里可以学习/找到从Kinect流式传输的手势识别示例?(基于opencv的手势识别)