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;
}
Post your comments / questions
Recent Article
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
Related Article