- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Lấy và download tất cả link hình ảnh từ đường dẫn website
Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách get link tất cả hình ảnh từ một đường dẫn website trên lập trình C#, Winform.
[C#] Download All Image From Link Website
Dưới đây là giao diện demo ứng dụng tải tất cả hình ảnh từ website:
- Đầu tiên, các bạn chọn đường dẫn vào và bấm nút Get Images => trả về danh sách các đường dẫn hình ảnh từ link URL.
- Tiếp đến các bạn bấm nút Download All để tải hình ảnh về ổ đĩa máy tính của bạn.
Full source code Download Image C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FetchAllImageFromWebsite
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}
private void btnGetImages_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
listImages.Items.Clear();
foreach (string image in FetchImages(txtURL.Text))
{
listImages.Items.Add(image);
}
this.Cursor = Cursors.Default;
}
public List<string> FetchImages(string Url)
{
List<string> imageList = new List<string>();
if (!Url.StartsWith("http://") && !Url.StartsWith("https://"))
Url = "http://" + Url;
var htmlDoc = new HtmlAgilityPack.HtmlWeb().Load(Url);
var htmlData = htmlDoc.DocumentNode.OuterHtml;
string imageHtmlCode = "<img";
string imageSrcCode = @"src=""";
int index = htmlData.IndexOf(imageHtmlCode);
while (index != -1)
{
//Remove previous data
htmlData = htmlData.Substring(index);
//Find the location of the two quotes that mark the image's location
int brackedEnd = htmlData.IndexOf('>'); //make sure data will be inside img tag
int start = htmlData.IndexOf(imageSrcCode) + imageSrcCode.Length;
int end = htmlData.IndexOf('"', start + 1);
//Extract the line
if (end > start && start < brackedEnd)
{
string loc = htmlData.Substring(start, end - start);
//Store line
imageList.Add(loc);
}
//Move index to next image location
if (imageHtmlCode.Length < htmlData.Length)
index = htmlData.IndexOf(imageHtmlCode, imageHtmlCode.Length);
else
index = -1;
}
//Format the image URLs
for (int i = 0; i < imageList.Count; i++)
{
string img = imageList[i];
string baseUrl = GetBaseURL(Url);
if ((!img.StartsWith("http://") && !img.StartsWith("https://"))
&& baseUrl != string.Empty)
img = baseUrl + "/" + img.TrimStart('/');
imageList[i] = img;
}
return imageList;
}
private string GetBaseURL(string Url)
{
int inx = Url.IndexOf("://") + "://".Length;
int end = Url.IndexOf('/', inx);
string baseUrl = string.Empty;
if (end != -1)
return Url.Substring(0, end);
else
return string.Empty;
}
private void listImages_SelectedValueChanged(object sender, EventArgs e)
{
string curItem = listImages.SelectedItem.ToString();
picImage.LoadAsync(curItem);
}
private void btnDownload_Click(object sender, EventArgs e)
{
var firstname = "";
foreach(var item in listImages.Items)
{
using (WebClient webClient = new WebClient())
{
try
{
string fileName = Path.GetFileName(new UriBuilder(item.ToString()).Path);
webClient.DownloadFile(item.ToString(), "images\\" + fileName);
firstname = fileName;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
ExploreFile(Application.StartupPath + $"\\images\\{firstname}");
}
public bool ExploreFile(string filePath)
{
if (!System.IO.File.Exists(filePath))
{
return false;
}
filePath = System.IO.Path.GetFullPath(filePath);
System.Diagnostics.Process.Start("explorer.exe", string.Format("/select,\"{0}\"", filePath));
return true;
}
}
}
Thanks for watching!