Domanda

Background: I am building a C# Forms application that works with the Facebook API. My program can already successfully authenticate and retrieve a full list of friend ID's and names. I understand how to add these into a ListView control and this part is successful. This process takes a matter of seconds as my program requests and receives a small JSON file.

With a collection of friends ID's I am then able to get each freinds profile picture from https://graph.facebook.com/USER_ID/picture .

Does anyone have a solution on how I could add these internet sourced images into my ImageList while adding an Image Key so I can link them to a ListViewItem with the User ID ?

private void populate_Click(object sender, EventArgs e)
{
    var fb = new FacebookClient(_aT);
    dynamic friends = fb.Get("/me/friends");

    // Populate the ImageList
    ImageList avatars = new ImageList();

    foreach (dynamic item in friends.data)
    {
        var src = "https://graph.facebook.com/" + item.id + "/picture";

        // Add Image from src to avatars ImageList

    }


}

Please remember that I have a very large set of image to work through. Each Image should be a JPG of 50 x 50 Thanks.

È stato utile?

Soluzione

// Download the bitmap data using an instance of WebClient class.
using (WebClient webClient = new WebClient())
{
    foreach (var url in urls)
    {
        byte[] bitmapData;
        bitmapData = webClient.DownloadData(url);

        // Bitmap data => bitmap => resized bitmap.            
        using (MemoryStream memoryStream = new MemoryStream(bitmapData))
        using (Bitmap bitmap = new Bitmap(memoryStream))
        using (Bitmap resizedBitmap = new Bitmap(bitmap, 50, 50))
        {
            // NOTE:
            // Resized bitmap must be disposed because the imageList.Images.Add() method
            // makes a copy (!) of the source bitmap!
            // For details, see https://stackoverflow.com/questions/9515759/                
            imageList.Images.Add(resizedBitmap);
        }
    }
}

To increase the timeout of an instance of WebClient class please take a look at this answer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top