c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
asp.net MVC

How to share image to twitter post using asp.net MVC?

| | ASP-NET , CSharp , MVC

In this article we will describe how to share a post along with image in twitter. We can share image with SendTweetWithMedia() method by passing image as stream. Inorder to send image as stream we need to convert it.

In my previous example I have explained how to share twitter post please refer before proceeding.

Import following namespaces,

using System.Net;
using System.IO;
using TweetSharp;
using System.Drawing;
using System.Drawing.Imaging;

Example:

private void sharetwitter ()
    {
              var oauth_consumer_key = "your API key";
              var oauth_consumer_secret = "Your API secret key";
              string token = "access token";
              string tokenSecret = "access token secret ";
              var post = db.Posts.Where(p => p.PostId ==id).FirstOrDefault();
              StringBuilder str = new StringBuilder();
              str.AppendLine(post.Title.Trim());
              str.AppendLine("learn .net a lot of helpful programming stuffs.");
              var service = new TweetSharp.TwitterService(oauth_consumer_key,oauth_consumer_secret);
              service.AuthenticateWith(token, tokenSecret);
              string url = "http://www.infinetsoft.com/Images/logoinfi.png";
              service.SendTweetWithMedia(new SendTweetWithMediaOptions
              {
                   Status = str.ToString(),
                   Images = new Dictionary<string, Stream> { { "infinetsoft", urltostream(url) } }
               });
             TempData["SuccessMessage"] = "tweeted success";
    }

private Stream urltostream(string url)
  {
          WebClient wc = new WebClient();
          byte[] bytes = wc.DownloadData(url);
          Stream stream = new System.IO.MemoryStream(bytes);
          return stream;
    }