2 using System.Collections.Generic;
\r
3 using System.Drawing;
\r
4 using System.Drawing.Imaging;
\r
7 using System.Runtime.InteropServices;
\r
11 using DWORD = UInt32;
\r
13 using WORD = UInt16;
\r
15 public struct BITMAPINFOHEADER
\r
17 public DWORD biSize;
\r
18 public LONG biWidth;
\r
19 public LONG biHeight;
\r
20 public WORD biPlanes;
\r
21 public WORD biBitCount;
\r
22 public DWORD biCompression;
\r
23 public DWORD biSizeImage;
\r
24 public LONG biXPelsPerMeter;
\r
25 public LONG biYPelsPerMeter;
\r
26 public DWORD biClrUsed;
\r
27 public DWORD biClrImportant;
\r
30 public unsafe class PSDFile
\r
32 internal Image image;
\r
34 public PSDFile(string file)
\r
36 image = LoadImage(file);
\r
39 public static Bitmap LoadImage(string file)
\r
41 FileStream fs = File.OpenRead(file);
\r
42 byte[] buf = new byte[fs.Length];
\r
43 fs.Read(buf, 0, buf.Length);
\r
45 IntPtr pHBInfo = IntPtr.Zero;
\r
46 IntPtr pHBm = IntPtr.Zero;
\r
49 fixed(byte* p= &buf[0])
\r
51 int rc = GetPicture(p, buf.Length, 1, out pHBInfo, out pHBm, null, 0);
\r
56 BITMAPINFOHEADER* pInfo = (BITMAPINFOHEADER*)LocalLock(pHBInfo);
\r
57 byte* pBM = (byte*)LocalLock(pHBm);
\r
59 Rectangle r = new Rectangle(0, 0, pInfo->biWidth, pInfo->biHeight);
\r
60 bmp = new Bitmap(pInfo->biWidth, pInfo->biHeight, PixelFormat.Format24bppRgb);
\r
61 BitmapData data = bmp.LockBits(r, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
\r
62 byte* dst = (byte*)data.Scan0;
\r
63 dst +=data.Stride * data.Height;
\r
65 switch(pInfo->biBitCount)
\r
70 for(uint y= 0; y < pInfo->biHeight; ++y)
\r
74 for(uint x= 0; x < pInfo->biWidth; ++x)
\r
76 dst[x*3+0] = pBM[s++];
\r
77 dst[x*3+1] = pBM[s++];
\r
78 dst[x*3+2] = pBM[s++];
\r
84 for(uint y= 0; y < pInfo->biHeight; ++y)
\r
88 for(uint x= 0; x < pInfo->biWidth; ++x)
\r
90 dst[x*3+0] = pBM[s++];
\r
91 dst[x*3+1] = pBM[s++];
\r
92 dst[x*3+2] = pBM[s++];
\r
99 bmp.UnlockBits(data);
\r
101 LocalUnlock(pHBInfo);
\r
108 [DllImport("kernel32.dll")]
\r
109 public extern static void* LocalLock(IntPtr handle);
\r
111 [DllImport("kernel32.dll")]
\r
112 public extern static int LocalUnlock(IntPtr handle);
\r
114 [DllImport("ifpsd.spi")]
\r
115 public extern static int GetPicture(byte* file, int len, uint flag, out IntPtr pHBInfo, out IntPtr pHBm, void* lpPrgressCallback, uint lData);
\r
117 //[DllImport("axpsd.spi")]
\r
118 //public extern static int GetPicture(string file, int len, uint flag, out IntPtr pHBInfo, out IntPtr pHBm, void* lpPrgressCallback, uint lData);
\r