[C#]从网站获取图像到C# [英] [C#] Get image from website to C#

查看:72
本文介绍了[C#]从网站获取图像到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站在这里: http://pixplorer.co.uk/#/ [ ^ ]



API为: http://pixplorer.co.uk/getimage/



API网站上的文字:

The website is here: http://pixplorer.co.uk/#/[^]

The API is: http://pixplorer.co.uk/getimage/

The text on API site:

{ "0": { "imglink": "http://t3.gstatic.com/images?q=tbn:ANd9GcRykxeuR9P3M-5QOrHhf0xxWER44ab1MQn2ByVvqBjSiI8_em9yk5OGa4Q", "word": "stamp" } }





如何在C#表格的图片框中显示imglink图片?



How can i show that "imglink" picture in a picture box on my C# form?

推荐答案

我的解决方案如下:

我假设你有:

-added包Json.NET通过nuget

-你的控件有textbox:textBox1 ,button:button1 with clickhandler:button1_Click和图片框:pictureBox1



然后简单的处理程序看起来像这样:



My solution below:
I assume that you have:
-added package Json.NET through nuget
-on your control there is textbox:textBox1, button:button1 with clickhandler: button1_Click and picture box:pictureBox1

then simplyfied handler would look something like this:

private void button1_Click(object sender, EventArgs e)
     {
         WebRequest wr = WebRequest.Create("http://pixplorer.co.uk/getimage/" + string.Join("+",this.textBox1.Text.Split(' ')));
         WebResponse res = wr.GetResponse();
         using (StreamReader reader = new StreamReader(res.GetResponseStream()))
         {
             string json = reader.ReadToEnd();
             dynamic images = JsonConvert.DeserializeObject(json);
             this.pictureBox1.Load(images["0"].imglink.Value);
         }
     }





用法:

输入关键字对于文本框中的图像并按下按钮,随机图片出现在PictureBox中。



Usage:
Enter your keywords for image in textbox and hit the button, random picture appears in PictureBox.


要检索图片的链接,您需要制作一个简单的WebRequest表格:



//获取一张猫的照片

http://pixplorer.co.uk/getimage/cat



这将返回您需要的编码字符串,您可以解析它并加载它,如下所示。
To retrieve the link to a picture requires you need to make a simple WebRequest of the form:

// to get a picture of a cat
http://pixplorer.co.uk/getimage/cat

That will return the coded string you need, and you can parse it and load it as shown below.
// requires: using System.Net;
// requires using System.IO;

private void GetAPic_Click(object sender, EventArgs e)
{        
    WebRequest request = HttpWebRequest.Create("http://pixplorer.co.uk/getimage/cat");
    
    var response = (HttpWebResponse) request.GetResponse();
    
    string path;
    
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        path = reader.ReadToEnd();
    
        path = path.Substring(path.IndexOf("http:"));
    
        path = path.Remove(path.IndexOf("\",\n        \"word\""));
    }
    
    using (Stream picStream = new MemoryStream(new WebClient().DownloadData(path)))
    {
        pictureBox1.Image = Image.FromStream(picStream);
    }
}


网站API提供的文本称为JSON。用于以文本形式提供和访问数据和资源。



http:// json .org / [ ^ ]



您的代码可以进一步格式化为



The text provided by the website's API is called JSON. And it is used to provide and access data and resources in form of text.

http://json.org/[^]

Your code can be further formatted as this

{ 
   "0": { 
      "imglink": "http://t3.gstatic.com/images?q=tbn:ANd9GcRykxeuR9P3M-5QOrHhf0xxWER44ab1MQn2ByVvqBjSiI8_em9yk5OGa4Q", 
      "word": "stamp" 
   } 
}





JSON是一种以文本形式发送回复的格式。您可以看到,这些属性和结果是以文本的形式而不是它们自己的二进制响应类型。 C#有许多库可以处理JSON数据。其中一些是由.NET开发人员提供的,但我个人喜欢Newtonsoft.JSON库。您可以从NuGet库获得它。



您可以使用此代码库来提取它。确保响应是一个字符串。





JSON is a format to send the responses in the form of text. You can see, that these properties and results are in the form of text rather than their own types of binary responses. There are many libraries for C# to work with JSON data. Some of them are provided by .NET developers, but personally I like the Newtonsoft.JSON library. You can get that from NuGet library.

You can use this code of library, to extract it. Make sure the response is a string.

// create a new class as
class Response {
   public string imglink { get; set; }
   public string word { get; set; }
}





您还可以进一步将数据(字符串)转换为响应对象为





You can further more, convert the data (string) to Response object as

// take the response as the string
string resp = "your response from API here, as string";
// convert the string to the response, fill the properties
Response response = JsonConvert.DeserializeObject<response>(resp);





如何在<$ c $中显示URL中的图像c> PictureBox



完成后你就取出了数据。将其传递给图像的来源。如果您正在使用PictureBox(Win Forms),则将此字符串作为ImageLocation传递为





How to show the Image from URL in the PictureBox.

After that is done and you've taken out the data. Pass it to the source for the image. If you're using PictureBox (Win Forms) then pass this string as ImageLocation as

// myPictureBox = the picture box control
// imgLink = the image link from the response
myPictureBox.ImageLocation = imgLink;





在此处阅读更多内容: http ://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.imagelocation(v = vs.110).aspx [ ^ ]


这篇关于[C#]从网站获取图像到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆