English 中文(简体)
从C#的tw中获取照片?
原标题:Get photos from twitter in C#?

I m trying to implement a feature in a .Net WPF application to automatically display photos from twitpic when users post them to my hashtag.

我是利用Twitter2普森图书馆的,从我搜索过来的所有信条都作为t.co链接(我认为这来自Twitter)。 我不知道如何将这些链接平分,以获取图像,将其发送用户。

最佳回答

我不知道“Twitter2”普森软件是如何运作的,但我认为你可以以两种方式解决你的问题:

  1. Search in Official Twitter API something that helps you to parse such links (look at Tweet Entities);
  2. Download the content of that URL yourself and parse it;

关于第二种做法,我要建议的是如下功能(这一教程基于我所看到的头一种情况,即:只有TwitPic图像的画面:

private static Uri GetPicture(string twitterUri)
{
    using (var webClient = new WebClient())
    {
        string html = webClient.DownloadString(twitterUri);
        int imgIndex = html.IndexOf("<img class="photo" id="photo-display"");
        int srcStartIndex = html.IndexOf("src", imgIndex) + 5;
        int srcEndIndex = html.IndexOf(""", srcStartIndex);
        string imgSrc = html.Substring(srcStartIndex, srcEndIndex - srcStartIndex);
        return new Uri(imgSrc);
    }
}

你们可以想象的是:

Uri imgUri = GetPicture("http://t.co/RQu9hZn8"); // this is a real image
问题回答

你可以依靠Twitter归还的媒体实体数据。

你们可以看到一个样本,并解释一下,我在这里贴出一段话:

样本中不包括该软件,但有一个新的<密码>TwitterMediaEntity类别,提供关于图像和录像的信息。

all attach media and audio URL in any tweet will be in entitiesmedia and media will be array of items that attach with the post if you want to get path of first attached item as suppose you have your tweets saved in enumerableTwitts your could will be

foreach (dynamic t in enumerableTwitts)
            {
 dynamic media = t["entities"]["media"];
                        string media_url = media[0]["media_url"].ToString();
}

你们应当使用捕捉物,因为有些 t子没有附上档案。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签