使用API从上传到imgur的图片中检索网址 [英] Retrieving the url from an image uploaded to imgur using API
问题描述
大家好,
我试图通过电子邮件服务发送报告。但由于电子邮件的正文使用HTML,要插入图像,你需要在该段落中添加正确的URL。
如果是这样,我正在尝试实现imgur API来检索上传链接并使用它在字符串上,如下:
< img =>
上传我设法找到这个代码输出必要的值。但是我该如何检索imgur?。
这段代码实际上使用的是默认图像,如何更改它以转换图片框中的图像?
我尝试了什么:
我只需上传一个默认图片的代码要意识到的。无需检索第一个问题中需要使用的链接字符串。
使用( var w = new WebClient())
{
使用( var ms = new MemoryStream())
{
var values = new NameValueCollection
{
{ key, 2781d465a48c3e2},
{ image,Convert.ToBase64String (File.ReadAllBytes( @ Default.jpg))}
};
byte [] response = w.UploadValues( http://imgur.com/api/upload.xml,值);
Console.WriteLine(XDocument.Load( new MemoryStream(response)));
}
}
这给了我这个输出:
< data type =arraysuccess =1status =200>
< id> T2O8lwI< / id>
< title />
< description />
< datetime> 1496402171< / datetime>
< type> image / jpeg< / type>
< animated> false< / animated>
< width> 600< / width>
< height> 800< / height>
< size> 53452< / size>
< views> 0< / views>
< bandwidth> 0< / bandwidth>
< vote />
< favorite> false< / favorite>
< nsfw />
< section />
< account_url />
< account_id> 0< / account_id>
< is_ad> false< / is_ad>
< in_most_viral> false< / in_most_viral>
< tags />
< ad_type> 0< / ad_type>
< ad_url />
< in_gallery> false< / in_gallery>
< deletehash> CKmIUBffASpWHTW< / deletehash>
< name />
< link> http://i.imgur.com/T2O8lwI.jpg< / link>
< / data>
图像链接就在XML中;你需要阅读它。
var responseXml = XDocument.Load( new MemoryStream(response));
if (( int ?)responseXml.Root.Attributes( success)== 1 )
{
string imageUrl =( string )responseXml.Root.Element( link);
...
}
但正如我在评论中所提到的,将图像直接嵌入到消息中要简单得多。 / BLOCKQUOTE>
Hello guys,
Im trying to send reports through e-mail service. But since the email's body uses HTML, to insert an image, you need to assin that paragraph the right url.
If so, I'm trying to implement the imgur API to retrieve the upload link and use it on the string, like this:
<img= "">
To upload the image I managed to find this code that outputs the values necessary. But how can I retrieve the imgur ?.
This code actually uses a default image, how can i change it to convert the image from a picturebox?
What I have tried:
The code I have to simply upload a default image to imgur. Without retrieving the link string needed to be used in the 1st question.
using (var w = new WebClient()) { using (var ms = new MemoryStream()) { var values = new NameValueCollection { { "key", "2781d465a48c3e2" }, { "image", Convert.ToBase64String(File.ReadAllBytes(@"Default.jpg")) } }; byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values); Console.WriteLine(XDocument.Load(new MemoryStream(response))); } }
This gives me this output:
<data type="array" success="1" status="200"> <id>T2O8lwI</id> <title /> <description /> <datetime>1496402171</datetime> <type>image/jpeg</type> <animated>false</animated> <width>600</width> <height>800</height> <size>53452</size> <views>0</views> <bandwidth>0</bandwidth> <vote /> <favorite>false</favorite> <nsfw /> <section /> <account_url /> <account_id>0</account_id> <is_ad>false</is_ad> <in_most_viral>false</in_most_viral> <tags /> <ad_type>0</ad_type> <ad_url /> <in_gallery>false</in_gallery> <deletehash>CKmIUBffASpWHTW</deletehash> <name /> <link>http://i.imgur.com/T2O8lwI.jpg</link> </data>解决方案The image link is right there in the XML; you just need to read it.
var responseXml = XDocument.Load(new MemoryStream(response)); if ((int?)responseXml.Root.Attributes("success") == 1) { string imageUrl = (string)responseXml.Root.Element("link"); ... }
But as I mentioned in the comments, it would be far simpler to embed the image directly in the message.
这篇关于使用API从上传到imgur的图片中检索网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!