Dim client As New Net.WebClient() client.DownloadFile("http://www.server.com/billede.jpg", "C:\lokal fil.jpg")
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; namespace DownloadImagesFromWeb { public class WebsiteCommunication { private string WebsiteHtml { get; set; } public Uri WebsiteUrl { get; private set; } public IList<string> WebsiteImages { get; private set; } public string DownloadPath { get; private set; } public IList<FileInfo> DownloadedFiles { get; private set; } public WebsiteCommunication(string url, string downloadPath) { this.WebsiteImages = new List<string>(); this.DownloadedFiles = new List<FileInfo>(); this.DownloadPath = downloadPath; if (url != string.Empty) { this.WebsiteUrl = new Uri(url); RequestHTMLFromWebsite(); FindAllImagesInHtml(); DownloadImagesFromWeb(); } } private void DownloadImagesFromWeb() { if ((this.DownloadPath != string.Empty) && (Directory.Exists(this.DownloadPath))) { try { this.DownloadedFiles.Clear(); foreach (string imageFilePath in this.WebsiteImages) { string imageExt = string.Empty; if (imageFilePath.EndsWith("jpg")) imageExt = ".jpg"; else if (imageFilePath.EndsWith("gif")) imageExt = ".gif"; else if (imageFilePath.EndsWith("png")) imageExt = ".png"; else if (imageFilePath.EndsWith("bmp")) imageExt = ".bmp"; else if (imageFilePath.EndsWith("jpeg")) imageExt = ".jpeg"; if(imageExt != string.Empty) { WebClient webclient = new WebClient(); byte[] buffer = webclient.DownloadData(imageFilePath); string filename = this.DownloadPath + Guid.NewGuid().ToString().Replace("-", "") + imageExt; FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite); BinaryWriter bw = new BinaryWriter(fs); bw.Write(buffer); bw.Close(); if (File.Exists(filename)) { FileInfo fi = new FileInfo(filename); this.DownloadedFiles.Add(fi); } } } } catch (Exception) { } } } private void FindAllImagesInHtml() { if (this.WebsiteHtml != string.Empty) { try { Regex regex = new Regex(@"<img[a-zA-Z0-9_\^\$\.\|\{\[\}\]\(\)\*\+\?\\~`!@#%&-=;:'"",/\n\s]*>"); MatchCollection mc = regex.Matches(this.WebsiteHtml); if(this.WebsiteImages == null) this.WebsiteImages = new List<string>(); this.WebsiteImages.Clear(); foreach (Match m in mc) { string value = m.Value; string source = GetSrcFromImgTag(value); if ((source.StartsWith("http")) || (source.StartsWith("www"))) { this.WebsiteImages.Add(source); } } } catch (Exception) { } } } private string GetSrcFromImgTag(string imgTag) { string src = string.Empty; if (imgTag != string.Empty) { bool foundStartQuote = false; int startOfSrcQuote = 0; bool foundEndQuote = false; int endOfSrcQuote = 0; for (int i = 0; i < imgTag.Length; i++) { if (!foundStartQuote) { if (imgTag.Substring(i, 3) == "src") { foundStartQuote = true; startOfSrcQuote = (5 + i); } } else { if (imgTag.Substring(i, 1) == " ") { foundEndQuote = true; endOfSrcQuote = i - 1; } if ((foundStartQuote) && (foundEndQuote)) { src = imgTag.Substring(startOfSrcQuote, (endOfSrcQuote - startOfSrcQuote)); break; } } } } return src; } private void RequestHTMLFromWebsite() { if (this.WebsiteUrl != null) { try { WebRequest request = WebRequest.Create(this.WebsiteUrl); request.Credentials = CredentialCache.DefaultNetworkCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); this.WebsiteHtml = reader.ReadToEnd(); reader.Close(); dataStream.Close(); } response.Close(); } catch (Exception) { } } } } }
Det kan du sagtens men jeg er ikke sikker på at det er lovligt og derfor vil jeg heller ikke poste kode til det her.
Det kan du sagtens men jeg er ikke sikker på at det er lovligt og derfor vil jeg heller ikke poste kode til det her.Så en browser er ulovlig? For mig bekendt så henter den billeder?
Jeg bøjer mig... Kunne ikke lade den challenge gå forbi.Her er koden til en klasse der kan udføre præcis det du har spurgt efter:CSharp kode using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; namespace DownloadImagesFromWeb { public class WebsiteCommunication { private string WebsiteHtml { get; set; } public Uri WebsiteUrl { get; private set; } public IList<string> WebsiteImages { get; private set; } public string DownloadPath { get; private set; } public IList<FileInfo> DownloadedFiles { get; private set; } public WebsiteCommunication(string url, string downloadPath) { this.WebsiteImages = new List<string>(); this.DownloadedFiles = new List<FileInfo>(); this.DownloadPath = downloadPath; if (url != string.Empty) { this.WebsiteUrl = new Uri(url); RequestHTMLFromWebsite(); FindAllImagesInHtml(); DownloadImagesFromWeb(); } } private void DownloadImagesFromWeb() { if ((this.DownloadPath != string.Empty) && (Directory.Exists(this.DownloadPath))) { try { this.DownloadedFiles.Clear(); foreach (string imageFilePath in this.WebsiteImages) { string imageExt = string.Empty; if (imageFilePath.EndsWith("jpg")) imageExt = ".jpg"; else if (imageFilePath.EndsWith("gif")) imageExt = ".gif"; else if (imageFilePath.EndsWith("png")) imageExt = ".png"; else if (imageFilePath.EndsWith("bmp")) imageExt = ".bmp"; else if (imageFilePath.EndsWith("jpeg")) imageExt = ".jpeg"; if(imageExt != string.Empty) { WebClient webclient = new WebClient(); byte[] buffer = webclient.DownloadData(imageFilePath); string filename = this.DownloadPath + Guid.NewGuid().ToString().Replace("-", "") + imageExt; FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite); BinaryWriter bw = new BinaryWriter(fs); bw.Write(buffer); bw.Close(); if (File.Exists(filename)) { FileInfo fi = new FileInfo(filename); this.DownloadedFiles.Add(fi); } } } } catch (Exception) { } } } private void FindAllImagesInHtml() { if (this.WebsiteHtml != string.Empty) { try { Regex regex = new Regex(@"<img[a-zA-Z0-9_\^\$\.\|\{\[\}\]\(\)\*\+\?\\~`!@#%&-=;:'"",/\n\s]*>"); MatchCollection mc = regex.Matches(this.WebsiteHtml); if(this.WebsiteImages == null) this.WebsiteImages = new List<string>(); this.WebsiteImages.Clear(); foreach (Match m in mc) { string value = m.Value; string source = GetSrcFromImgTag(value); if ((source.StartsWith("http")) || (source.StartsWith("www"))) { this.WebsiteImages.Add(source); } } } catch (Exception) { } } } private string GetSrcFromImgTag(string imgTag) { string src = string.Empty; if (imgTag != string.Empty) { bool foundStartQuote = false; int startOfSrcQuote = 0; bool foundEndQuote = false; int endOfSrcQuote = 0; for (int i = 0; i < imgTag.Length; i++) { if (!foundStartQuote) { if (imgTag.Substring(i, 3) == "src") { foundStartQuote = true; startOfSrcQuote = (5 + i); } } else { if (imgTag.Substring(i, 1) == " ") { foundEndQuote = true; endOfSrcQuote = i - 1; } if ((foundStartQuote) && (foundEndQuote)) { src = imgTag.Substring(startOfSrcQuote, (endOfSrcQuote - startOfSrcQuote)); break; } } } } return src; } private void RequestHTMLFromWebsite() { if (this.WebsiteUrl != null) { try { WebRequest request = WebRequest.Create(this.WebsiteUrl); request.Credentials = CredentialCache.DefaultNetworkCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); this.WebsiteHtml = reader.ReadToEnd(); reader.Close(); dataStream.Close(); } response.Close(); } catch (Exception) { } } } } }