首页 > 其他分享 >获取图片并预览

获取图片并预览

时间:2022-09-30 15:33:04浏览次数:77  
标签:预览 photo List System 获取 new using net 图片

环境介绍:

  • 代码工具: Visual Studio 2022
  • 开发框架: .net formwork 4.7.2

开发须知

当前我所了解的有.net core以及.net formwork两种框架
.net formwork:
这个是微软专用的框架,简单来说你所生成的可执行程序在其他电脑上是可以直接运行的,不需要安装环境,因为默认就已经有了。限制就是只能在windows上运行,根据选择的框架不同受限不同的windwos操作系统。默认高版本的.net formwork兼容低版本的.net formwork。

.net core:
这个框架适用于跨平台(windwos\linux\macos),但是前提是需要安装.net core的环境。简单来,如果你开发的可执行程序需要在其他电脑上运行,那么就必须在需要运行的电脑上安装.net core环境,要不然执行不了 -. -

创建项目

打开Visual Studio 准备创建项目
image

创建一个net formwork项目
image

image

创建窗体

image

创建一个类文件

image

代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace photo
{
    public static class PhotoGril
    {
        static List<string> photo_path_name = new List<string>();
        public static List<string> GetPhoto()
        {

            // 清空目录
            if (Directory.Exists("D:\\Photo_Gril_2022_0923\\gril\\")) {
                DirectoryInfo di = new DirectoryInfo("D:\\Photo_Gril_2022_0923\\gril\\");
                di.Delete(true);
            }
                
            //List<string> tp = new List<string>();

            string url = "https://t2cy.com/acg/cos/";

            // 存放前20页的地址
            List<string> urlList = new List<string>();

            //
            List<string> page = new List<string>();

            //
            List<string> photoList = new List<string>();

            // 创建web对象
            WebClient request = new WebClient();

            //
            request.Encoding = System.Text.Encoding.UTF8;

            // 第一页默认的Url
            urlList.Add(url + "index.html");

            // 生成前20页的链接列表
            for (int i = 2; i < 5; i++)
            {
                urlList.Add(url + "index_" + i + ".html");
            }

            for (int i = 0; i < urlList.Count; i++)
            {
                string sw = request.DownloadString(urlList[i]);
                //<a href=""(.+?)"".+?block;"></a>
                MatchCollection tiqu = Regex.Matches(sw, @"<a href=""(.+?)"".+?");
                foreach (Match tiqu1 in tiqu)
                {
                    //将杂项链接去除
                    if (tiqu1.Groups[1].Value.Length < 36)
                    {

                    }
                    else
                    {
                        if (page.Contains(@"https://t2cy.com" + tiqu1.Groups[1].Value))
                        {

                        }
                        else
                        {
                            page.Add(@"https://t2cy.com" + tiqu1.Groups[1].Value);
                        }

                    }
                }

            }

            // 获取图片
            for (int i = 0; i < page.Count; i++)
            {
                string swsa = request.DownloadString(page[i]);//下载页面
                //^<p><img src=""(.+?)"" alt="".+?cos""/></p>$
                MatchCollection jihe34 = Regex.Matches(swsa, @"<p><img src=""(.+?)"".+?");//提取页面里图片链接
                //int n = 0;
                //循环集合提取图片li链接
                foreach (Match abcd in jihe34)
                {
                    //n++;
                    if (true)
                    {
                        photoList.Add(@"https://t2cy.com" + abcd.Groups[1].Value);
                    }
                }
            }

            // 下载图片

            string photodir = @"D:\\Photo_Gril_2022_0923\\gril\\";
            Directory.CreateDirectory(photodir);

            //List<string> photo_path_name = new List<string>();


            for (int j = 0; j < photoList.Count; j++)
            {

                try
                {
                    request.DownloadFile(photoList[j], photodir + j + ".jpg");
                    photo_path_name.Add(photodir + j + ".jpg");
                }
                catch
                {

                }
            }

            return photo_path_name;

        }            
    }
}

窗体逻辑代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace photo
{
    public partial class Form1 : Form
    {

        public static List<string> photo = new List<string>();

        int clock = 0;

        public Form1()
        {
            InitializeComponent();   
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (clock == 0)
            {
                pictureBox1.Image = Image.FromFile(photo[0]);
                MessageBox.Show("妹子在下一页");
            }
            else {

                clock = clock - 1;
                pictureBox1.Image = Image.FromFile(photo[clock]);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("正在获取资源。。。。");
            List<string> urlList = PhotoGril.GetPhoto();
            MessageBox.Show("资源获取完毕");
            photo = urlList;
            pictureBox1.Image = Image.FromFile(photo[0]);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clock = clock + 1;
            pictureBox1.Image = Image.FromFile(photo[clock]);

        }
    }
}

参考链接

https://blog.csdn.net/sharenfish/article/details/121597049

标签:预览,photo,List,System,获取,new,using,net,图片
From: https://www.cnblogs.com/shangcc205/p/16729812.html

相关文章