Binary Fortress
Binary Fortress Software
CheckCentral
ClipboardFusion
CloudShow
CloudShow Manager
DisplayFusion
FileSeek
HashTools
LogFusion
Notepad Replacer
Online Base64 Decoder
Online Base64 Encoder
Online JSON Formatter
ShellSend
TrayStatus
VoiceBot
WallpaperFusion
Window Inspector
More Apps...
DisplayFusion
CheckCentral
CloudShow
ClipboardFusion
FileSeek
TrayStatus
VoiceBot
WallpaperFusion
Display
Fusion
by Binary Fortress Software
Download
Download
Change Log
Download Beta
Beta Change Log
License (EULA)
Features
Free vs Pro
More
Screenshots
Scripted Functions (Macros)
Languages
Help
Help Guide
FAQ
Discussions
Contact Us
Find My License
Mailing Address
Advanced Settings
Purchase
Login / Register
WARNING: You currently have Javascript disabled!
This website will not function correctly without Javascript enabled.
Title
Message
OK
Confirm
Yes
No
Optimizing/shrink size for selected files images via https://tinypng.com
Return to DisplayFusion Scripted Functions (Macros)
Description
Optimize/shrink size for all selected files (available for webp, png, jpeg and jpg fromat). Required entry Your unical API key. More info here: https://tinypng.com/developers
Language
C#.net
Minimum Version
9.9+
Created By
KarolPiechoczek
Contributors
-
Date Created
Apr 12, 2022
Date Last Modified
Apr 12, 2022
Scripted Function (Macro) Code
Copy
Select All
using System; using System.Drawing; using System.IO; using System.Net; using System.Text.RegularExpressions; using System.Windows.Forms; // The 'windowHandle' parameter will contain the window handle for the: // - Active window when run by hotkey // - Trigger target when run by a Trigger rule // - TitleBar Button owner when run by a TitleBar Button // - Jump List owner when run from a Taskbar Jump List // - Currently focused window if none of these match public static class DisplayFusionFunction { public static void Run(IntPtr windowHandle) { string[] imageExtensions = { ".webp", ".png", ".jpeg", ".jpg" }; BFS.Clipboard.Copy(); foreach (string file in Clipboard.GetFileDropList()) { string fileExtension = Path.GetExtension(file.ToLower()); if (Array.IndexOf(imageExtensions, fileExtension) != -1) { WebRequest request = WebRequest.Create("https://api.tinify.com/shrink"); //Entry API KEY without [] request.Headers.Add("Authorization", "Basic [ENTRY HERE YOUR API KEY]"); request.Method = "POST"; string fileName = Path.GetFileNameWithoutExtension(file); string dirFile = Path.GetDirectoryName(file); byte[] data = File.ReadAllBytes(file); request.ContentLength = data.Length; Stream stream = request.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); stream = request.GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(stream); WebClient webClient = new WebClient(); webClient.DownloadFile(Regex.Match(sr.ReadToEnd(), "\"url\":\"(.*)\"}}").Groups[1].Value, $@"{dirFile}\{fileName}_optimized{fileExtension}"); sr.Close(); stream.Close(); } } } }