Click here to Skip to main content
Email Password   helpLost your password?
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

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.

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

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

And thanks

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalawesum Idea
blober
6:32 7 Dec '09  
call me lazy but who has time for this? like a second job to install, and didnt work at the end, rather spend a dollar on a scratch and win.... great idea though.
GeneralPS preview handler
rodisava
1:27 31 Jul '09  
THANK YOU SO MUCH!

I'm not an IT pro, still, after spending some time figuring out how to make it work, I suceeded.

It works a little bit slow, it takes about 5 seconds to generate the preview (and I have a decent system).

Could there be an workaround for speeding it up ?

But it is not that important for me - as I also have, and registered, the psicon.dll file - so I have .psd thumbnails, and I don't always need the preview.

I must say that I could also have previews of .psd files, through windows media palyer, by using the previewconfig program. It is so simple, it is fast, but it generates really blurred images.

And, as people never get enough..., could there be any solution to have .ai thumbs, as of course, aicon.dll doesn't work on Vista?

THANKS again,

rodica
QuestionTrying to install this
zack62000
17:04 4 Jun '09  
Can some one give me a walk thru on installing this i have read the directions inside and out and just cant get what im suppose to do Lord I miss my XP any help is appreciated Thanks
AnswerRe: Trying to install this
Nasenbaaer
22:37 10 Jun '09  
Hi. The installation was made very difficult. My single Bat file looks like that:
"gacutil" -i "MsdnMagPreviewHandlers.dll"
"regasm" /codebase  "MsdnMagPreviewHandlers.dll"
"gacutil" -i "PSDPreviewHandler.dll"
"regasm" /codebase  "PSDPreviewHandler.dll"

Nothing else.
But you have to put the dll file in the same folder like the bat file is. And you have to copy these file from your computer into the same folder like the bat file is:
- regasm.exe
- gacutil.exe
- gacutil.exe.config
If you like, I can make you any Setup to execute Smile
regards
goldengel.ch
GeneralCS3 support
sharper_1
18:27 21 Jan '09  
Can anyone verify for me if this works with adobe CS3 .psd files?
I've installed as written via the readme, the install indicated success, however, it is not working...
Questionfailure adding assembly...
pipiaa980
2:11 13 Oct '08  
when i try to execute the .bat files there's error show up "failure adding assembly to the cache : unknown error". What's happen? my windows is vista 32 bit ultimate.
AnswerRe: failure adding assembly...
Derek Loewen
18:00 2 May '09  
The same happens here. Any fix?
Questionunable to install
simontull
3:24 10 Oct '08  
having copied all preview handler files to ./photoshope elements 6.0, and having set the appropriate admin priviliges for Adobe applications, I run ïnstall but get "an error occurred while writing the registration information to the registry". How to fix?
AnswerRe: unable to install
Quartz.
10:47 10 Oct '08  
read me file for instructions

Omit Needless Words - Strunk, William, Jr.

Vista Gadget Book: Creating Vista Gadgets using HTML, CSS, & JavaScript. Sample chapter here Selling Your Gadget

AnswerRe: unable to install
simontull
6:50 14 Oct '08  
okay, got it. Thanks. Previews are rather slow to load though......my psd images are of the order of 40 MB
GeneralInstall Photoshop Preview Handler
Phillip Gordon
15:56 9 Oct '08  
Hi, I recently downloaded the photoshop handler zip, but when I tried to install the handler the command prompt said error,as system cannot find the file specified?? any ideas??please help me.

Riptorn

GeneralRe: Install Photoshop Preview Handler
Quartz.
10:48 10 Oct '08  
see the read me file

Omit Needless Words - Strunk, William, Jr.

Vista Gadget Book: Creating Vista Gadgets using HTML, CSS, & JavaScript. Sample chapter here Selling Your Gadget

GeneralRe: Install Photoshop Preview Handler
Phillip Gordon
0:32 11 Oct '08  
Hi Quartz, after reading the read me file, I am still not having any luck??, I am new to this and the read me file is quite complex for me,as I do not understand what to do??,can you simplify the install for me please.Thanks.

Riptorn

GeneralRe: Install Photoshop Preview Handler
Quartz.
10:43 12 Oct '08  
check here http://www.codeproject.com/KB/vista/PhotoshopPreviewHandler.aspx?msg=1981561#xx1981561xx
GeneralThank you!
m.mishka
10:20 5 Sep '08  
Thank you, your v1 with 'gacutil' and 'regasm' has done the trick for me! Smile
GeneralRe: Thank you!
Quartz.
10:48 10 Oct '08  
thanks for the message

Omit Needless Words - Strunk, William, Jr.

Vista Gadget Book: Creating Vista Gadgets using HTML, CSS, & JavaScript. Sample chapter here Selling Your Gadget

GeneralThis is Excellent work!
paragme
19:42 1 Aug '08  
Great Work Rajesh, keep them coming Smile


Best Regards,
Parag C Mehta
Web : http://iparag.com

Best Regards,
Parag Mehta
iparag.com

GeneralRe: This is Excellent work!
Quartz.
10:47 10 Oct '08  
thanks

Omit Needless Words - Strunk, William, Jr.

Vista Gadget Book: Creating Vista Gadgets using HTML, CSS, & JavaScript. Sample chapter here Selling Your Gadget

GeneralThanx, great job.
AMIR_KUSUT
18:40 16 Jun '08  
Your PSD Preview Handler is Working well also in Xp. Million thanx!! Smile Smile Smile
GeneralRe: Thanx, great job.
Quartz.
10:47 10 Oct '08  
i am glad to hear that Big Grin

Omit Needless Words - Strunk, William, Jr.

Vista Gadget Book: Creating Vista Gadgets using HTML, CSS, & JavaScript. Sample chapter here Selling Your Gadget

QuestionInstallation problems...
sosimomonon
10:45 25 Mar '08  
Can anyone tell me, step by step, how to install this thing?
I would be very happy if someone did.
GeneralRe: Installation problems...
Quartz.
12:17 6 Apr '08  
Please try to follow the read me file which comes with the zip


HERE[^]

Omit Needless Words - Strunk, William, Jr.

Like tricks, Vista? Daily Tricks Vista Gadget, Trick of Mind

Generalx64 support or not?
m17design
9:16 12 Feb '08  
does these preview handler support Vista x64, I don't know why I can't see the thumbs in my Vista 64Frown
GeneralRe: x64 support or not?
rcjkierkels
2:02 19 Aug '08  
I would like to know that too...
QuestionWell Done
afaz
3:34 20 Sep '07  
Hi...this a great work.
Is there a way to make a chages in your psd framework to save psd file format?


Nothing is impossible


Last Updated 4 Apr 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010