Permalink
Browse files

Image uploading with drag and drop

  • Loading branch information...
OsoianMarcel committed Apr 5, 2016
1 parent 480a344 commit cf65d7f2993c899271a6babd081a7ea3721aea2d
View
@@ -65,8 +65,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\FindImage.cs" />
<Compile Include="Helpers\FlashWindow.cs" />
<Compile Include="Helpers\Global.cs" />
<Compile Include="Helpers\MimeType.cs" />
<Compile Include="Helpers\Thread.cs" />
<Compile Include="HelpForm.cs">
<SubType>Form</SubType>
@@ -80,8 +82,10 @@
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Models\Image.cs" />
<Compile Include="Models\Token.cs" />
<Compile Include="Models\ImageUpload.cs" />
<Compile Include="Models\ImageFile.cs" />
<Compile Include="Models\StaticMd\Image.cs" />
<Compile Include="Models\StaticMd\Token.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\Upload.cs" />
View
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace App.Helpers
{
public static class FindImage
{
// Find image files
public static List<Models.ImageFile> find(string[] paths)
{
List<Models.ImageFile> images = new List<Models.ImageFile>();
foreach (string path in paths)
{
// Check if current path is a file
if (!File.Exists(path))
{
continue;
}
// Get file extension
string extension = getFileExtension(path);
// Check if current file is an accepted image
if (!isExtensionAccepted(extension))
{
continue;
}
// Push image to collection
Models.ImageFile imageFile = new Models.ImageFile();
imageFile.path = path;
imageFile.mimeType = MimeType.find(extension);
images.Add(imageFile);
}
return images;
}
// Check if image is accepted by extension
private static bool isExtensionAccepted(string extension)
{
return Global.acceptedImageExtensions.Contains(extension);
}
// Get file extension (lowercase)
private static string getFileExtension(string path)
{
return Path.GetExtension(path).Replace(".", "").ToLower();
}
}
}
View
@@ -1,9 +1,16 @@
using System;
using System.Collections.Generic;
namespace App.Helper
namespace App.Helpers
{
public static class Global
{
public static string userAgent = "ImageUploader/1.0 (By OMD)";
// User-Agent
public static readonly string userAgent = "ImageUploader/1.0 (By OMD)";
// Accepted image extensions
public static readonly string[] acceptedImageExtensions = {
"jpg", "jpeg", "jpe", "png", "gif", "ico"
};
}
}
View
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace App.Helpers
{
public static class MimeType
{
// Accepted mime types
private static readonly Dictionary<string, string> MIMETypes = new Dictionary<string, string>
{
{"jpe", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"jpg", "image/jpeg"},
{"png", "image/png"},
{"gif", "image/gif"},
{"ico", "image/x-icon"}
};
// Find mime type by extension
public static string find(string extension, string def = "application/octet-stream")
{
if (MIMETypes.ContainsKey(extension))
{
return MIMETypes[extension];
}
return def;
}
}
}
View
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using Threading = System.Threading;
namespace App.Helper
namespace App.Helpers
{
public static class Thread
{
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -13,9 +13,10 @@ namespace App
{
public partial class MainForm : Form
{
private delegate void OnSuccessDelegate(Model.Image image);
private delegate void OnSuccessDelegate(Models.StaticMd.Image image);
private delegate void OnErrorDelegate(string message);
private delegate void OnCompleteDelegate();
private delegate void OnImageDelegate(Image image);
private enum MenuCopyAs { Text, BBCode, HTML }
@@ -36,16 +37,17 @@ public MainForm()
// Init upload service
this.uploadService = new Service.Upload();
this.uploadService.onSuccess(new Service.Upload.SuccessCallback((Model.Image image) => this.Invoke(new OnSuccessDelegate(this.onSuccess), image)));
this.uploadService.onSuccess(new Service.Upload.SuccessCallback((Models.StaticMd.Image image) => this.Invoke(new OnSuccessDelegate(this.onSuccess), image)));
this.uploadService.onError(new Service.Upload.ErrorCallback((string message) => this.Invoke(new OnErrorDelegate(this.onError), message)));
this.uploadService.onComplete(new Service.Upload.CompleteCallback(() => this.Invoke(new OnCompleteDelegate(this.onComplete))));
this.uploadService.onImage(new Service.Upload.ImageCallback((Image image) => this.Invoke(new OnImageDelegate(this.onImage), image)));
// Append app version to main form
this.Text += " | " + Application.ProductVersion;
}
// Upload: On success
private void onSuccess(Model.Image image)
private void onSuccess(Models.StaticMd.Image image)
{
this.imageListBox.Items.Add(image.image);
this.imageListBox.ClearSelected();
@@ -74,10 +76,47 @@ private void onError(string message)
// Upload: On complete
private void onComplete()
{
this.uiFinishUpload();
}
// Upload: On image
private void onImage(Image image)
{
try
{
if (image.Width <= this.previewPictureBox.Width && image.Height <= this.previewPictureBox.Height)
{
this.previewPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
}
else
{
this.previewPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
}
this.previewPictureBox.Image = image;
}
catch (Exception ex)
{
MessageBox.Show("Can not set image in preview box. Exception: " + ex.Message, "Error: Preview");
}
}
// UI: Start upload
private void uiStartUpload()
{
this.captureButton.Enabled = false;
this.uploadProgressBar.Style = ProgressBarStyle.Marquee;
this.uploadProgressBar.MarqueeAnimationSpeed = 15;
this.AllowDrop = false;
}
// UI: Finish upload
private void uiFinishUpload()
{
this.captureButton.Enabled = true;
this.uploadProgressBar.MarqueeAnimationSpeed = 0;
this.uploadProgressBar.Style = ProgressBarStyle.Blocks;
this.AllowDrop = true;
// Flash form if it is not active
if (!this.Focused)
@@ -98,29 +137,8 @@ private void captureButton_Click(object sender, EventArgs e)
return;
}
try
{
Image cloneImage = (Image)image.Clone();
if (cloneImage.Width <= this.previewPictureBox.Width && cloneImage.Height <= this.previewPictureBox.Height)
{
this.previewPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
}
else
{
this.previewPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
}
this.previewPictureBox.Image = cloneImage;
}
catch (Exception ex)
{
MessageBox.Show("Can not set image in preview box. Exception: " + ex.Message, "Error: Preview");
}
this.uploadService.doRequestAsync(image);
this.captureButton.Enabled = false;
this.uploadProgressBar.Style = ProgressBarStyle.Marquee;
this.uploadProgressBar.MarqueeAnimationSpeed = 15;
this.uiStartUpload();
this.uploadService.uploadImageAsync(image);
}
// Listbox key up event
@@ -248,7 +266,7 @@ private void helpButton_Click(object sender, EventArgs e)
}
// Copy selected items to clipboard
void imageListBoxCopySelectedToClipboard(MenuCopyAs copyAs = MenuCopyAs.Text)
private void imageListBoxCopySelectedToClipboard(MenuCopyAs copyAs = MenuCopyAs.Text)
{
int count = imageListBox.SelectedItems.Count;
@@ -304,17 +322,43 @@ private void copyStringToClipboard(string text)
}
catch (Exception ex)
{
MessageBox.Show("It looks like another application on your machine might be locking the clipboard. Exception: " + ex.Message, "Error: Clipboard");
MessageBox.Show("It looks like another application on your machine might be locking the clipboard. "
+ "Exception: " + ex.Message, "Error: Clipboard");
}
}
// Remove selected items
void imageListBoxRemoveSelected()
private void imageListBoxRemoveSelected()
{
for (int i = this.imageListBox.SelectedItems.Count - 1; i >= 0; i--)
{
this.imageListBox.Items.Remove(this.imageListBox.SelectedItems[i]);
}
}
// Drop files event
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
string[] droppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
List<Models.ImageFile> images = Helpers.FindImage.find(droppedFiles);
if (images.Count == 0)
{
return;
}
this.uiStartUpload();
this.uploadService.uploadImageFilesAsync(images);
}
// Accept files by drag and drop
private void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
}
}
View
@@ -0,0 +1,10 @@
using System;
namespace App.Models
{
public class ImageFile
{
public string path { get; set; }
public string mimeType { get; set; }
}
}
View
@@ -0,0 +1,11 @@
using System;
namespace App.Models
{
public class ImageUpload
{
public byte[] bytes { get; set; }
public string mimeType { get; set; }
public ImageFile imageFile { get; set; }
}
}
@@ -1,6 +1,6 @@
using System;
namespace App.Model
namespace App.Models.StaticMd
{
public class Image
{
@@ -1,6 +1,6 @@
using System;
namespace App.Model
namespace App.Models.StaticMd
{
public class Token
{
Oops, something went wrong.

0 comments on commit cf65d7f

Please sign in to comment.