Example #1
0
    public static void clr_GetADusersPhotos(SqlString ADpath, SqlString ADfilter)
    {
        //System.IO.StreamWriter file = Util.CreateLogFile();

        SearchResultCollection results = null;
        Int32 itemcount = 0;
        try
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("ObjectGUID", typeof(Guid));
            tbl.Columns.Add("Width", typeof(int));
            tbl.Columns.Add("Height", typeof(int));
            tbl.Columns.Add("Format", typeof(string));
            tbl.Columns.Add("Photo", typeof(byte[]));
            DataRow row;

            DirectoryEntry entry = new DirectoryEntry((string)ADpath);
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.Filter = (string)ADfilter;
            searcher.PageSize = 500;

            results = searcher.FindAll();
            foreach (SearchResult searchResult in results)
            {
                itemcount++;
                DirectoryEntry item = searchResult.GetDirectoryEntry();

                PropertyValueCollection prop = Util.GetADproperty(item, "thumbnailphoto");
                if (prop == null)
                    continue;

                // Get image size
                ImgSize imgsize = new ImgSize(0, 0, "xxx");
                try
                {
                    imgsize = ImageHeader.GetDimensions((byte[])prop[0]);
                }
                catch(Exception ex)
                {
                    SqlContext.Pipe.Send("Warning: Get image size failed for user (" + Util.GetDistinguishedName(item) + ")"
                        + " Exception: " + ex.Message);
                }

                row = tbl.NewRow();
                row[0] = item.Guid;
                if (!imgsize.IsEmpty()) // Image size will be NULL unless size has been read from the image header.
                {
                    row[1] = imgsize.Width;
                    row[2] = imgsize.Height;
                    row[3] = imgsize.Format;
                }
                row[4] = prop[0];
                tbl.Rows.Add(row);
            }

            // Return dataset to SQL server.
            ReturnDatasetToSqlServer(tbl);
        }
        catch (System.Runtime.InteropServices.COMException)
        {
            SqlContext.Pipe.Send("COMException in clr_GetADusersPhotos. ItemCounter = " + itemcount.ToString());
            throw;
        }
        catch (InvalidOperationException)
        {
            SqlContext.Pipe.Send("InvalidOperationException in clr_GetADusersPhotos. ItemCounter = " + itemcount.ToString());
            throw;
        }
        catch (NotSupportedException)
        {
            SqlContext.Pipe.Send("NotSupportedException in clr_GetADusersPhotos. ItemCounter = " + itemcount.ToString());
            throw;
        }
        catch (Exception)
        {
            SqlContext.Pipe.Send("Exception in clr_GetADusersPhotos. ItemCounter = " + itemcount.ToString());
            throw;
        }
        finally
        {
            if (null != results)
            {
                results.Dispose();  // To prevent memory leaks, always call
                results = null;     // SearchResultCollection.Dispose() manually.
            }
        }
        //file.Close();
    }