Пример #1
0
        public static byte[] GetExtraLargeIcon(string extension)
        {
            var trimmed   = extension.TrimStart(new char[] { '.' });
            var iconIndex = GetIconIndex("*." + trimmed);

            if (iconIndex == 0)
            {
                throw new NullReferenceException("Got null for icon index.");
            }
            IntPtr hIcon = GetExtraLargeIconNative(iconIndex);
            // from native to managed

            Icon ico = (Icon)Icon.FromHandle(hIcon).Clone();

            byte[] imageBytes;
            using (var stream = new MemoryStream())
            {
                ico.ToBitmap().Save(stream, ImageFormat.Png);
                imageBytes = stream.ToArray();
            }


            Shell32.DestroyIcon(hIcon); // don't forget to cleanup
            return(imageBytes);
        }
Пример #2
0
 /// <summary>
 /// Remove the icon from the system tray.
 /// </summary>
 public void RemoveFromTray()
 {
     UpdateIcon(IconType.Disconnected, ProductConstants.DefaultSystemTrayTitle, false);
     if (iconHandle != IntPtr.Zero)
     {
         Shell32.DestroyIcon(iconHandle);
     }
 }
Пример #3
0
        static Icon IconFromExtension(string extension)
        {
            SHFILEINFO shFileInfo = new SHFILEINFO();

            Shell32.SHGetFileInfo(extension, 0x80, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), (uint)(SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes));
            Icon icon = (Icon)Icon.FromHandle(shFileInfo.hIcon).Clone();

            Shell32.DestroyIcon(shFileInfo.hIcon);
            return(icon);
        }
Пример #4
0
        public static ImageSource GetImage(string path, int size)
        {
            IntPtr      intPtr      = FileOperation.GetIcon(path, size, size);
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHBitmap(intPtr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            if (imageSource != null)
            {
                imageSource.Freeze();
            }
            Shell32.DestroyIcon(intPtr);

            return(imageSource);
        }
Пример #5
0
        //Working example as of 2017/05/19 on windows 10 x64
        //example found here.
        //http://stackoverflow.com/a/28530403/1572750
        public static BitmapSource extract(string pathFile)
        {
            BitmapSource iconf;
            IntPtr       hIcon = GetJumboIcon(GetIconIndex(pathFile));

            // from native to managed
            using (Icon ico = (Icon)Icon.FromHandle(hIcon).Clone())
            {  // save to file (or show in a picture box)
                iconf = GetBitmapSource(ico.ToBitmap());
            }

            Shell32.DestroyIcon(hIcon); // don't forget to cleanup
            return(iconf);
        }
Пример #6
0
        public static void Main(string[] args)
        {
            IntPtr hIcon = GetJumboIcon(GetIconIndex(args[0]));

            using (Icon ico = (Icon)Icon.FromHandle(hIcon).Clone()) {
                Bitmap bitmap = ico.ToBitmap();

                System.IO.MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Png);
                byte[] byteImage = ms.ToArray();
                var    SigBase64 = Convert.ToBase64String(byteImage);

                Console.WriteLine(SigBase64);
            }
            Shell32.DestroyIcon(hIcon);
        }
    public static Dictionary <string, Icon> GetPrintersWithIcons(IntPtr hwndOwner)
    {
        Dictionary <string, Icon> result = new Dictionary <string, Icon>();

        Shell32.IShellFolder iDesktopFolder = Shell32.GetDesktopFolder();
        try
        {
            IntPtr pidlPrintersFolder;
            if (Shell32.SHGetFolderLocation(hwndOwner, (int)Shell32.CSIDL.CSIDL_PRINTERS, IntPtr.Zero, 0, out pidlPrintersFolder) == 0)
            {
                try
                {
                    StringBuilder strDisplay       = new StringBuilder(260);
                    Guid          guidIShellFolder = Shell32.IID_IShellFolder;
                    IntPtr        ptrPrintersShellFolder;
                    iDesktopFolder.BindToObject(pidlPrintersFolder, IntPtr.Zero, ref guidIShellFolder, out ptrPrintersShellFolder);
                    Object objPrintersShellFolder = Marshal.GetTypedObjectForIUnknown(ptrPrintersShellFolder, Shell32.ShellFolderType);
                    try
                    {
                        Shell32.IShellFolder printersShellFolder = (Shell32.IShellFolder)objPrintersShellFolder;
                        IntPtr ptrObjectsList;
                        printersShellFolder.EnumObjects(hwndOwner, Shell32.ESHCONTF.SHCONTF_NONFOLDERS, out ptrObjectsList);
                        Object objEnumIDList = Marshal.GetTypedObjectForIUnknown(ptrObjectsList, Shell32.EnumIDListType);
                        try
                        {
                            Shell32.IEnumIDList iEnumIDList = (Shell32.IEnumIDList)objEnumIDList;
                            IntPtr[]            rgelt       = new IntPtr[1];
                            IntPtr         pidlPrinter;
                            int            pceltFetched;
                            Shell32.STRRET ptrString;
                            while (iEnumIDList.Next(1, rgelt, out pceltFetched) == 0 && pceltFetched == 1)
                            {
                                printersShellFolder.GetDisplayNameOf(rgelt[0],
                                                                     Shell32.ESHGDN.SHGDN_NORMAL, out ptrString);
                                if (Shell32.StrRetToBuf(ref ptrString, rgelt[0], strDisplay,
                                                        (uint)strDisplay.Capacity) == 0)
                                {
                                    pidlPrinter = Shell32.ILCombine(pidlPrintersFolder, rgelt[0]);
                                    string             printerDisplayNameInPrintersFolder = strDisplay.ToString();
                                    Shell32.SHFILEINFO shinfo = new Shell32.SHFILEINFO();
                                    Shell32.SHGetFileInfo(pidlPrinter, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), Shell32.SHGFI.PIDL | Shell32.SHGFI.AddOverlays | Shell32.SHGFI.Icon);
                                    Icon printerIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
                                    Shell32.DestroyIcon(shinfo.hIcon);
                                    result.Add(printerDisplayNameInPrintersFolder, printerIcon);
                                }
                            }
                        }
                        finally
                        {
                            Marshal.ReleaseComObject(objEnumIDList);
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(objPrintersShellFolder);
                    }
                }
                finally
                {
                    Shell32.ILFree(pidlPrintersFolder);
                }
            }
        }
        finally
        {
            Marshal.ReleaseComObject(iDesktopFolder);
        }
        return(result);
    }