c#批量下载图片并存储到本地

        1、使用了OpencvSharp,进行图片的存储,所以项目需要引入OpencvSharp的包。

        2、准备好从别的数据库获得的图像url的txt,每行一个url地址。

        参考代码如下:

string txt = @"C:\\Users\\zyh\\Desktop\\test.txt";

int name = 1000;
Mat mat;
StreamReader sr = new StreamReader(txt, Encoding.Default);
string content;
while ((content = sr.ReadLine()) != null)
{
    try
    {
        System.Net.WebClient webClient = new System.Net.WebClient();
        System.IO.Stream stream = webClient.OpenRead(@content.ToString());
        if (stream != null)
        {
            mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(new Bitmap(Image.FromStream(stream)));
            Cv2.ImWrite("C:\\Users\\zyh\\Desktop\\r_g_pic\\" + name + ".jpg", mat);
            stream.Dispose();
            name++;
        }

        Console.WriteLine(content.ToString());
    }
    catch (Exception)
    {

    }
}

猜你喜欢

转载自blog.csdn.net/bashendixie5/article/details/113439176