public async override void Invoke()
            {
                ITextBuffer textBuffer = this.HtmlSmartTag.TextBuffer;
                ElementNode element = this.HtmlSmartTag.Element;
                AttributeNode src = element.GetAttribute("src", true);
                ImageCompressor compressor = new ImageCompressor();

                bool isDataUri = src.Value.StartsWith("data:image/", StringComparison.Ordinal);

                if (isDataUri)
                {
                    string dataUri = await compressor.CompressDataUriAsync(src.Value);

                    if (dataUri.Length < src.Value.Length)
                    {
                        using (EditorExtensionsPackage.UndoContext("Optimize image"))
                        {
                            Span span = Span.FromBounds(src.ValueRangeUnquoted.Start, src.ValueRangeUnquoted.End);
                            textBuffer.Replace(span, dataUri);
                        }
                    }
                }
                else
                {
                    var fileName = ImageQuickInfo.GetFullUrl(src.Value, textBuffer);

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

                    await compressor.CompressFilesAsync(fileName);
                }
            }
Example #2
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();
                        await FileHelpers.WriteAllBytesRetry(fileName, buffer);
                    }
            }

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

            ProjectHelpers.AddFileToActiveProject(fileName);
        }
        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.CompressDataUriAsync(url);

                if (dataUri.Length < url.Length)
                {
                    using (WebEssentialsPackage.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.CompressFilesAsync(fileName);
            }
        }
		public async override void Invoke(CancellationToken cancellationToken)
		{
			ImageCompressor compressor = new ImageCompressor();

			bool isDataUri = Attribute.Value.StartsWith("data:image/", StringComparison.Ordinal);

			if (isDataUri)
			{
				string dataUri = await compressor.CompressDataUriAsync(Attribute.Value);

				if (dataUri.Length < Attribute.Value.Length)
				{
					using (WebEssentialsPackage.UndoContext(this.DisplayText))
					using (ITextEdit edit = TextBuffer.CreateEdit())
					{
						Span span = Span.FromBounds(Attribute.ValueRangeUnquoted.Start, Attribute.ValueRangeUnquoted.End);
						edit.Replace(span, dataUri);
						edit.Apply();
					}
				}
			}
			else
			{
				var fileName = ImageQuickInfo.GetFullUrl(Attribute.Value, TextBuffer);

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

				await compressor.CompressFilesAsync(fileName);
			}
		}
Example #5
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;
        }