使用c下载并保存图像
本文关键字:保存 图像 下载 使用 | 更新日期: 2023-09-27 18:26:30
我从wikipediaapi生成了三张图片。现在我想把它存储在我当前的目录中。使用以下代码,我可以成功创建名为的文件夹。但它只保存一个图像,即最后一个图像。我尝试了很多。但无法修复如何相应地保存三个图像
public static void Load_Image1(string name1, string name2, string name3,string LocationName)
{
var startPath = Application.StartupPath;
string Imagefolder = Path.Combine(startPath, "Image");
string subImageFolder = Path.Combine(Imagefolder, LocationName);
System.IO.Directory.CreateDirectory(subImageFolder);
//string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
List<PictureBox> pictureBoxes = new List<PictureBox>();
pictureBoxes.Add(Image1);
pictureBoxes.Add(Image2);
pictureBoxes.Add(Image3);
using (var wc = new System.Net.WebClient())
{
var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&format=json&iiprop=url&iiurlwidth=400&titles="+name1+"|"+name2+"|"+name3);
var response = wc.DownloadString(new Uri(uri));
var responseJson = JsonConvert.DeserializeObject<RootObject>(response);
List<string> urls = new List<string>();
foreach (KeyValuePair<string, Pageval> entry in responseJson.query.pages)
{
var url = entry.Value.imageinfo.First().thumburl;
urls.Add(url);
var hash = uri.GetHashCode();
string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
wc.DownloadFile(url, path);
}
for (int i = 0; i < pictureBoxes.Count; i++)
{
Image1.SizeMode = PictureBoxSizeMode.StretchImage;
Image2.SizeMode = PictureBoxSizeMode.StretchImage;
Image3.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBoxes[i].Load(urls[i]);
var hash = uri.GetHashCode();
string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
wc.DownloadFile(urls[i], path);
}
}
}
}
您正在将所有图像下载到磁盘上的同一文件名中,导致前两个图像被最后一个图像覆盖。问题是你的基本文件名是基于的
var hash = uri.GetHashCode();
这返回相同的值,因为它基于所有3个图像的url。更改为:
var hash = url.GetHashCode();
您实际上保存了所有图片,但使用相同的名称,这就是为什么文件系统中只剩下最后一张图片(您不断覆盖图像)。您应该在变量路径中使用一个唯一的标识符来区分图像,用不同的名称保存图像以避免覆盖
public static void Load_Image1(string name1, string name2, string name3,string LocationName)
{
var startPath = Application.StartupPath;
string Imagefolder = Path.Combine(startPath, "Image");
string subImageFolder = Path.Combine(Imagefolder, LocationName);
System.IO.Directory.CreateDirectory(subImageFolder);
//string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
List<PictureBox> pictureBoxes = new List<PictureBox>();
pictureBoxes.Add(Image1);
pictureBoxes.Add(Image2);
pictureBoxes.Add(Image3);
using (var wc = new System.Net.WebClient())
{
var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&format=json&iiprop=url&iiurlwidth=400&titles="+name1+"|"+name2+"|"+name3);
var response = wc.DownloadString(new Uri(uri));
var responseJson = JsonConvert.DeserializeObject<RootObject>(response);
List<string> urls = new List<string>();
foreach (KeyValuePair<string, Pageval> entry in responseJson.query.pages)
{
var url = entry.Value.imageinfo.First().thumburl;
urls.Add(url);
var hash = url.GetHashCode();
string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
wc.DownloadFile(url, path);
}
for (int i = 0; i < pictureBoxes.Count; i++)
{
Image1.SizeMode = PictureBoxSizeMode.StretchImage;
Image2.SizeMode = PictureBoxSizeMode.StretchImage;
Image3.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBoxes[i].Load(urls[i]);
}
}
}
}