919.459.6097 info@reso.org  

C# Photo Download Example

This example, using libRETS, shows how to download photos for listings as you’re pulling them from RETS, using a Visual Studio 05 (2.0) console application. In my previous entry, I referenced a ProcessListings(list) function in DownloadAllListings(string propType, int offset). The how-to for grabbing photos as you query listings from rets is below:

  • If you’re wondering why there’s a “middle man” function, it’s because we set it up initially to handle agent photo downloads and office logos as well. But, later we were informed rets doesn’t serve this info. So for your code, you could just call DownloadAllListingPhotos(listing.MlsNumber) from DownloadAllListings(string propType, int offset) directly in your code.

Our real estate customer’s website can handle up to 15 photos for each listing. This is where the lettering system comes in. It’s probably not the best solution, but it works. The most photos I’ve seen for a listing in RETS pulls has been 12. In this example, if the photo file name exists in the directory already, this code will overwrite it.

        /// <summary>
        /// Processes images, etc for each listing record.
        /// </summary>
        /// <param name="listings">ListingCollection to process.</param>
        public void ProcessListings(ListingCollection listings)
        {
            Console.WriteLine("\nin ProcessListings() *downloading all listing photos, takes quite a while...");
            foreach (Listing listing in listings)
            {
                // Per Rapattoni, RETS server does not currently serve agent or affiliate (office) photos
               DownloadAllListingPhotos(listing.MlsNumber);
            }
        }
 
        /// <summary>
        /// Downloads all listing photos per listing query 
        /// (query only returning certain listing statuses as of now, not full list)
        /// </summary>
        /// <param name="MLSNumber">MLSNumber of listing currently being queried</param>
        public void DownloadAllListingPhotos(int MLSNumber)
        {
            try
            {
                string photoFilePath = ConfigurationManager.AppSettings["ListingPhotoSavePath"].ToString();
                //taken from app.config in  connection example
                string CurrentMLS = MLSNumber.ToString();
 
                int intCurrentPhotoNo = 1;
 
                //loop through objects until obj is null. It does not error out if the objectID does not exist.
                ObjectDescriptor obj;
                do
                {
                    using (librets.GetObjectRequest request = new GetObjectRequest("Property", "Photo"))
                    {
                        request.AddObject(CurrentMLS, intCurrentPhotoNo);
                        GetObjectResponse response = this.Session.GetObject(request);
                        obj = response.NextObject();
                        if (obj != null)
                        {
                            string strFilename = string.Empty;
                            string currentPhotoLetter = string.Empty;
 
                            // Get the photo letter
                            currentPhotoLetter = GetPhotoLetter(intCurrentPhotoNo);
 
                            // Create the file name.
                            strFilename = MLSNumber + currentPhotoLetter + ".jpg";
 
                            // get the bytes of the downloaded image
                            byte[] imageBytes = obj.GetDataAsBytes();
 
                            // Create the full path to the file.
                            string fullPath = Path.Combine(photoFilePath, strFilename);
 
                            // Write the file.
                            File.WriteAllBytes(fullPath, imageBytes);
 
                            // Increment photo number.
                            intCurrentPhotoNo++;
                        }
                    }
                } while (obj != null);
            }
 
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 
        private static string GetPhotoLetter(int intCurrentPhotoNo)
        {
            string currentPhotoLetter = string.Empty;
 
            switch (intCurrentPhotoNo)
            {
                case 2:
                    currentPhotoLetter = "a";
                    break;
                case 3:
                    currentPhotoLetter = "b";
                    break;
                case 4:
                    currentPhotoLetter = "c";
                    break;
                case 5:
                    currentPhotoLetter = "d";
                    break;
                case 6:
                    currentPhotoLetter = "e";
                    break;
                case 7:
                    currentPhotoLetter = "f";
                    break;
                case 8:
                    currentPhotoLetter = "g";
                    break;
                case 9:
                    currentPhotoLetter = "h";
                    break;
                case 10:
                    currentPhotoLetter = "i";
                    break;
                case 11:
                    currentPhotoLetter = "j";
                    break;
                case 12:
                    currentPhotoLetter = "k";
                    break;
                case 13:
                    currentPhotoLetter = "l";
                    break;
                case 14:
                    currentPhotoLetter = "m";
                    break;
                case 15:
                    currentPhotoLetter = "n";
                    break;
                default:
                    break;
            }
            return currentPhotoLetter;
        }