Click here to Skip to main content
Click here to Skip to main content

Photoshop Preview Handler for Windows Vista

By , 4 Apr 2007
 
Screenshot - PreviewHandler.jpg

Introduction

Preview handler is the latest methodology in Microsoft Windows Vista™, which provide a rich, interactive, and read-only preview of a file without having to launch the associated application. The in-place interactive previews of the file are available in the Explorer windows, common dialogs, and search results. The core feature is integrated into Windows Vista Shell which allows developers to extend this functionality by writing managed code add-ins i.e. preview handlers for custom files.

For example, "a Microsoft Word 2007 Preview Handler" will enable a user to view and interact with a Microsoft Word 2007 document (.docx file) without having to launch Microsoft Word 2007. In fact, you don't even need Microsoft Word 2007 or Office 2007 to see the preview if you have the related preview handler - now that's amazing.

This article describes a preview handler for Adobe Photoshop® (PSD) file. Here is how the preview of a PSD file will look like in the preview pane

Screenshot - VistaPreview.jpg

The goal

When the user clicks on a Photoshop file (vista_adobe.psd) in the Windows Explorer

  • A "readonly" preview of the file will be shown in the preview pane
  • Image "Dimension" and "number of layers" in the PSD file will be shown in the "tooltip"
  • Resolution, Color Depth, & Compression information of the file will be available to the user

All this occurs without the Adobe Photoshop application.

Background

Managed Preview Handler Framework

The preview handler described here uses a Managed Preview Handler Framework MsdnMagPreviewHandlers.dll which was first described by Stephen Toub here. For those of you who want to jump in and view the preview handler in action, you can download the first zip file containing the framework, as well as Photoshop Preview Handler, and follow the ReadMe.txt (included). Here is the overview:

Screenshot - prevframework.jpg

New Shell Interfaces for Windows Vista

An extensive set of COM interfaces have been added to the Shell in Windows Vista™ and greatly extend its built-in capabilities. These interfaces offer such features as: new interfaces that expose functionalities that exist in previous versions of Windows but were not exposed by the shell (such as common file dialogs), and interfaces for new Vista UI components or operating system functionality such as Known Folders, Dynamic Autoplay, and property and preview handlers. Download the complete list in the form of help file here

The following is a list of the new interfaces added to the Shell in Windows Vista to provide methods to work with preview handlers.

  • IPreviewHandler
  • IPreviewHandlerFrame
  • IPreviewHandlerVisuals
  • IPreviewHostSurrogateClient
  • IPreviewHostSurrogateSink

Making of the Photoshop Preview Handler

The whole project can be divided into three parts:

  1. The Managed Preview Handler Framework
  2. The Photoshop Preview Handler
  3. The functionality of Photoshop file Parsing

The Managed Preview Handler Framework

Here are the parts of the class diagram, which is of immediate concern for us. More details can be found in Stephen Toub's article

Screenshot - PreviewHandlerClassdiagram.jpg

Preview Handler attribute

Screenshot - PreviewHandlerClassdiagram.jpg

So what we do is create a class file in managed code and inherit it from the framework as follows:

public sealed class PhotoshopPreviewHandler : FileBasedPreviewHandler

The Photoshop Preview Handler

Screenshot - PSDPreviewHandler.jpg

Using the code

Here is the brief code which gives a bird's eye view of the preview handlers load function. For those of you who want to create a Preview Handler, I will suggest this brilliant screencast by Daniel Moth here . In fact everything you see here I did step by step as shown by Daniel from scratch.

[ProgId("CSharpTricks.PhotoshopPreviewHandler")]
[Guid("BC928906-855C-4a6e-8AB6-78105D355708")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public sealed class PhotoshopPreviewHandler : FileBasedPreviewHandler
{
    protected override PreviewHandlerControl CreatePreviewHandlerControl()
    {
            return new PhotoshopPreviewHandlerControl();
    }

private sealed class PSDPreviewHandlerControl:FileBasedPreviewHandlerControl
{
    public override void Load(FileInfo file)
    {
        FileInfo mypsdfile = MakeTemporaryCopy(file);

        CPSD psd = new CPSD();
        Label label1 = new Label();
        PictureBox pictureBox1 = new PictureBox();
        pictureBox1.BorderStyle = BorderStyle.FixedSingle;
        pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        int nResult = psd.Load(mypsdfile.FullName);
        myToolTip.SetToolTip(pictureBox1, label1.Text);
        this.Controls.Add(pictureBox1);
    }

If there is an error while parsing the Photoshop file, we have to safely show an error image and not the exception. If you want to test this, create a text file and change the extension to .psd and see the preview pane when that file is selected.

Screenshot - errorsmall.jpg

Photoshop File Parsing

Well, now comes the tricky part parsing the Photoshop file. A Photoshop file is just like a binary file with all the information as a mixture of XML and binary data. The first challenge is to get the file format specification because Adobe does not provide that freely. You have to do a request for Photoshop SDK here as well as a separate request for Photoshop File format. The good news is the Photoshop file format has not changed much since version 3.0, so if you get a copy of an old Photoshop file format, you pretty much have the stuff you need. You can also find an old version of the Photoshop File format here.

The following image gives you a glimpse of how it looks.

Screenshot - fileformat.jpg

This is the basic format of how data and information is stored in a PSD file

A file header will give you all the image related information as shown

Screenshot - FileHeader.jpg

Image resource block is one of the basic units of the PSD file

Screenshot - ImageResourceBlock.jpg

This is how data is stored in the Photoshop file; to extract it is another story. When I started my binary data reader object, I came across these excellent articles by ihaml here and Igor Tolmachev here. These articles saved me the time it might take to reinvent the wheel (reinforcing my initial idea of time saving), though I also enhanced their work to get the information of number of layers in the Photoshop file and interact with the way needed by the preview handler.

Here is the code to read the PSD File header as shown (File header image above)

protected bool ReadHeader(FileStream stream)
{
    bool bSuccess = false;
    BinaryReader binReader = new BinaryReader(stream);
    try
    {
        // Set Position to the beginning of the stream.
        binReader.BaseStream.Position = 0;
        byte [] Signature  = binReader.ReadBytes(4); // always equal 8BPS,
        byte [] Version    = binReader.ReadBytes(2); // always equal 1, do 
                                                     // not read
        byte [] Reserved   = binReader.ReadBytes(6); // must be zero
        byte [] Channels   = binReader.ReadBytes(2); // number of channels 
                                                     // 1 to 24
        byte [] Rows       = binReader.ReadBytes(4); // height in PIXELS,
        byte [] Columns    = binReader.ReadBytes(4); // width in PIXELS,
        byte [] Depth      = binReader.ReadBytes(2); // number of bpp
        byte [] Mode       = binReader.ReadBytes(2); // colour mode of the 
                                                     // file
        // Btmap=0, Grayscale=1, Indexed=2, RGB=3,
        // CMYK=4, Multichannel=7, Duotone=8, Lab=9
        ASCIIEncoding encoding = new ASCIIEncoding();

So you create a Photoshop parser, return the file information in a Photoshop Preview handler, which, utilizing the preview handler framework, shows you the image data information for the preview pane.

Points of Interest

  • During the development of preview handlers, if you don't see the changes (even after using reinstall.bat) a PC restart solves the problem of detaching the existing version of the preview handler dll and the framework from GAC.
  • If you want to create your own preview handler look closely the implementation of MakeTemporaryCopy(file)
  • Will add more as I come across, adios

Photoshop Preview Handler in action for Outlook 2007

Screenshot - OutlookPreview.jpg

Some concluding thoughts

Almost all software applications in the world are developed with at least one purpose in mind: "saving time". Be it a Windows application, a Java applet, a shell script, tiny applications hosted in sidebar gadget, a search engine, an e-commerce application, a tic tac toe game (yes! even a simple game), all the projects, products, api's, solutions, services, everything which has been developed for the purpose of saving time. Think about it, software which saves more time is better software. This is one of the reasons why Preview Handlers will be very popular.

References

Article History

  • Mar 29, 2007: First published
  • Apr 01, 2007: Content revised

And thanks

For coming so far! I hope you find this as useful as I do, and take care.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Raj Lal
Software Developer (Senior) Nokia Inc.
United States United States
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiondoes this work for windows 8memberSisneh K8 Jan '13 - 19:05 
GeneralMy vote of 5memberrickz657 Apr '11 - 23:02 
GeneralRe: My vote of 5memberrajesh-lal2 Dec '11 - 2:49 
AnswerWorks with Windows 7 (x64) with CS5memberDire Entity27 Jul '10 - 19:43 
AnswerRe: Works with Windows 7 (x64) with CS5membersascha jazbec21 Oct '10 - 17:05 
Questionhow to create a column preview handlermemberSibster29 Mar '10 - 22:04 
Generalawesum Ideamemberblober7 Dec '09 - 5:32 
AnswerNEW INSTALL LINK WITH ALLmemberrajesh-lal22 Oct '10 - 12:24 
GeneralRe: NEW INSTALL LINK WITH ALLmemberjimmiem31 Oct '10 - 10:56 
GeneralPS preview handlermemberrodisava31 Jul '09 - 0:27 
QuestionTrying to install thismemberzack620004 Jun '09 - 16:04 
AnswerRe: Trying to install thismemberNasenbaaer10 Jun '09 - 21:37 
GeneralCS3 supportmembersharper_121 Jan '09 - 17:27 
Questionfailure adding assembly...memberpipiaa98013 Oct '08 - 1:11 
AnswerRe: failure adding assembly...memberDerek Loewen2 May '09 - 17:00 
Questionunable to installmembersimontull10 Oct '08 - 2:24 
AnswerRe: unable to installmvpQuartz.10 Oct '08 - 9:47 
AnswerRe: unable to installmembersimontull14 Oct '08 - 5:50 
GeneralInstall Photoshop Preview HandlermemberPhillip Gordon9 Oct '08 - 14:56 
GeneralRe: Install Photoshop Preview HandlermvpQuartz.10 Oct '08 - 9:48 
GeneralRe: Install Photoshop Preview HandlermemberPhillip Gordon10 Oct '08 - 23:32 
GeneralRe: Install Photoshop Preview HandlermvpQuartz.12 Oct '08 - 9:43 
GeneralThank you!memberm.mishka5 Sep '08 - 9:20 
GeneralRe: Thank you!mvpQuartz.10 Oct '08 - 9:48 
GeneralThis is Excellent work!memberparagme1 Aug '08 - 18:42 
GeneralRe: This is Excellent work!mvpQuartz.10 Oct '08 - 9:47 
GeneralThanx, great job.memberAMIR_KUSUT16 Jun '08 - 17:40 
GeneralRe: Thanx, great job.mvpQuartz.10 Oct '08 - 9:47 
QuestionInstallation problems...membersosimomonon25 Mar '08 - 9:45 
GeneralRe: Installation problems...mvpQuartz.6 Apr '08 - 11:17 
Questionx64 support or not?memberm17design12 Feb '08 - 8:16 
AnswerRe: x64 support or not?memberrcjkierkels19 Aug '08 - 1:02 
QuestionWell Donememberafaz20 Sep '07 - 2:34 
AnswerRe: Well DonememberQuartz.7 Oct '07 - 11:39 
Question5 Globes code :DmemberUnRusoDeCaracas10 Sep '07 - 11:42 
QuestionIPreviewHandlerFrame and Prevhost.exememberTim Haughton17 Jul '07 - 23:54 
AnswerRe: IPreviewHandlerFrame and Prevhost.exememberQuartz.10 Sep '07 - 12:06 
GeneralCloser to the DreammemberwhyJoe?10 Jul '07 - 18:48 
GeneralRe: Closer to the DreammemberQuartz.10 Jul '07 - 19:27 
GeneralThanks - great workmemberkrishnaclc10 Jul '07 - 12:12 
GeneralThanks!! I had a couple errors though!!memberklxz7916 Jun '07 - 19:46 
AnswerRe: Thanks!! I had a couple errors though!! [tested with Photoshop CS]memberQuartz.18 Jun '07 - 19:55 
QuestionHow?memberbirbjo2 Jun '07 - 12:38 
AnswerRe: How?memberQuartz.2 Jun '07 - 16:15 
GeneralRe: How?memberbirbjo2 Jun '07 - 23:30 
QuestionRe: How?memberMember 475811328 Dec '07 - 16:31 
GeneralRe: How?memberQuartz.28 Dec '07 - 16:47 
GeneralRe: How?memberMember 475811330 Dec '07 - 9:25 
GeneralRe: How?memberQuartz.30 Dec '07 - 15:24 
GeneralRe: How?memberMember 475811315 Jan '08 - 11:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 4 Apr 2007
Article Copyright 2007 by Raj Lal
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid