Exemplo n.º 1
0
        private byte[] InternalGetIcon(string appId, int pxSize = 48)
        {
            // extract icon from .exe
            // note: `IconExtractor` use LoadLibrary Win32API, so I need save the command binary into file.
            var tmpPath = ExtractEntryPointCommandFile(appId);

            if (tmpPath == null)
            {
                return(NoImagePng());
            }

            try
            {
                using (var msIco = new MemoryStream())
                    using (var msPng = new MemoryStream())
                    {
                        IconExtractor.Extract1stIconTo(tmpPath, msIco);
                        if (msIco.Length == 0)
                        {
                            return(NoImagePng());
                        }

                        msIco.Seek(0, SeekOrigin.Begin);
                        var icon = new FromMono.System.Drawing.Icon(msIco, pxSize, pxSize);

                        var iconBmp  = icon.ToBitmap();
                        var iconSize = iconBmp.Size.Width;
                        if (iconSize < pxSize)
                        {
                            //var margin = (pxSize - iconSize) / 2;
                            var newBmp = new Bitmap(width: pxSize, height: pxSize);
                            using (var g = Graphics.FromImage(newBmp))
                            {
                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                g.DrawImage(iconBmp, 0, 0, pxSize, pxSize);
                            }
                            iconBmp.Dispose();
                            iconBmp = newBmp;
                        }
                        iconBmp.Save(msPng, ImageFormat.Png);
                        iconBmp.Dispose();
                        icon.Dispose();

                        return(msPng.ToArray());
                    }
            }
            finally
            {
                System.IO.File.Delete(tmpPath);
            }
        }
Exemplo n.º 2
0
        private byte[] InternalGetIcon(string appId)
        {
            var commandPath = GetEntryPointCommandPath(appId);

            if (commandPath == null)
            {
                return(NoImagePng());
            }

            var commandBytes = this.ClickOnceFileRepository.GetFileContent(appId, commandPath);

            if (commandBytes == null)
            {
                return(NoImagePng());
            }

            // extract icon from .exe
            // note: `IconExtractor` use LoadLibrary Win32API, so I need save the command binary into file.
            var tmpPath = Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString("N") + ".exe");

            System.IO.File.WriteAllBytes(tmpPath, commandBytes);
            try
            {
                using (var msIco = new MemoryStream())
                    using (var msPng = new MemoryStream())
                    {
                        IconExtractor.Extract1stIconTo(tmpPath, msIco);
                        if (msIco.Length == 0)
                        {
                            return(NoImagePng());
                        }

                        msIco.Seek(0, SeekOrigin.Begin);
                        var icon = new FromMono.System.Drawing.Icon(msIco, 48, 48);

                        icon.ToBitmap().Save(msPng, ImageFormat.Png);
                        return(msPng.ToArray());
                    }
            }
            finally
            {
                System.IO.File.Delete(tmpPath);
            }
        }
Exemplo n.º 3
0
        private byte[] InternalGetIcon(string appId, int pxSize = 48)
        {
            // extract icon from .exe
            // note: `IconExtractor` use LoadLibrary Win32API, so I need save the command binary into file.
            var tmpPath = ExtractEntryPointCommandFile(appId);
            if (tmpPath == null) return NoImagePng();

            try
            {
                using (var msIco = new MemoryStream())
                using (var msPng = new MemoryStream())
                {
                    IconExtractor.Extract1stIconTo(tmpPath, msIco);
                    if (msIco.Length == 0) return NoImagePng();

                    msIco.Seek(0, SeekOrigin.Begin);
                    var icon = new FromMono.System.Drawing.Icon(msIco, pxSize, pxSize);

                    var iconBmp = icon.ToBitmap();
                    var iconSize = iconBmp.Size.Width;
                    if (iconSize < pxSize)
                    {
                        //var margin = (pxSize - iconSize) / 2;
                        var newBmp = new Bitmap(width: pxSize, height: pxSize);
                        using (var g = Graphics.FromImage(newBmp))
                        {
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            g.DrawImage(iconBmp, 0, 0, pxSize, pxSize);
                        }
                        iconBmp.Dispose();
                        iconBmp = newBmp;
                    }
                    iconBmp.Save(msPng, ImageFormat.Png);
                    iconBmp.Dispose();
                    icon.Dispose();

                    return msPng.ToArray();
                }
            }
            finally
            {
                System.IO.File.Delete(tmpPath);
            }
        }
Exemplo n.º 4
0
        private byte[] InternalGetIcon(string appId)
        {
            var commandPath = GetEntryPointCommandPath(appId);
            if (commandPath == null) return NoImagePng();

            var commandBytes = this.ClickOnceFileRepository.GetFileContent(appId, commandPath);
            if (commandBytes == null) return NoImagePng();

            // extract icon from .exe
            // note: `IconExtractor` use LoadLibrary Win32API, so I need save the command binary into file.
            var tmpPath = Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString("N") + ".exe");
            System.IO.File.WriteAllBytes(tmpPath, commandBytes);
            try
            {
                using (var msIco = new MemoryStream())
                using (var msPng = new MemoryStream())
                {
                    IconExtractor.Extract1stIconTo(tmpPath, msIco);
                    if (msIco.Length == 0) return NoImagePng();

                    msIco.Seek(0, SeekOrigin.Begin);
                    var icon = new FromMono.System.Drawing.Icon(msIco, 48, 48);

                    icon.ToBitmap().Save(msPng, ImageFormat.Png);
                    return msPng.ToArray();
                }
            }
            finally
            {
                System.IO.File.Delete(tmpPath);
            }
        }