Click here to Skip to main content
6,295,667 members and growing! (12,145 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate License: The Code Project Open License (CPOL)

Reading Adobe Photoshop images

By Igor Tolmachev

Simple C# library for opening and displaying Adobe Photoshop images.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:3 Jul 2005
Views:53,136
Bookmarked:45 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
12 votes for this article.
Popularity: 4.87 Rating: 4.52 out of 5

1
1 vote, 8.3%
2

3
2 votes, 16.7%
4
9 votes, 75.0%
5

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


Member

Occupation: Software Developer (Senior)
Location: Ukraine Ukraine

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralLoad error - stream open Pinmemberdomagoj barisic10:39 16 Jul '07  
GeneralSmall bug in ReadImageData() Pinmemberjf36712:53 25 Apr '07  
Generaladding menu in photoshop Pinmemberfarschadnaji7:14 17 Nov '06  
GeneralViewer works great, but need to print Pinmemberhorton3315:44 2 Aug '06  
Jokesome psd file could not be rendered Pinmembercodeprojectnatwar22:04 10 May '06  
Generalimages could not be rendered Pinmemberayunlonglongago23:46 26 Dec '05  
GeneralReading layers PinmemberJonas Beckeman10:54 28 Jul '05  
GeneralRe: Reading layers PinmemberIgor Tolmachev2:17 2 Aug '05  
GeneralRe: Reading layers PinmemberJonas Beckeman6:16 2 Aug '05  
GeneralRe: Reading layers Pinmemberpgruzdev17:24 30 May '06  
GeneralRe: Reading layers PinmemberJonas Beckeman8:08 1 Jun '06  
GeneralRe: Reading layers PinmemberJonas Beckeman23:21 8 Oct '06  
GeneralRe: Reading layers Pinmembervish_mak5:04 18 Apr '07  
Generalview PSD in ASP.NET Pinmemberpyroalpha18:14 10 Jul '05  
GeneralRe: view PSD in ASP.NET PinmemberIgor Tolmachev2:24 2 Aug '05  
Generalgood, but some psd files could not be rendered... PinmemberUnruled Boy1:12 4 Jul '05  
GeneralRe: good, but some psd files could not be rendered... PinmemberIgor Tolmachev2:27 2 Aug '05  
GeneralRe: good, but some psd files could not be rendered... PinmemberUnruled Boy6:01 2 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Jul 2005
Editor: Smitha Vijayan
Copyright 2005 by Igor Tolmachev
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project