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 get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
Related Article