public async override void Invoke()
        {
            if (string.IsNullOrEmpty(_url.UrlString.Text))
                return;

            ImageCompressor compressor = new ImageCompressor();
            string url = _url.UrlString.Text.Trim('"', '\'');

            if (_url.IsDataUri())
            {
                string dataUri = await compressor.CompressDataUri(url);

                if (dataUri.Length < url.Length)
                {
                    using (EditorExtensionsPackage.UndoContext("Optimize image"))
                    {
                        Span span = Span.FromBounds(_url.UrlString.Start, _url.UrlString.AfterEnd);
                        _span.TextBuffer.Replace(span, "'" + dataUri + "'");
                    }
                }
            }
            else
            {
                string selection = Uri.UnescapeDataString(url);
                string fileName = ProjectHelpers.ToAbsoluteFilePath(selection, _span.TextBuffer.GetFileName());

                if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName))
                    return;

                await compressor.CompressFiles(fileName);
            }
        }
        public static async void SaveClipboardImageToFile(IDataObject data, string fileName)
        {
            if (data.GetDataPresent(DataFormats.FileDrop))
            {
                string original = ((string[])data.GetData(DataFormats.FileDrop))[0];

                if (File.Exists(original))
                {
                    File.Copy(original, fileName, true);
                }
            }
            else
            {
                using (Bitmap image = (Bitmap)data.GetData("System.Drawing.Bitmap"))
                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Save(ms, GetImageFormat(Path.GetExtension(fileName)));
                        byte[] buffer = ms.ToArray();
                        File.WriteAllBytes(fileName, buffer);
                    }
            }

            ImageCompressor compressor = new ImageCompressor();
            await compressor.CompressFilesAsync(fileName).HandleErrors("compressing " + fileName);

            ProjectHelpers.AddFileToActiveProject(fileName);
        }
示例#3
0
        void BeforeQueryStatus(object sender, System.EventArgs e)
        {
            OleMenuCommand button = sender as OleMenuCommand;

            _selectedPaths = ProjectHelpers.GetSelectedFilePaths()
                             .Where(p => ImageCompressor.IsFileSupported(p));

            int items = _selectedPaths.Count();

            button.Text    = items == 1 ? "Optimize image" : "Optimize images";
            button.Enabled = items > 0;
        }
        public static async void SaveClipboardImageToFile(IDataObject data, string fileName)
        {
            if (data.GetDataPresent(DataFormats.FileDrop))
            {
                string original = ((string[])data.GetData(DataFormats.FileDrop))[0];

                if (File.Exists(original))
                    File.Copy(original, fileName, true);
            }
            else
            {
                using (Bitmap image = (Bitmap)data.GetData("System.Drawing.Bitmap"))
                using (MemoryStream ms = new MemoryStream())
                {
                    image.Save(ms, GetImageFormat(Path.GetExtension(fileName)));
                    byte[] buffer = ms.ToArray();
                    File.WriteAllBytes(fileName, buffer);
                }
            }

            ImageCompressor compressor = new ImageCompressor();
            await compressor.CompressFiles(fileName);

            ProjectHelpers.AddFileToActiveProject(fileName);
        }