示例#1
0
        public static Image GetFileLogo(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(NativeThemeManager.LoadBitmap("UnknownFile.png"));
            }

            Image fileLogo = null;

            if (IsURL(fileName))
            {
                fileLogo = (Bitmap)WebsiteImage.DownloadSiteIcon(fileName);
                if (fileLogo == null)
                {
                    fileLogo = NativeThemeManager.LoadBitmap("url.png");
                }
            }
            else
            {
                fileLogo = (Bitmap)FileImage.FileNameImage(fileName);

                if (fileLogo == null)
                {
                    if (FileOperations.DirectoryExists(fileName))
                    {
                        fileLogo = NativeThemeManager.LoadBitmap("Folder.png");
                    }
                    else
                    {
                        fileLogo = GetExtensionLogo(fileName);
                        if (fileLogo == null)
                        {
                            fileLogo = NativeThemeManager.LoadBitmap("UnknownFile.png");
                        }
                    }
                }
            }

            return(fileLogo);
        }
示例#2
0
        private Image GetResourceData()
        {
            string resourceName;

            if (resources.Count < 1)
            {
                return(null);
            }

            if (desiredIndex >= resources.Count)
            {
                desiredIndex = 0;
            }

            if (desiredIndex >= 0)
            {
                resourceName = resources[desiredIndex];
            }
            else
            {
                desiredIndex = Math.Abs(desiredIndex);
                resourceName = desiredIndex.ToString(CultureInfo.InvariantCulture);
            }

            IntPtr resourceInfo = IntPtr.Zero;
            IntPtr resourceData = IntPtr.Zero;
            IntPtr resourceLock = IntPtr.Zero;
            IntPtr ico          = IntPtr.Zero;
            Bitmap bitmap       = null;

            int resourceSize = 0;

            if (IsIntResource(resourceName))
            {
                int resourceNumber = int.Parse(resourceName, CultureInfo.InvariantCulture);
                if (resourceNumber == 0)
                {
                    resourceInfo = NativeMethods.FindResource(moduleHandle,
                                                              Marshal.StringToHGlobalAuto("0"),
                                                              ResourceTypes.RT_GROUP_ICON);
                }
                else
                {
                    resourceInfo = NativeMethods.FindResource(moduleHandle,
                                                              (IntPtr)resourceNumber,
                                                              ResourceTypes.RT_GROUP_ICON);
                }
            }
            else
            {
                resourceInfo = NativeMethods.FindResource(moduleHandle, resourceName, ResourceTypes.RT_GROUP_ICON);
            }

            if (resourceInfo == IntPtr.Zero)
            {
                return(null);
            }

            resourceData = NativeMethods.LoadResource(moduleHandle, resourceInfo);
            if (resourceData == IntPtr.Zero)
            {
                return(null);
            }

            resourceLock = NativeMethods.LockResource(resourceData);
            if (resourceLock == IntPtr.Zero)
            {
                return(null);
            }


            //number od desired icon
            int nID = NativeMethods.LookupIconIdFromDirectoryEx(resourceLock, true, desiredSize, desiredSize, LoadIconFlags.LR_DEFAULTCOLOR);

            if (nID <= 0)
            {
                return(null);
            }

            resourceInfo = NativeMethods.FindResource(moduleHandle,
                                                      (IntPtr)nID, ResourceTypes.RT_ICON);

            if (resourceInfo == IntPtr.Zero)
            {
                return(null);
            }

            resourceData = NativeMethods.LoadResource(moduleHandle, resourceInfo);
            if (resourceData == IntPtr.Zero)
            {
                return(null);
            }

            resourceLock = NativeMethods.LockResource(resourceData);
            if (resourceLock == IntPtr.Zero)
            {
                return(null);
            }

            resourceSize = NativeMethods.SizeofResource(moduleHandle, resourceInfo);
            if (resourceSize > 0)
            {
                ico = NativeMethods.CreateIconFromResourceEx(resourceLock, resourceSize, true,
                                                             0x00030000, 48, 48, LoadIconFlags.LR_DEFAULTCOLOR);
                if (ico == IntPtr.Zero)
                {
                    byte[] buf = new byte[resourceSize];
                    Marshal.Copy(resourceLock, buf, 0, buf.Length);

                    //The resource contains icon, but getting icon failed for some reason
                    if (buf[0] == 40)
                    {
                        return(null);
                    }

                    //The resource contains PNG (check by signature). This can happens
                    //on Vista. Getting PNG directly from resource without conversion
                    if ((buf[0] == 0x89) && (buf[1] == 0x50) && (buf[2] == 0x4E))
                    {
                        MemoryStream stream = new MemoryStream(buf);
                        try
                        {
                            bitmap = new Bitmap(stream);
                        }
                        finally
                        {
                            stream.Dispose();
                        }
                        return(bitmap);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    Icon icon = (Icon)System.Drawing.Icon.FromHandle(ico);
                    bitmap = (Bitmap)FileImage.ConvertIconToBitmap(icon);
                    NativeMethods.DestroyIcon(ico);
                    icon.Dispose();
                    return(bitmap);
                }
            }
            else
            {
                return(null);
            }
        }