Skip to content

Commit

Permalink
Improved image load/save dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaex authored and Jaex committed Nov 20, 2013
1 parent 4c3cdcc commit d3c110b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 39 deletions.
16 changes: 16 additions & 0 deletions HelpersLib/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ public static string GetProperName(string name)
return sb.ToString();
}

public static string GetProperExtension(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
int dot = filePath.LastIndexOf('.');

if (dot >= 0)
{
string ext = filePath.Substring(dot + 1);
return ext.ToLowerInvariant();
}
}

return null;
}

public static string Encode(string text, string unreservedCharacters)
{
StringBuilder result = new StringBuilder();
Expand Down
64 changes: 64 additions & 0 deletions HelpersLib/Helpers/ImageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ You should have received a copy of the GNU General Public License
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -950,5 +951,68 @@ public static Bitmap Sharpen(Image image, double strength)
}
return null;
}

public static string OpenImageFileDialog()
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "Image files (*.png, *.jpg, *.jpeg, *.jpe, *.jfif, *.gif, *.bmp, *.tif, *.tiff)|*.png;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.bmp;*.tif;*.tiff|" +
"PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tif, *.tiff)|*.tif;*.tiff|" +
"All files (*.*)|*.*";

if (ofd.ShowDialog() == DialogResult.OK)
{
return ofd.FileName;
}
}

return null;
}

public static void SaveImageFileDialog(Image img)
{
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.DefaultExt = ".png";
sfd.Filter = "PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tif, *.tiff)|*.tif;*.tiff";

if (sfd.ShowDialog() == DialogResult.OK)
{
string filePath = sfd.FileName;
string ext = Helpers.GetProperExtension(filePath);

if (!string.IsNullOrEmpty(ext))
{
ImageFormat imageFormat;

switch (ext)
{
default:
case "png":
imageFormat = ImageFormat.Png;
break;
case "jpg":
case "jpeg":
case "jpe":
case "jfif":
imageFormat = ImageFormat.Jpeg;
break;
case "gif":
imageFormat = ImageFormat.Gif;
break;
case "bmp":
imageFormat = ImageFormat.Bmp;
break;
case "tif":
case "tiff":
imageFormat = ImageFormat.Tiff;
break;
}

img.Save(filePath, imageFormat);
}
}
}
}
}
}
7 changes: 3 additions & 4 deletions HelpersLib/Helpers/SSLBypassHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace HelpersLib
{
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class SSLBypassHelper : IDisposable
{
public SSLBypassHelper()
Expand Down
11 changes: 4 additions & 7 deletions HelpersLib/UITypeEditors/ImageFileNameEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide
return base.EditValue(context, provider, value);
}

using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "All image types (*.jpg, *.jpeg, *.png, *.gif, *.bmp, *.tif, *.tiff)|*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tif;*.tiff";
string filePath = ImageHelpers.OpenImageFileDialog();

if (ofd.ShowDialog() == DialogResult.OK)
{
value = ofd.FileName;
}
if (!string.IsNullOrEmpty(filePath))
{
value = filePath;
}

return value;
Expand Down
29 changes: 8 additions & 21 deletions ImageEffectsLib/ImageEffectsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,34 +340,21 @@ private void btnSettingsImport_Click(object sender, EventArgs e)

private void btnLoadImage_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Title = "Browse for images...";
ofd.Filter = "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp";
string filePath = ImageHelpers.OpenImageFileDialog();

if (ofd.ShowDialog() == DialogResult.OK)
{
if (DefaultImage != null) DefaultImage.Dispose();
DefaultImage = Helpers.GetImageFromFile(ofd.FileName);
UpdatePreview();
}
if (!string.IsNullOrEmpty(filePath))
{
if (DefaultImage != null) DefaultImage.Dispose();
DefaultImage = Helpers.GetImageFromFile(filePath);
UpdatePreview();
}
}

private void btnSaveImage_Click(object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog())
using (Image img = ApplyEffects())
{
sfd.DefaultExt = ".png";
sfd.Filter = "PNG image (*.png)|*.png";

if (sfd.ShowDialog() == DialogResult.OK)
{
using (Image preview = ApplyEffects())
{
preview.Save(sfd.FileName, ImageFormat.Png);
}
}
ImageHelpers.SaveImageFileDialog(img);
}
}

Expand Down
11 changes: 4 additions & 7 deletions ImageEffectsLib/WatermarkForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,11 @@ private void txtWatermarkImageLocation_TextChanged(object sender, EventArgs e)

private void btwWatermarkBrowseImage_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "All image types (*.jpg, *.jpeg, *.png, *.gif, *.bmp, *.tif, *.tiff)|*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tif;*.tiff";
string filePath = ImageHelpers.OpenImageFileDialog();

if (ofd.ShowDialog() == DialogResult.OK)
{
txtWatermarkImageLocation.Text = ofd.FileName;
}
if (!string.IsNullOrEmpty(filePath))
{
txtWatermarkImageLocation.Text = filePath;
}
}
}
Expand Down

0 comments on commit d3c110b

Please sign in to comment.