Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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>
Posted
Updated 2-Jun-17 3:15am
v2
Comments
Richard Deeming 2-Jun-17 9:11am    
Why not simply embed the image in the message? That way, it will display even if the user has blocked downloading external images.

Sending Email Using Embedded Images[^]

1 solution

The image link is right there in the XML; you just need to read it.
C#
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.
 
Share this answer
 
Comments
Scribling Doodle 5-Jun-17 4:17am    
Since I directly want my application to retrieve the "link" to store it on a string, this way seems much more simple. Thanks in advance for the great answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900