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
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
- Remove all special characters, punctuation except spaces from string - Python
- OSError: cannot write mode RGBA as JPEG- Python
- expected str, bytes or os.PathLike object, not JpegImageFile - Python
Related Article