Пример #1
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            if (_alloc != null)
            {
                Marshal.ReleaseComObject(_alloc);
            }
            _alloc = null;
            if (ThumbNail != null)
            {
                ThumbNail.Dispose();
            }
            _disposed = true;
        }
Пример #2
0
        protected bool ReadImageResource(FileStream stream)
        {
            bool bSuccess = false;

            BinaryReader binReader = new BinaryReader(stream);
            try
            {
                int nLength = 0;

                byte [] Length  = binReader.ReadBytes(4);

                m_ImageResource = new ImageResource();
                m_ResolutionInfo = new ResolutionInfo();
                m_DisplayInfo = new DisplayInfo();
                m_ThumbNail = new ThumbNail();

                if(SwapBytes(Length,4)) nLength = BitConverter.ToInt32(Length,0);
                m_ImageResource.nLength = nLength;

                int nBytesRead = 0;
                int nTotalBytes = m_ImageResource.nLength;
                long nStreamLen = stream.Length;

                while(stream.Position < nStreamLen && nBytesRead < nTotalBytes)
                {
                    m_ImageResource.Reset();
                    m_ImageResource.OSType  = binReader.ReadBytes(4);
                    nBytesRead += 4;

                    ASCIIEncoding encoding = new ASCIIEncoding();

                    if(encoding.GetString(m_ImageResource.OSType).Equals("8BIM"))
                    {
                        byte [] ID = binReader.ReadBytes(2);
                        nBytesRead += 2;

                        if(SwapBytes(ID,2)) m_ImageResource.nID = BitConverter.ToInt16(ID,0);

                        byte SizeOfName = binReader.ReadByte();
                        nBytesRead += 1;

                        int nSizeOfName = (int)SizeOfName;
                        if(nSizeOfName>0)
                        {
                            if((nSizeOfName % 2)!=0)	// must read 1 more byte to make size even
                            {
                                SizeOfName = binReader.ReadByte();
                                nBytesRead += 1;
                            }

                            m_ImageResource.Name = new byte[nSizeOfName];
                            m_ImageResource.Name = binReader.ReadBytes(nSizeOfName);
                            nBytesRead += nSizeOfName;
                        }

                        SizeOfName = binReader.ReadByte();
                        nBytesRead += 1;

                        byte [] Size = binReader.ReadBytes(4);
                        if(SwapBytes(Size,4)) m_ImageResource.nSize = BitConverter.ToInt32(Size,0);
                        nBytesRead += 4;

                        if((m_ImageResource.nSize % 2) != 0)	// resource data must be even
                            m_ImageResource.nSize++;

                        if(m_ImageResource.nSize>0)
                        {
                            byte [] IntValue = new byte[4];
                            byte [] ShortValue = new byte[2];

                            switch(m_ImageResource.nID)
                            {
                                case 1005:
                                {
                                    m_bResolutionInfoFilled = true;

                                    ShortValue = binReader.ReadBytes(2);
                                    if(SwapBytes(ShortValue,2)) m_ResolutionInfo.hRes = BitConverter.ToInt16(ShortValue,0);
                                    nBytesRead += 2;
                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ResolutionInfo.hResUnit = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;
                                    ShortValue = binReader.ReadBytes(2);
                                    if(SwapBytes(ShortValue,2)) m_ResolutionInfo.widthUnit = BitConverter.ToInt16(ShortValue,0);
                                    nBytesRead += 2;
                                    ShortValue = binReader.ReadBytes(2);
                                    if(SwapBytes(ShortValue,2)) m_ResolutionInfo.vRes = BitConverter.ToInt16(ShortValue,0);
                                    nBytesRead += 2;
                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ResolutionInfo.vResUnit = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;
                                    ShortValue = binReader.ReadBytes(2);
                                    if(SwapBytes(ShortValue,2)) m_ResolutionInfo.heightUnit = BitConverter.ToInt16(ShortValue,0);
                                    nBytesRead += 2;
                                }
                                    break;
                                case 1007:
                                {
                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    if(SwapBytes(ShortValue,2)) m_DisplayInfo.ColourSpace = BitConverter.ToInt16(ShortValue,0);

                                    for(int n=0; n<4; ++n)
                                    {
                                        ShortValue = binReader.ReadBytes(2);
                                        nBytesRead += 2;
                                        if(SwapBytes(ShortValue,2)) m_DisplayInfo.Colour[n] = BitConverter.ToInt16(ShortValue,0);
                                    }

                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    if(SwapBytes(ShortValue,2)) m_DisplayInfo.Opacity = BitConverter.ToInt16(ShortValue,0);
                                    if(m_DisplayInfo.Opacity<0 || m_DisplayInfo.Opacity>100) m_DisplayInfo.Opacity = 100;

                                    byte c = binReader.ReadByte();
                                    nBytesRead += 1;
                                    if(c == 0x00) m_DisplayInfo.kind = false;
                                    else m_DisplayInfo.kind = true;

                                    nBytesRead += 1;
                                    m_DisplayInfo.padding = binReader.ReadByte();
                                }
                                    break;
                                case 1034:
                                {
                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    int nCopyright = 0;
                                    if(SwapBytes(ShortValue,2)) nCopyright = BitConverter.ToInt16(ShortValue,0);
                                    if(nCopyright>0) m_bCopyright = true;
                                    else m_bCopyright = false;
                                }
                                    break;
                                case 1033:
                                case 1036:
                                {
                                    m_bThumbnailFilled = true;

                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ThumbNail.nFormat = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;

                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ThumbNail.nWidth = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;

                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ThumbNail.nHeight = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;

                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ThumbNail.nWidthBytes = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;

                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ThumbNail.nSize = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;

                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_ThumbNail.nCompressedSize = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;

                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    if(SwapBytes(ShortValue,2)) m_ThumbNail.nBitPerPixel = BitConverter.ToInt16(ShortValue,0);

                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    if(SwapBytes(ShortValue,2)) m_ThumbNail.nPlanes = BitConverter.ToInt16(ShortValue,0);

                                    int nTotalData = m_ImageResource.nSize - 28; // header

                                    byte [] buffer = new byte[nTotalData];
                                    byte c;
                                    if(m_ImageResource.nID == 1033)
                                    {
                                        // In BGR format
                                        for(int n=0; n<nTotalData; n=n+3)
                                        {
                                            c = binReader.ReadByte();
                                            nBytesRead += 1;
                                            buffer[n+2] = c;
                                            c = binReader.ReadByte();
                                            nBytesRead += 1;
                                            buffer[n+1] = c;
                                            c = binReader.ReadByte();
                                            nBytesRead += 1;
                                            buffer[n] = c;
                                        }
                                    }
                                    else if(m_ImageResource.nID == 1036)
                                    {
                                        // In RGB format
                                        for (int n=0; n<nTotalData; ++n)
                                        {
                                            c = binReader.ReadByte();
                                            nBytesRead += 1;
                                            buffer[n] = c;
                                        }
                                    }
                                }
                                    break;
                                case 1037:
                                {
                                    IntValue = binReader.ReadBytes(4);
                                    if(SwapBytes(IntValue,4)) m_nGlobalAngle = BitConverter.ToInt32(IntValue,0);
                                    nBytesRead += 4;
                                }
                                    break;
                                case 1046:
                                {
                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    if(SwapBytes(ShortValue,2)) m_nColourCount = BitConverter.ToInt16(ShortValue,0);
                                }
                                    break;
                                case 1047:
                                {
                                    ShortValue = binReader.ReadBytes(2);
                                    nBytesRead += 2;
                                    if(SwapBytes(ShortValue,2)) m_nTransparentIndex = BitConverter.ToInt16(ShortValue,0);
                                }
                                    break;
                                default:
                                {
                                    byte c;
                                    for(int n=0; n<m_ImageResource.nSize; ++n )
                                    {
                                        c = binReader.ReadByte();
                                        nBytesRead += 1;
                                    }
                                }
                                    break;
                            }
                        }
                    }
                }
                bSuccess = true;
            }
            catch(EndOfStreamException e)
            {
                bSuccess = false;
                MessageBox.Show("Exception occurs while reading image resources: {0}", e.GetType().Name);
            }

            return bSuccess;
        }
Пример #3
0
 private void AddThumbNail()
 {
     ThumbNail.Add("href", "");
 }
Пример #4
0
        public Bitmap GetThumbnail(string fileName)
        {
            if (!File.Exists(fileName) && !Directory.Exists(fileName))
            {
                throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName);
            }

            if (ThumbNail != null)
            {
                ThumbNail.Dispose();
                ThumbNail = null;
            }

            var folder = getDesktopFolder;

            if (folder == null)
            {
                return(ThumbNail);
            }

            var pidlMain = IntPtr.Zero;

            try
            {
                var cParsed   = 0;
                var pdwAttrib = 0;
                var filePath  = Path.GetDirectoryName(fileName);
                folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref cParsed, ref pidlMain, ref pdwAttrib);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, null, null);
                throw;
            }

            if (pidlMain == IntPtr.Zero)
            {
                freeResources(pidlMain, folder, null, null);
                return(null);
            }


            var          iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
            IShellFolder item           = null;

            try
            {
                folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, null, item);
                throw;
            }

            if (item == null)
            {
                freeResources(pidlMain, folder, null, null);
                return(null);
            }

            IEnumIDList idEnum = null;

            try
            {
                item.EnumObjects(IntPtr.Zero, (Eshcontf.ShcontfFolders | Eshcontf.ShcontfNonfolders), ref idEnum);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, idEnum, item);
                throw;
            }

            if (idEnum == null)
            {
                freeResources(pidlMain, folder, null, item);
                return(null);
            }

            var pidl     = IntPtr.Zero;
            var fetched  = 0;
            var complete = false;

            while (!complete)
            {
                var hRes = idEnum.Next(1, ref pidl, ref fetched);
                if (hRes != 0)
                {
                    pidl     = IntPtr.Zero;
                    complete = true;
                }
                else
                {
                    if (getThumbNail(fileName, pidl, item))
                    {
                        complete = true;
                    }
                }

                if (pidl != IntPtr.Zero)
                {
                    allocator.Free(pidl);
                }
            }

            freeResources(pidlMain, folder, idEnum, item);
            return(ThumbNail);
        }