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

Reading Adobe Photoshop images

By , 3 Jul 2005
 

SimplePsd Test program

Introduction

Most of the image editors and viewers do not allow to open Adobe Photoshop images directly. Adobe Photoshop is a bitmap graphics editor (with some text and vector graphics capabilities) developed and published by Adobe Systems. It is the market leader for commercial image manipulation. Adobe Photoshop native file format (*.psd) is not documented well and quite complicated.

Having noticed that there isn't any decent C# sources for opening Adobe Photoshop files, I decided to focus my efforts on this library. The aim of this library is to provide quick opening and displaying of Photoshop images and to provide ability to convert images to other formats.

A number of web search efforts brought no results for .NET platform, but I have found a few C\C++ codes regarding Adobe Photoshop imports, including a great C++ article MyPSD class of Iosif Hamlatzis.

Having extensive C\C++ experience and beginning C# experience, I have created the SimplePsd class library that allows you to open images saved in Adobe Photoshop native format (*.psd) so it may be easily included in any other projects. Currently, the following formats are supported: RGB, Lab, CMY, CMYK, Indexed, Grayscale, Duotone. These formats can be uncompressed or compressed with RLE.

Using the code

Usage of SimplePsd library is very simple:

...
using SimplePsd;
...
private SimplePsd.CPSD psd = new SimplePsd.CPSD();
...
if(this.openFileDialog.ShowDialog().Equals(DialogResult.OK))
{
    int nResult = psd.Load(openFileDialog.FileName);
    if(nResult == 0)
    {
        int nCompression = psd.GetCompression();
        string strCompression = "Unknown";
        switch(nCompression)
        {
            case 0:
                strCompression = "Raw data";
                break;
            case 1:
                strCompression = "RLE";
                break;
            case 2:
                strCompression = "ZIP without prediction";
                break;
            case 3:
                strCompression = "ZIP with prediction";
                break;
        }
        label1.Text = string.Format("Image Width: {0}px\r\nImage Height: {1}px\r\n"+
                    "Image BitsPerPixel: {2}\r\n"+
                    "Resolution (pixels/inch): X={3} Y={4}\r\n",
                    psd.GetWidth(),
                    psd.GetHeight(),
                    psd.GetBitsPerPixel(),
                    psd.GetXResolution(),
                    psd.GetYResolution());
        label1.Text += "Compression: "+strCompression;
        pictureBox1.Image = System.Drawing.Image.FromHbitmap(psd.GetHBitmap());
    }
    else if(nResult == -1)
        MessageBox.Show("Cannot open the File");
    else if(nResult == -2)
        MessageBox.Show("Invalid (or unknown) File Header");
    else if(nResult == -3)
        MessageBox.Show("Invalid (or unknown) ColourMode Data block");
    else if(nResult == -4)
        MessageBox.Show("Invalid (or unknown) Image Resource block");
    else if(nResult == -5)
        MessageBox.Show("Invalid (or unknown) Layer and Mask Information section");
    else if(nResult == -6)
        MessageBox.Show("Invalid (or unknown) Image Data block");
}

Credits

I would like to thank Iosif Hamlatzis for his article and permission to use some of the converting functions from his project.

SimplePsd License

You may include the source code, modified source code, assembly within your own projects for either personal or commercial use with only one restriction: don't change the name of the library "SimplePsd.dll".

Finally, if you improve any part of this library or want to discuss something related to the sources, please feel free to contact me by e-mail.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Igor Tolmachev
Software Developer (Senior)
Ukraine Ukraine
Member
No Biography provided

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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionthe lage file not be rendered.membernamo10334 Jul '12 - 22:42 
I used the dll to load a file about 600M,it not works;
Questionnot working for image greater than 1GB of sizememberprojectgirish11 Apr '12 - 3:08 
its working well for psd images less than 900mb.but it gives an exception saying that the image resources cant be read..and image block cannot be identified for image size greater than 900mb.
GeneralMy vote of 5memberkitsemen20 Oct '11 - 16:53 
дякую
QuestionChange psd resolution? or dimensions?memberz1freeride29 Oct '09 - 12:43 
Is it possible to change a PSD's resolution from 300 to 72? Or possibly it's dimensions?
 
Thanks,
Chris
GeneralLoad error - stream openmemberdomagoj barisic16 Jul '07 - 9:39 
just add stream.Close(); in Load(string strPathName) procedure....

GeneralSmall bug in ReadImageData()memberjf36725 Apr '07 - 11:53 
I came across this library today because a friend without photoshop needs to view a PSD file, and the code provided is great! I found a small bug in ReadImageData() though in case you didn't know yet: When reading RGB format, you're using a loop to read each color channel, but you hardcoded the number of iterations to 3 (red, green and blue); it should be m_HeaderInfo.nChannels instead. I happened to tried the code on an image with 4 channels (last one for alpha i guess), so the code didn't work initially without fixing this bug. Great work though, thanks a lot!
Generaladding menu in photoshopmemberfarschadnaji17 Nov '06 - 6:14 
hi guys
please let me know what is code for adding menu in photoshop like this link
http://www.zizi.ir/reg/image.php?id=9947all copy.JPG
 
thanks

GeneralViewer works great, but need to printmemberhorton332 Aug '06 - 14:44 
The viewer has great screen resolution, but there are a couple of things that I would like to see added to it.
 
I have an image that was sent to me by email. What I want to do is view and print the image.
 
I can only view part of the image - no scroll bars to get around rest of image.
 
Image is reported as:
Image width: 850px
Image height: 1169px
Image BitsPerPixel: 8
Resolution (pixels/inch): X=100 Y=100
Compression: RLE
 
Hugh Orton
Jokesome psd file could not be renderedmembercodeprojectnatwar10 May '06 - 21:04 
SimplePSD Dll is work fine Smile | :) with most of PSD,
But some psd file could not be rendered.
 
check out a some psd file that could not be rendered.
 
http://220.227.46.41/psd/Psd_files.zip
 
Regards,
Natwar Garg
ngarg@mhlin.com

Generalimages could not be renderedmemberayunlonglongago26 Dec '05 - 22:46 
Reading Adobe Photoshop images could not be rendered when Layer1 or backimages is Transparent,and have one Pixel is too.
by way : if open file is Asynchronous then it is better end if
GeneralReading layersmemberJonas Beckeman28 Jul '05 - 9:54 
I'm writing a C# psd importer for use within my graphics/game toolkit Endogine, and I've succeeded in parsing most of the file (layer info, effect settings etc) but I have problems with the actual images.
 
Then I looked at codeproject and found your article (and others), but the problem is they all only rip the merged bitmap, not the individual layers' bitmaps.
 
I think I'm almost there, but the RLE encoding in layers seems to differ from the merged one (e.g. the RLE "header" is per channel, not per bitmap).
 
Have you looked into this?
 

 
BTW, My psd-parsing classes (including a modified version of your SimplePSD pixel-ripping code) can be found in the Endogine source at
http://endogine.com/CS/Endogine.zip (I haven't udated the codeproject article yet)
 
The Photoshop code is in Endogine\Serialization\Photoshop (Photoshop.cs as the starting point).
Start it up, right-click DefaultLayer in the SceneGraphViewer, select Import/Replace to import a psd.
GeneralRe: Reading layersmemberIgor Tolmachev2 Aug '05 - 1:17 
Thank you very much for your comments to my article. SimplePSD library was developed to be a simple and small as possible. Probably, I will add ability to read layers in the next releases.
 
I have dowloaded your Endogine project and will go deep into it.
 
I guess, the Codeproject community will be glad to pick up some useful code from your article.
 
Thanks.
GeneralRe: Reading layersmemberJonas Beckeman2 Aug '05 - 5:16 
Great! Let me know if you figure anything out. Should I find something, I'll post it here. (There's an update submitted to CodeProject which should appear by the end of the week, but I have made no changes in the Photoshop code.)
 
Oh, and feel free to try and fix my Flash rendering code as well... Wink | ;)
GeneralRe: Reading layersmemberpgruzdev30 May '06 - 16:24 
BTW did any of you guys tried to read vector and text layers from PSD file format ?
GeneralRe: Reading layersmemberJonas Beckeman1 Jun '06 - 7:08 
I haven't implemented any of them, sorry. Paths are simple constructs, texts are more complicated. Well, not difficult to read, but in order to render the text exactly as Photoshop does it, there's a lot of parameters for each character that must be carefully implemented - kerning, leading, base shift etc - and also the antialiasing algorithms are different from GDI+. You'll have to mimick them yourself, and font antialiasing is a complex beast...
 
If you want to give it a shot, here's info to get you started:
http://www.fine-view.com/jp/lab/doc/ps6ffspecsv2.pdf
 
HTH
GeneralRe: Reading layersmemberJonas Beckeman8 Oct '06 - 22:21 
I've been working on an updated version of my PSD parser, and have added Path and Text support. Text resources in later Photoshop versions are a lot more complicated than I thought - a simple Text layer can add over 300 kB of data to the document...
 
Did you look into this? Did you get any results?
GeneralRe: Reading layersmembervish_mak18 Apr '07 - 4:04 
I want to differ two layes form the psd file. can any body of you help me for same.
GeneralRe: Reading layersmemberMember 371981423 Nov '11 - 3:55 
I need to decode PSD layers, info.
Did not find the content at Endogine to be of help.
The photoshop class is empty. There is no PSD decoding capability there.
 
It appears my options are one of the following:
1) extend libPSD and add a C# wrapper.
2) extend ImageMagickNET and add a C# wrapper.
3) append layer functionality in your simplePSD class.
 
Extending your simplePSD looks most promising.
I will forward you any worthwhile addition if/when completed.
 
CT
Generalview PSD in ASP.NETmemberpyroalpha10 Jul '05 - 17:14 
can you view the PSD directly in ASP.NET or you have to convert it to supported web files?
As far as I know, we can't view PSD file directly in the web.
 
I'm having trouble in viewing the PSD file in the web.
GeneralRe: view PSD in ASP.NETmemberIgor Tolmachev2 Aug '05 - 1:24 
Unfortunately, I am not a big friend of ASP.
 
I guess, if you need to display PSD inside web page, you can create and use some ActiveX control (like Macromedia Flash - SWF).
Generalgood, but some psd files could not be rendered...memberUnruled Boy4 Jul '05 - 0:12 
check out a sample psd file that could not be rendered:
 
http://www.cnblogs.com/Files/unruledboy/7.zip[^]
 
Regards,
unruledboy@hotmail.com
GeneralRe: good, but some psd files could not be rendered...memberIgor Tolmachev2 Aug '05 - 1:27 
Thank you very much for your example.
I will check it out.
 
Will inform you when I found the bugs and fix them.
GeneralRe: good, but some psd files could not be rendered...memberUnruled Boy2 Aug '05 - 5:01 
i think the latest vc++ version has fixed the bug;)
 
Regards,
unruledboy@hotmail.com

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 4 Jul 2005
Article Copyright 2005 by Igor Tolmachev
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid