Archive

Posts Tagged ‘C#’

Use Windows Desktop Search API Inside of Managed Code

April 17th, 2007
Starting from Windows Desktop Search (WDS) 3.0, a new helper API ISearchQueryHelper is added to help simplify the construction of index OLE DB connection string and search queries. The search service is implemented and the API is exposed as COM objects. Fortunately, there is a way for managed code to invoke the WDS too.
Download the Windows Search SDK. After unzip the package, the search interop assembly (Microsoft.Search.Interop.dll) is in the "Managed" folder. Add it to the reference inside of your solution and put “using Microsoft.Search.Interop; ” at the top of your C# code. Now it’s ready to write C# code and make use of WDS API. Some of the unmanaged and managed counterparts of classes are the following:
Unmanaged
Managed
ISearchManager
CSearchManager
ISearchCatalogManager
CSearchCatalogManager
ISearchQueryHelper
CSearchQueryHelper
 
The following sample code constructs a search query using ISearchQueryHelper to display the sender, recipients, and summary of the first 100 emails that contain a particular keyword (“share” in this example).
int maxSumLength = 100;
 
// Setup the catalog and search query helper
CSearchManager srchMngr = new CSearchManager();
CSearchCatalogManager srchCatMngr = srchMngr.GetCatalog("SystemIndex");
CSearchQueryHelper srchQueryHelper = srchCatMngr.GetQueryHelper();
 
// Assemble the query
srchQueryHelper.QuerySelectColumns = "System.Message.FromAddress, System.Message.ToAddress, System.Search.AutoSummary";
srchQueryHelper.QueryWhereRestrictions = "AND CONTAINS(System.Kind, ‘\"email\"’)";
string sqlQuery = srchQueryHelper.GenerateSQLFromUserQuery("share");
 
// Setup the OLE DB connection
OleDbConnection conn = new OleDbConnection(srchQueryHelper.ConnectionString);
conn.Open();
 
// Execute the query
OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);
OleDbDataReader srchResult = cmd.ExecuteReader();
 
// Process the search result and send to output
for (int i = 0; (i < 100) && srchResult.Read(); ++i)
{
                string fromAddr = "";
                string toAddr = "";
                string sumAddr = "";
 
                if (null != srchResult.GetValue(0) && !(srchResult.GetValue(0) is System.DBNull))
                {
                    string[] addrs = (string[])srchResult.GetValue(0);
                    fromAddr = "From: " + System.String.Join(",", addrs);
                }
                if (null != srchResult.GetValue(1) && !(srchResult.GetValue(1) is System.DBNull))
                {
                    string[] addrs = (string[])srchResult.GetValue(1);
                    toAddr = "To: " + System.String.Join(",", addrs);
                }
                if (null != srchResult.GetValue(2) && !(srchResult.GetValue(2) is System.DBNull))
                {
                    sumAddr = (string)srchResult.GetString(2);
                }
 
                textBox1.AppendText("(" + i + ") ");
                textBox1.AppendText(fromAddr + " " + toAddr + " ");
                textBox1.AppendText("Content: " + (sumAddr.Length <= maxSumLength ? sumAddr : sumAddr.Substring(0, maxSumLength)));
                textBox1.AppendText(Environment.NewLine);
}
 
srchResult.Close();
conn.Close();
           

Search Engine , , ,

Extract Favorite Websites Collection from Bookmark RSS feeds

February 25th, 2007

Bookmarking a website and bookmarking an article are different usage of the bookmarking tools. You may want to have a clean collection of all your favorite websites and on the other hand you may want to bookmark multiple good articles from the same website. This way your bookmark collection is a mixture of both. This is my little frustration of using sites like http://del.icio.us/. It’s great in collect articles and links, but not so great to keep a clean list of my favorite websites. Although I can use a different bookmarking tool (e.g., http://favorites.live.com/, which could sync with IE bookmarks) to collect just sites, it’d be nice if I could keep them in one place. So I developed a custom web control that could be put on your website to display all the websites of a list of http://del.icio.us/ RSS feeds in the order of popularity. Multiple articles of each websites are collapsed into one entry shown for that website, and the number of the articles bookmarked from that site is used as the indicator of the popularity of that site within the RSS feeds. So the more articles of a website that are bookmarked, the more popular the website is. In my example of using this web control, I put my bookmark feeds and the del.icios.us homepage hotlist feeds in so that I will get a list of all the websites either I bookmarked or on the hostlist.

<cc1:BookmarkedWebsites
    id
="WebCustomControl1_1"
    Url
="http://del.icio.us/rss/dbtu/; http://del.icio.us/rss/"
    runat
="server"></cc1:BookmarkedWebsites>

The "Url" property of the control specifies a list of RSS feeds to extract from. The delimiter is ";".

RSS.NET library is used to parse the RSS feed. The website part (domain) of the URL is extracted and used as the collapsing key.

protected override void RenderContents(HtmlTextWriter output)
{
    // Get each individual RSS feed
    char
[] delimiters = { ‘;’ };
    string[] feeds = Url.Split(delimiters);
   
    // Get all websites
    SortedDictionary
<string, int> sites =
        new
SortedDictionary<string, int>();
    foreach (string feedUrl in feeds)
    {
        RssFeed feed = RssFeed.Read(feedUrl);
        foreach (RssChannel channel in feed.Channels)
        {
            foreach (RssItem item in channel.Items)
            {
                string link = item.Link.Scheme + "://" +
                    item.Link.Host;
                if (sites.ContainsKey(link))
                {
                    sites[link]++;
                }
                else
                {
                    sites[link] = 1;
                }
            }
        }
    }

    // Reorder it on the number of occurrence (thus popularity)
    KeyValuePair
<string, int>[] popSites =
        new
KeyValuePair<string,int>[sites.Count];
    sites.CopyTo(popSites, 0);
    Array.Sort(popSites, new SitePopularityComparer());
    foreach (KeyValuePair<string, int> s in popSites)
    {
        output.Write("( " + s.Value + " ) <a href=\"" +
            s.Key + "\">"+ s.Key + "</a><br />");
    }
}

public class SitePopularityComparer : IComparer
{
    int IComparer.Compare(object x, object y)
    {
        return ((KeyValuePair<string, int>)y).Value -
                ((KeyValuePair<string, int>)x).Value;
    }
}

The web control looks like the following:

 

Furthermore, it would be cooler to be able to sync this website list into the browser bookmark so that you can use them as your reading list even more conveniently.

The prototype source code can be downloaded here: MyFavoriteWebSites.zip.

.NET , , , ,

Create thumbnails of PowerPoint presentations in C#

February 7th, 2007

It’s sometimes useful to generate JPEG thumbnails of office documents programmatically.  With Office object library, it’s very convenient to do this in C#.NET.  If it’s for a quick and dirty task, an easy way is to convert the office document into a webpage and then use a WebBrowser control to generate the thumbnail.  For a more performance oriented task, customized controls subclassed from the Office classes could be written to dump the bitmaps directly into images.  The following is a sample implementation of the first approach.  Don’t forget to add the reference to COM object library "Microsoft PointPoint 12.0 Object Library".

Creating Thumbnails for PowerPoint Presentations
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

private void pptThumbnail(string sourceFile, string targetFile, int thumbW, int thumbH)
{
    // Open the document and convert into HTML pages
    PowerPoint.ApplicationClass oApp = new PowerPoint.ApplicationClass();
    PowerPoint.Presentation oDoc = oApp.Presentations.Open(
        sourceFile,
        Microsoft.Office.Core.MsoTriState.msoTrue, // read only
        Microsoft.Office.Core.MsoTriState.msoTrue, // untitled
        Microsoft.Office.Core.MsoTriState.msoFalse); // with window
    string tmpHtmlFile = System.IO.Path.GetTempFileName() + ".html";
    oApp.Presentations[1].SaveCopyAs(
        tmpHtmlFile,
        PowerPoint.PpSaveAsFileType.ppSaveAsHTML, // format
        Microsoft.Office.Core.MsoTriState.msoTrue); // embed true type font

    // Create the thumbnail from the HTML pages
    Size browserSize = new Size(800, 800);
    WebBrowser browser = new WebBrowser();
    browser.Size = browserSize;
    browser.Navigate(tmpHtmlFile);
    while (WebBrowserReadyState.Complete != browser.ReadyState)
    {
        Application.DoEvents();
    }
    Bitmap bm = new Bitmap(browserSize.Width, browserSize.Height);
    browser.DrawToBitmap(bm,
        new Rectangle(0, 0, browserSize.Width, browserSize.Height));
    Bitmap thumbnail = new Bitmap(thumbW, thumbH);
    Graphics g = Graphics.FromImage(thumbnail);
    g.DrawImage(
        bm,
        new Rectangle(0, 0, thumbnail.Width, thumbnail.Height),
        new Rectangle(0, 0, browserSize.Width, browserSize.Height),
        GraphicsUnit.Pixel);
    thumbnail.Save(targetFile);
}

.NET , , ,