Skip to content

Commit

Permalink
Added new files'
Browse files Browse the repository at this point in the history
  • Loading branch information
tonym128 committed Sep 22, 2017
1 parent 90a5cd8 commit 8b53d5d
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 98 deletions.
Binary file added .vs/Lauren/v15/sqlite3/storage.ide
Binary file not shown.
21 changes: 18 additions & 3 deletions ImageResizer/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Lauren.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
<userSettings>
<Lauren.Properties.Settings>
<setting name="Sizes" serializeAs="String">
<value>90x90;300x167;300x225;300x450;620x439;940x530</value>
</setting>
<setting name="DefaultCompression" serializeAs="String">
<value>70</value>
</setting>
</Lauren.Properties.Settings>
</userSettings>
</configuration>
122 changes: 50 additions & 72 deletions ImageResizer/Lauren.Designer.cs

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

111 changes: 99 additions & 12 deletions ImageResizer/Lauren.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -13,10 +15,29 @@ namespace Lauren
public partial class Lauren : Form
{
static Point FocalPoint = new Point(0, 0);
static String File = string.Empty;
static string File = string.Empty;
List<PictureBox> pictureBoxes = new List<PictureBox>();

public Lauren()
{
InitializeComponent();
List<String> sizes = Properties.Settings.Default.Sizes.Split(';').ToList();

foreach (string size in sizes)
{
pictureBoxes.Add(createPictureBox(Convert.ToInt16(size.Split('x').First()), Convert.ToInt16(size.Split('x').Last())));
}

trackBar1.Value = (int)Properties.Settings.Default.DefaultCompression;
label2.Text = trackBar1.Value.ToString();
}

private PictureBox createPictureBox(int x, int y)
{
PictureBox picture = new PictureBox();
picture.Size = new Size(x, y);
picture.Tag = String.Format("{0}x{1}", x, y);
return picture;
}

private void button1_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -60,11 +81,10 @@ private void ResizePicture()
Image Orig = Image.FromFile(openFileDialog1.FileName);
PicOrig.Image = Orig;

Resize_Image(openFileDialog1.FileName, PicOrig.Image, ref PicSmallest);
Resize_Image(openFileDialog1.FileName, PicOrig.Image, ref PicSmaller);
Resize_Image(openFileDialog1.FileName, PicOrig.Image, ref Pic_Large);
Resize_Image(openFileDialog1.FileName, PicOrig.Image, ref pic_Ban);
Resize_Image(openFileDialog1.FileName, PicOrig.Image, ref Pic_Mid);
foreach (PictureBox pictureBox in pictureBoxes)
{
Resize_Image(openFileDialog1.FileName, PicOrig.Image, pictureBox);
}
}

private static Size CalculateRatio(Size From, Size To)
Expand Down Expand Up @@ -173,11 +193,43 @@ private Image CalculateAndCropImage(double Margin, Size NewSize, Image Input)

Rectangle cropRect = new Rectangle(CropLeft, CropTop, CurrentSize.Width - CropLeft - CropRight, CurrentSize.Height - CropTop - CropBottom);
Image i2 = cropImage(Input, cropRect);
Bitmap retrn = new Bitmap(i2, NewSize);
Bitmap retrn = ResizeImage(i2, NewSize.Width,NewSize.Height);
i2.Dispose();
return retrn;
}

/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);

destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}

return destImage;
}

private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Expand All @@ -186,29 +238,50 @@ private static Image cropImage(Image img, Rectangle cropArea)
return bmpImage2;
}

private long Resize_Image(string Extension, string filename, Image Original, Size s, ref PictureBox output)
private ImageCodecInfo GetEncoder(ImageFormat format)
{

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}

private long Resize_Image(string Extension, string filename, Image Original, Size s, PictureBox output)
{
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string sSmallest = fi.DirectoryName + "\\" + fi.Name.Remove(fi.Name.Length - fi.Extension.Length) + "_" + Extension + ".jpg";
Image i = CalculateAndCropImage(0.05, s, Original);

Bitmap smallest = new Bitmap(i, s);
smallest.Save(sSmallest, System.Drawing.Imaging.ImageFormat.Jpeg);

ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)trackBar1.Value);
myEncoderParameters.Param[0] = myEncoderParameter;
smallest.Save(sSmallest, jpgEncoder, myEncoderParameters);

smallest.Dispose();
fi = new System.IO.FileInfo(sSmallest);
using (Image h = Image.FromFile(sSmallest))
{
Bitmap h2 = new Bitmap(h);
output.Image = h2;
h.Dispose();
}

return fi.Length;
}

private long Resize_Image(string filename, Image Original, ref PictureBox output)
private long Resize_Image(string filename, Image Original, PictureBox output)
{
return Resize_Image(output.Tag.ToString(), filename, Original, output.Size, ref output);
return Resize_Image(output.Tag.ToString(), filename, Original, output.Size, output);
}

private void textBox1_DragDrop(object sender, DragEventArgs e)
Expand Down Expand Up @@ -255,5 +328,19 @@ private void ImageResizer_DragDrop(object sender, DragEventArgs e)
DrawFocalPoint(FocalPoint);
}
}

private void trackBar1_MouseUp(object sender, MouseEventArgs e)
{
try{
ResizePicture();
DrawFocalPoint(FocalPoint);
} catch { }

}

private void trackBar1_Scroll(object sender, EventArgs e)
{
label2.Text = trackBar1.Value.ToString();
}
}
}
Loading

0 comments on commit 8b53d5d

Please sign in to comment.