从三个 url 加载三个图像并显示在 3 个图片框中
Three Imges load from three url and show in 3 picturebox
我正在尝试从 3 个 url 中请求 3 个图像,然后尝试在三个中显示 picturebox.But 我的代码有问题。不胜感激!
private void button1_Click(object sender, EventArgs e)
{
var request1 = WebRequest.Create("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Schloss_Neuschwanstein_2013.jpg/300px-Schloss_Neuschwanstein_2013.jpg");
var request2 = WebRequest.Create("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Schloss_Neuschwanstein_2013.jpg/300px-Schloss_Neuschwanstein_2013.jpg");
var request3 = WebRequest.Create("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Schloss_Neuschwanstein_2013.jpg/300px-Schloss_Neuschwanstein_2013.jpg");
using (var response1 = request1.GetResponse())
using (var response2 = request2.GetResponse())
using (var response3 = request2.GetResponse())
using (var stream1 = response1.GetResponseStream())
using (var stream2 = response2.GetResponseStream())
using (var stream3 = response2.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream1);
pictureBox2.Image = Bitmap.FromStream(stream2);
pictureBox3.Image = Bitmap.FromStream(stream3);
}
}
}
}
你可以试试这个。
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load("http://i.imgur.com/ZkWyBo5.jpg?2");
pictureBox2.Load("http://i.imgur.com/ZkWyBo5.jpg?2");
pictureBox3.Load("http://i.imgur.com/ZkWyBo5.jpg?2");
}
由于未正确使用 using 语句,您的代码已挂起。请记住,在 using 语句中有一个范围。
如果您想使用您的代码:
private void button1_Click(object sender, EventArgs e)
{
var request1 = WebRequest.Create("http://i.imgur.com/ZkWyBo5.jpg?2");
var request2 = WebRequest.Create("http://i.imgur.com/ZkWyBo5.jpg?2");
var request3 = WebRequest.Create("http://i.imgur.com/ZkWyBo5.jpg?2");
using (var response1 = request1.GetResponse())
{
using (var stream1 = response1.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream1);
}
}
using (var response2 = request2.GetResponse())
{
using (var stream2 = response2.GetResponseStream())
{
pictureBox2.Image = Bitmap.FromStream(stream2);
}
}
using (var response3 = request3.GetResponse())
{
using (var stream3 = response3.GetResponseStream())
{
pictureBox3.Image = Bitmap.FromStream(stream3);
}
}
}
我正在尝试从 3 个 url 中请求 3 个图像,然后尝试在三个中显示 picturebox.But 我的代码有问题。不胜感激!
private void button1_Click(object sender, EventArgs e)
{
var request1 = WebRequest.Create("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Schloss_Neuschwanstein_2013.jpg/300px-Schloss_Neuschwanstein_2013.jpg");
var request2 = WebRequest.Create("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Schloss_Neuschwanstein_2013.jpg/300px-Schloss_Neuschwanstein_2013.jpg");
var request3 = WebRequest.Create("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Schloss_Neuschwanstein_2013.jpg/300px-Schloss_Neuschwanstein_2013.jpg");
using (var response1 = request1.GetResponse())
using (var response2 = request2.GetResponse())
using (var response3 = request2.GetResponse())
using (var stream1 = response1.GetResponseStream())
using (var stream2 = response2.GetResponseStream())
using (var stream3 = response2.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream1);
pictureBox2.Image = Bitmap.FromStream(stream2);
pictureBox3.Image = Bitmap.FromStream(stream3);
}
}
}
}
你可以试试这个。
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load("http://i.imgur.com/ZkWyBo5.jpg?2");
pictureBox2.Load("http://i.imgur.com/ZkWyBo5.jpg?2");
pictureBox3.Load("http://i.imgur.com/ZkWyBo5.jpg?2");
}
由于未正确使用 using 语句,您的代码已挂起。请记住,在 using 语句中有一个范围。
如果您想使用您的代码:
private void button1_Click(object sender, EventArgs e)
{
var request1 = WebRequest.Create("http://i.imgur.com/ZkWyBo5.jpg?2");
var request2 = WebRequest.Create("http://i.imgur.com/ZkWyBo5.jpg?2");
var request3 = WebRequest.Create("http://i.imgur.com/ZkWyBo5.jpg?2");
using (var response1 = request1.GetResponse())
{
using (var stream1 = response1.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream1);
}
}
using (var response2 = request2.GetResponse())
{
using (var stream2 = response2.GetResponseStream())
{
pictureBox2.Image = Bitmap.FromStream(stream2);
}
}
using (var response3 = request3.GetResponse())
{
using (var stream3 = response3.GetResponseStream())
{
pictureBox3.Image = Bitmap.FromStream(stream3);
}
}
}