Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2 from keremdemirer/master
Browse files Browse the repository at this point in the history
Resize images before upload - configurable
  • Loading branch information
apsun authored Mar 22, 2017
2 parents a3035ae + 15ce7a5 commit 6864181
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 17 deletions.
27 changes: 21 additions & 6 deletions GoogleImageShell/ConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions GoogleImageShell/ConfigForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ private void installButton_Click(object sender, EventArgs e)
string menuText = menuTextTextBox.Text;
bool includeFileName = includeFileNameCheckBox.Checked;
bool allUsers = allUsersCheckBox.Checked;
bool resizeOnUpload = resizeOnUploadCheckbox.Checked;
ImageFileType[] types = fileTypeListBox.CheckedItems.Cast<ImageFileType>().ToArray();
Install(menuText, includeFileName, allUsers, types);
Install(menuText, includeFileName, allUsers, resizeOnUpload, types);
}

private void uninstallButton_Click(object sender, EventArgs e)
Expand All @@ -38,11 +39,11 @@ private void uninstallButton_Click(object sender, EventArgs e)
Uninstall(allUsers, types);
}

private static void Install(string menuText, bool includeFileName, bool allUsers, ImageFileType[] types)
private static void Install(string menuText, bool includeFileName, bool allUsers, bool resizeOnUpload, ImageFileType[] types)
{
try
{
ShortcutMenu.InstallHandler(menuText, includeFileName, allUsers, types);
ShortcutMenu.InstallHandler(menuText, includeFileName, allUsers, resizeOnUpload, types);
}
catch (Exception ex)
{
Expand Down
70 changes: 66 additions & 4 deletions GoogleImageShell/GoogleImages.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -11,14 +13,61 @@ namespace GoogleImageShell
{
public static class GoogleImages
{
public static async Task<string> Search(string imagePath, bool includeFileName, CancellationToken cancelToken)
private const int _maxImageSize = 800;

public static async Task<string> Search(string imagePath, bool includeFileName, bool resizeOnUpload, CancellationToken cancelToken)
{
var handler = new HttpClientHandler();
handler.AllowAutoRedirect = false;

StringContent content = null;


if (resizeOnUpload)
{
try
{

}
catch (Exception)
{

}
using (Bitmap bmp = new Bitmap(imagePath))
{
if (bmp.Width > _maxImageSize || bmp.Height > _maxImageSize)
{
var newSize = ResizeKeepAspect(bmp.Size, _maxImageSize, _maxImageSize);

using (var newBmp = new Bitmap(newSize.Width, newSize.Height))
{
using (Graphics g = Graphics.FromImage(newBmp))
{
g.DrawImage(bmp, new Rectangle(0, 0, newSize.Width, newSize.Height));
}

using (var ms = new MemoryStream())
{
newBmp.Save(ms, ImageFormat.Jpeg);

content = new StringContent(BinaryToBase64Compat(ms.ToArray()));
}
}
}
}
}



if (content == null)
{
content = new StringContent(FileToBase64Compat(imagePath));
}

using (var client = new HttpClient(handler))
{
var form = new MultipartFormDataContentCompat();
form.Add(new StringContent(FileToBase64Compat(imagePath)), "image_content");
form.Add(content, "image_content");
if (includeFileName)
{
form.Add(new StringContent(Path.GetFileName(imagePath)), "filename");
Expand All @@ -33,13 +82,26 @@ public static async Task<string> Search(string imagePath, bool includeFileName,
}
}

private static string BinaryToBase64Compat(byte[] content)
{
string base64 = Convert.ToBase64String(content).Replace('+', '-').Replace('/', '_');
return base64;
}


private static string FileToBase64Compat(string imagePath)
{
byte[] content = File.ReadAllBytes(imagePath);
string base64 = Convert.ToBase64String(content).Replace('+', '-').Replace('/', '_');
string base64 = BinaryToBase64Compat(content);
return base64;
}


public static Size ResizeKeepAspect(Size src, int maxWidth, int maxHeight)
{
decimal rnd = Math.Min(maxWidth / (decimal)src.Width, maxHeight / (decimal)src.Height);
return new Size((int)Math.Round(src.Width * rnd), (int)Math.Round(src.Height * rnd));
}

private class MultipartFormDataContentCompat : MultipartContent
{
public MultipartFormDataContentCompat() : base("form-data")
Expand Down
10 changes: 7 additions & 3 deletions GoogleImageShell/ShortcutMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ public static class ShortcutMenu
{ImageFileType.BMP, new[] {".bmp"}}
};

private static string CreateProgramCommand(bool includeFileName)
private static string CreateProgramCommand(bool includeFileName, bool resizeOnUpload)
{
string exePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
string command = $"\"{exePath}\" search \"%1\"";
if (includeFileName)
{
command += " -n";
}
if (resizeOnUpload)
{
command += " -r";
}
return command;
}

Expand All @@ -37,9 +41,9 @@ private static RegistryKey GetShellKey(bool allUsers, string fileType)
return shellKey;
}

public static void InstallHandler(string menuText, bool includeFileName, bool allUsers, ImageFileType[] types)
public static void InstallHandler(string menuText, bool includeFileName, bool allUsers, bool resizeOnUpload, ImageFileType[] types)
{
string command = CreateProgramCommand(includeFileName);
string command = CreateProgramCommand(includeFileName, resizeOnUpload);
foreach (ImageFileType fileType in types)
{
foreach (string typeExt in FileTypeMap[fileType])
Expand Down
7 changes: 6 additions & 1 deletion GoogleImageShell/UploadForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public partial class UploadForm : Form
{
private readonly string _imagePath;
private readonly bool _includeFileName;
private readonly bool _resizeOnUpload;

private readonly CancellationTokenSource _cancelTokenSource = new CancellationTokenSource();

public UploadForm(string[] args)
Expand All @@ -19,6 +21,7 @@ public UploadForm(string[] args)
{
string arg = args[i];
if (arg == "-n") _includeFileName = true;
else if (arg == "-r") _resizeOnUpload = true;
else _imagePath = arg;
}
}
Expand All @@ -27,7 +30,9 @@ private void UploadForm_Load(object sender, EventArgs e)
{
Log("Uploading image: " + _imagePath);
Log("Include file name: " + _includeFileName);
Task<string> task = GoogleImages.Search(_imagePath, _includeFileName, _cancelTokenSource.Token);
Log("Resize on upload: " + _resizeOnUpload);

Task<string> task = GoogleImages.Search(_imagePath, _includeFileName, _resizeOnUpload, _cancelTokenSource.Token);
task.ContinueWith(OnUploadComplete, TaskScheduler.FromCurrentSynchronizationContext());
}

Expand Down

0 comments on commit 6864181

Please sign in to comment.