asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Building an Image Keyword System
By Ziran Sun
Rating: 3.9 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

  • download source code
  • Why Build an Image Keyword System?

    Unlike text-based file formats (HTML, XML, plain text and even PDF and MS Word documents), image files aren't made up of text. This makes searching for an image file by keyword difficult. Instead of being able to simply open the file to see what words it contains, we're stuck looking at the text around it and other "metadata" to determine the image's meaning. If you're dealing with just images, then there is no surrounding text or metadata... so what should you do if you need to deal with a lot of images on a routine basis?

    Well, you could give them all really descriptive file names. The problem with that is that descriptive names means long names and dealing with long file names can be a pain... especially on the web. Creating a hyperlink or image tag for a file whose name is a hundred characters long (and includes spaces) is not going to get you into anyone's good graces. Aside from using long, descriptive filenames, building a simple keyword system to associate keywords with the images is probably the easiest option. That's what this article is going to cover.

    Database Design

    Okay, so we've decided to build a system to hold keywords that describe each image in our image library. So where do we store these keywords? Because they're designed to manage data and are easy to query, a database seems the natural choice. A first pass at a table design usually leads to something like this:

    Some sample data in this table might look like this:

    While this design is nice and simple, it does have some problems. First of all, it's not clear from the design in what format keywords should be entered. In one record they're comma-delimited and in the next there are no commas. There's also nothing to keep keywords uniform. The keywords for the image "reddog.gif" were probably meant to be "red" and "dog", but a typo has left "dog" misspelled. Luckily the misspelled "dogg" actually contains the correct spelling of "dog", but it'd only be sheer luck that the image would come up in a search for the word "dog". Finally, because the amount of data is so small it's also fairly obvious that "wetdog.gif" and "reddog.gif" were both meant to be associated with the keyword "dog", but as the size of the data set grows it'll be harder to determine which images are related. This makes searching for secondary images with keywords similar to a primary image more complicated then it should be.

    So what's to be done? Well first off, we move the keywords to a table of their own and give each keyword an integer for a primary key. This allows us to correct keyword typos without destroying associations that may have already been formed. Because each image can have many keywords and keywords can belong to many images, the easiest way to handle these relationships is via a relationship table that does nothing but relate images to keywords. So we end up with three tables:

    Notice that both the image and keyword tables have primary key identity columns, while the imagekeyword relationship table uses a combination of ImageId and KeywordId to form a primary key. This assures that each image is associated with each keyword only once.

    So, looking at the sample data above, the image table tells us that image file "19150545.jpg" has an id of 1. Then, by checking the imagekeyword table we find out that the image with an id of 1 is associated with keyword ids 1 and 3. Finally the keyword table tells us that keywords 1 and 3 are "house" and "tree" which would lead us to believe that image file "19150545.jpg" should be a picture containing a house and a tree.

    At first this method may seem like a lot more work, but luckily that's really no the case. Aside from a couple ugly joins and queries when searching for multiple keywords, this design actually makes most queries easier and more efficient!

    The Web Pages

    With the database design in place and the tables full of our sample data, now we get to the fun part... actually writing some code to get this thing running. Building a form to insert a new image or keyword is trivial and so I'm not going to waste your time going over them. As the sample stands there are three main pages:

    1. Show All Images (default.aspx): This page simply shows all the images in the database. Click on an image to go to the image details page for that image.

      This page really has very little to do with the sample, but it serves as a nice start page and an easy way to get to the detail page for any image. You can get the same result by going to the search page and selecting every keyword checkbox and using the "Any" search option, but the code to this page is much simpler then the search page.

    2. Image Details (details.aspx): Displays the image and associated details. Also displays / allows you to edit the keywords that are associated with the selected image.

      This page shows you all the details of the selected image and at the same time allows you to associate/disassociate keywords with the current image. Depending on your situation you might want the editing to be locked up in an administrative area, but as it stands currently anyone who can see the image details page can edit the keyword associations.

    3. Keyword Search (search.aspx): The search page allows you to search the images based on one or more keywords.

      This is sort of the whole point... an easy way to search for images based on keyword. You can change the interface to work any way you want (text links of each keyword would work well), but as it stands I'm using checkboxes because it's the easiest way to select multiple keywords easily. As I mentioned earlier, the query for multiple keywords is a little ugly, but once it's written you don't need to worry about it... just use the web page.

    Well that's it... I'm not going to bother posting the code here in the body of the article because after all it is code... it's meant for computers to read - not people!

    The Code

    You can download the code for the web pages above, the sample images, the database creation script (for SQL Server), and the actual data exported to .csv files all zipped up in a nice little package from: here (245 KB).

    Conclusion

    I hope you found the concept interesting and if the code pertains to something you're working on, please download it and take a look. I'll be disappointed if I'm the only one that thinks the keyword editing section on the image detail page is sort of cool!

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    Proposion N2N
    Proposion N2N connects Microsoft .NET applications to Lotus Notes and Lotus Domino databases. This ADO.NET managed data provider allows you to perform blindingly fast queries and updates of Notes data from ASP.NET pages, .NET web services, Windows, or Mobile applications. An innovative SQL-like query language leverages the unique features of Notes and makes collaborative software accessible to relational database programmers.
    [Top]
    Other Articles
    Apr 7, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
    In the second part of of his article on using MySQL with ASP.NET, Ziran Sun covers how to add a new MySQL user to the database server, assign the user the appropriate permissions, connect to the database, and build a simple ASP.NET page to perform a query.
    [Read This Article]  [Top]
    Feb 10, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 1
    Back in the days of classic ASP, if you were building a database-driven web site, your choice was either to invest a lot of money to get a copy of Microsoft SQL Server (or some other enterprise-ready database) or invest a lot of time finding a way to deal with the performance and scalability limitations of Microsoft Access. Luckily these days there's another viable alternative: MySQL.
    [Read This Article]  [Top]
    Jan 27, 2005 - Moving a Database from SQL Server 7.0 to SQL Server 2000
    Moving or copying a SQL Server database from one machine to another requires a lot of preparation in order to ensure a smooth transfer. In this article, Dina Fleet Berry examines the different methods and highlights the different issues associated with each of them.
    [Read This Article]  [Top]
    Jan 6, 2005 - Debugging a SQL Stored Procedure from inside SQL Server 2000 Query Analyzer
    There are many times when using SQL Server 2000 Query Analyzer to debug SQL statements is a better choice than debugging in Visual Studio .NET. In this article, Dina Fleet Berry explains why and walks you through the debugging process step-by step.
    [Read This Article]  [Top]
    Nov 24, 2004 - Persisting .NET Objects to SQL Server Using SQLXML and Serialization
    As a follow up to his article on retrieving objects from SQL Server using SQLXML and serialization, Gianluca Nuzzo discusses saving objects back to SQL Server using a schema definition file and updategrams.
    [Read This Article]  [Top]
    Sep 14, 2004 - Transaction Processing in ADO.NET 2.0
    One area that stands out when comparing ADO.NET 1.x to ADO.NET 2.0 is transaction processing. Bill Ryan shows just how easy transaction processing has become with the TransactionScope object in ADO.NET 2.0.
    [Read This Article]  [Top]
    Sep 8, 2004 - Custom Object Data Binding with .NET
    Developers often use brute force coding to marshal data between the GUI and application objects. In this article, Luther Stanton explains how to use .NET's out-of-the box data-binding functionality to make this job much easier.
    [Read This Article]  [Top]
    Sep 2, 2004 - Queue MSMQ Messages from SQL Server
    Learn how to create a console application to queue a message in Microsoft Message Queuing (MSMQ) and then use an extended stored procedure to call the console application from a SQL Server trigger.
    [Read This Article]  [Top]
    Aug 30, 2004 - Tuning Up ADO.NET Connection Pooling in ASP.NET Applications
    Connection pooling increases the performance of Web applications by reusing active database connections instead of creating a new connection with every request. This article shows how to monitor the connection pool, diagnose a potential problem, and apply the appropriate fix.
    [Read This Article]  [Top]
    Jul 13, 2004 - Retrieving Objects from SQL Server Using SQLXML and Serialization
    This article will describe how to design a data access layer for a set of entities. You'll learn how to write an XSD schema and design two simple helper classes -- one for reading an XML stream from SQL Server using SQLXML and another for deserializing the XML stream.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers