Click here to Skip to main content
11,433,997 members (49,807 online)
Click here to Skip to main content

Reading Adobe Photoshop images

, 3 Jul 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple C# library for opening and displaying Adobe Photoshop images.

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)

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 5 Pin
NStn1-Sep-13 4:12
memberNStn1-Sep-13 4:12 
Questionthe lage file not be rendered. Pin
namo10334-Jul-12 23:42
membernamo10334-Jul-12 23:42 
Questionnot working for image greater than 1GB of size Pin
projectgirish11-Apr-12 4:08
memberprojectgirish11-Apr-12 4:08 
GeneralMy vote of 5 Pin
kitsemen20-Oct-11 17:53
memberkitsemen20-Oct-11 17:53 
QuestionChange psd resolution? or dimensions? Pin
z1freeride29-Oct-09 13:43
memberz1freeride29-Oct-09 13:43 
GeneralLoad error - stream open Pin
domagoj barisic16-Jul-07 10:39
memberdomagoj barisic16-Jul-07 10:39 
GeneralSmall bug in ReadImageData() Pin
jf36725-Apr-07 12:53
memberjf36725-Apr-07 12:53 
Generaladding menu in photoshop Pin
farschadnaji17-Nov-06 7:14
memberfarschadnaji17-Nov-06 7:14 
GeneralViewer works great, but need to print Pin
horton332-Aug-06 15:44
memberhorton332-Aug-06 15:44 
Jokesome psd file could not be rendered Pin
codeprojectnatwar10-May-06 22:04
membercodeprojectnatwar10-May-06 22:04 
Generalimages could not be rendered Pin
ayunlonglongago26-Dec-05 23:46
memberayunlonglongago26-Dec-05 23:46 
GeneralReading layers Pin
Jonas Beckeman28-Jul-05 10:54
memberJonas Beckeman28-Jul-05 10:54 
GeneralRe: Reading layers Pin
Igor Tolmachev2-Aug-05 2:17
memberIgor Tolmachev2-Aug-05 2:17 
GeneralRe: Reading layers Pin
Jonas Beckeman2-Aug-05 6:16
memberJonas Beckeman2-Aug-05 6: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 layers Pin
pgruzdev30-May-06 17:24
memberpgruzdev30-May-06 17:24 
GeneralRe: Reading layers Pin
Jonas Beckeman1-Jun-06 8:08
memberJonas Beckeman1-Jun-06 8:08 
GeneralRe: Reading layers Pin
Jonas Beckeman8-Oct-06 23:21
memberJonas Beckeman8-Oct-06 23:21 
GeneralRe: Reading layers Pin
vish_mak18-Apr-07 5:04
membervish_mak18-Apr-07 5:04 
GeneralRe: Reading layers Pin
Member 371981423-Nov-11 4:55
memberMember 371981423-Nov-11 4:55 
Generalview PSD in ASP.NET Pin
pyroalpha10-Jul-05 18:14
memberpyroalpha10-Jul-05 18:14 
GeneralRe: view PSD in ASP.NET Pin
Igor Tolmachev2-Aug-05 2:24
memberIgor Tolmachev2-Aug-05 2:24 
Generalgood, but some psd files could not be rendered... Pin
Unruled Boy4-Jul-05 1:12
memberUnruled Boy4-Jul-05 1:12 
GeneralRe: good, but some psd files could not be rendered... Pin
Igor Tolmachev2-Aug-05 2:27
memberIgor Tolmachev2-Aug-05 2:27 
GeneralRe: good, but some psd files could not be rendered... Pin
Unruled Boy2-Aug-05 6:01
memberUnruled Boy2-Aug-05 6:01 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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