Skip to content

Commit dc39805

Browse files
author
kiddailey
committed
- Fixed bug with resize with constraint of images with different aspect ratios
- Added try/catch to destructor method for image data dispose in cases where it hadn't been initialized - Updated version number - Added unit tests for NUnit - Added/removed image resources for tests - Updated unit test project to be class library rather than console application - Added ignore properties to unit test Output directory to exclude test result images
1 parent f190214 commit dc39805

File tree

9 files changed

+319
-22
lines changed

9 files changed

+319
-22
lines changed

ASPNetImage.suo

8.5 KB
Binary file not shown.

ASPNetImage/Source/NetImage.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public NetImage()
4141
/// </summary>
4242
~NetImage()
4343
{
44-
this._image.Dispose();
44+
try
45+
{
46+
this._image.Dispose();
47+
}
48+
catch
49+
{
50+
}
4551
GC.Collect();
4652
}
4753

@@ -334,7 +340,7 @@ public string Version
334340
{
335341
get
336342
{
337-
return "2.3.1.0 (ASPNetImage v0.1)";
343+
return "2.3.1.0 (ASPNetImage v0.2)";
338344
}
339345
}
340346

@@ -786,10 +792,10 @@ public void Resize(int width, int height)
786792
if ((newHeight > height) || (newWidth > width))
787793
{
788794
// Use the center of the image as our basis for cropping
789-
int cropY = Convert.ToInt32((newHeight - height) / 2);
790-
int cropX = Convert.ToInt32((newWidth - width) / 2);
795+
int cropY = (newHeight == height ? 0 : Convert.ToInt32((newHeight - height) / 2));
796+
int cropX = (newWidth == width ? 0 : Convert.ToInt32((newWidth - width) / 2));
791797

792-
this.CropImage(cropX, cropX, width, height);
798+
this.CropImage(cropX, cropY, width, height);
793799
}
794800

795801
graphicsDest.Dispose();

UnitTests/ASPNetImageTest.cs

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using ASPNetImage;
5+
using NUnit.Framework;
6+
7+
namespace UnitTests
8+
{
9+
[TestFixture]
10+
public class ASPNetImageTest
11+
{
12+
[Test]
13+
public void TestVersion()
14+
{
15+
ASPNetImage.NetImage thisImage = new NetImage();
16+
17+
Assert.That(thisImage.Version == "2.3.1.0 (ASPNetImage v0.2)");
18+
}
19+
20+
[Test]
21+
public void TestRegisteredTo()
22+
{
23+
ASPNetImage.NetImage thisImage = new NetImage();
24+
thisImage.RegisteredTo = "Registered To Test";
25+
26+
Assert.That(thisImage.RegisteredTo == "Registered To Test");
27+
}
28+
29+
[Test]
30+
public void TestExpirationDate()
31+
{
32+
ASPNetImage.NetImage thisImage = new NetImage();
33+
34+
Assert.That(Convert.ToDateTime(thisImage.Expires) > DateTime.Now);
35+
}
36+
37+
[Test]
38+
public void TestGetImageSize()
39+
{
40+
ASPNetImage.NetImage thisImage = new NetImage();
41+
thisImage.LoadImage("../../Resources/1024x768.png");
42+
43+
int width, height;
44+
thisImage.GetImageSize(out width, out height);
45+
46+
Assert.That(width == 1024 && height == 768, "Image dimensions do not match");
47+
}
48+
49+
[Test]
50+
public void TestAutoClear()
51+
{
52+
string outputFilePath = "../../Output/AutoClear.jpg";
53+
54+
ASPNetImage.NetImage thisImage = new NetImage();
55+
thisImage.LoadImage("../../Resources/1024x768.png");
56+
thisImage.AutoClear = true;
57+
thisImage.Filename = outputFilePath;
58+
thisImage.SaveImage();
59+
60+
try
61+
{
62+
thisImage.RotateImage(90);
63+
Assert.Fail("Image data not cleared");
64+
}
65+
catch
66+
{
67+
}
68+
}
69+
70+
[Test]
71+
public void TestCrop()
72+
{
73+
string outputFilePath = "../../Output/Crop.jpg";
74+
75+
ASPNetImage.NetImage thisImage = new NetImage();
76+
thisImage.LoadImage("../../Resources/1024x768.png");
77+
thisImage.Filename = outputFilePath;
78+
thisImage.CropImage(10, 50, 800, 600);
79+
thisImage.SaveImage();
80+
81+
int width, height;
82+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
83+
84+
Assert.That(width == 800 && height == 600, "Image dimensions do not match");
85+
}
86+
87+
[Test]
88+
public void TestResizeSameAspectRatioWithConstraint()
89+
{
90+
string outputFilePath = "../../Output/Resize-Same_aspect_ratio-Constrained.jpg";
91+
92+
ASPNetImage.NetImage thisImage = new NetImage();
93+
thisImage.LoadImage("../../Resources/1024x768.png");
94+
thisImage.Filename = outputFilePath;
95+
thisImage.ConstrainResize = true;
96+
thisImage.Resize(800, 600);
97+
thisImage.SaveImage();
98+
99+
int width, height;
100+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
101+
102+
Assert.That(width == 800 && height == 600, "Image dimensions do not match");
103+
}
104+
105+
[Test]
106+
public void TestResizeLandscapeWithConstraint()
107+
{
108+
string outputFilePath = "../../Output/Resize-Variant_aspect_ratio_landscape-Constrained.jpg";
109+
110+
ASPNetImage.NetImage thisImage = new NetImage();
111+
thisImage.LoadImage("../../Resources/1440x900.png");
112+
thisImage.Filename = outputFilePath;
113+
thisImage.ConstrainResize = true;
114+
thisImage.Resize(800, 600);
115+
thisImage.SaveImage();
116+
117+
int width, height;
118+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
119+
120+
Assert.That(width == 800 && height == 600, "Image dimensions do not match");
121+
}
122+
123+
[Test]
124+
public void TestResizePortraitWithConstraint()
125+
{
126+
string outputFilePath = "../../Output/Resize-Variant_aspect_ratio_portrait-Constrained.jpg";
127+
128+
ASPNetImage.NetImage thisImage = new NetImage();
129+
thisImage.LoadImage("../../Resources/900x1440.png");
130+
thisImage.Filename = outputFilePath;
131+
thisImage.ConstrainResize = true;
132+
thisImage.Resize(800, 600);
133+
thisImage.SaveImage();
134+
135+
int width, height;
136+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
137+
138+
Assert.That(width == 800 && height == 600, "Image dimensions do not match");
139+
}
140+
141+
[Test]
142+
public void TestResizeLandscapeWithoutConstraint()
143+
{
144+
string outputFilePath = "../../Output/Resize-Variant_aspect_ratio_landscape-Unconstrained.jpg";
145+
146+
ASPNetImage.NetImage thisImage = new NetImage();
147+
thisImage.LoadImage("../../Resources/1440x900.png");
148+
thisImage.Filename = outputFilePath;
149+
thisImage.ConstrainResize = false;
150+
thisImage.Resize(800, 600);
151+
thisImage.SaveImage();
152+
153+
int width, height;
154+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
155+
156+
Assert.That(width == 800 && height == 600, "Image dimensions do not match");
157+
}
158+
159+
[Test]
160+
public void TestResizePortraitWithoutConstraint()
161+
{
162+
string outputFilePath = "../../Output/Resize-Variant_aspect_ratio_portrait-Unconstrained.jpg";
163+
164+
ASPNetImage.NetImage thisImage = new NetImage();
165+
thisImage.LoadImage("../../Resources/900x1440.png");
166+
thisImage.Filename = outputFilePath;
167+
thisImage.ConstrainResize = false;
168+
thisImage.Resize(800, 600);
169+
thisImage.SaveImage();
170+
171+
int width, height;
172+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
173+
174+
Assert.That(width == 800 && height == 600, "Image dimensions do not match");
175+
}
176+
177+
[Test]
178+
public void TestBrightenImage()
179+
{
180+
string outputFilePath = "../../Output/Brightness-Brighten_image.jpg";
181+
182+
ASPNetImage.NetImage thisImage = new NetImage();
183+
thisImage.LoadImage("../../Resources/1024x768-black.png");
184+
thisImage.Filename = outputFilePath;
185+
thisImage.BrightenImage(50);
186+
thisImage.SaveImage();
187+
188+
Assert.That(true == true); // TODO : Check RGB values
189+
}
190+
191+
[Test]
192+
public void TestDarkenImage()
193+
{
194+
string outputFilePath = "../../Output/Brightness-Darken_image.jpg";
195+
196+
ASPNetImage.NetImage thisImage = new NetImage();
197+
thisImage.LoadImage("../../Resources/1024x768-white.png");
198+
thisImage.Filename = outputFilePath;
199+
thisImage.DarkenImage(50);
200+
thisImage.SaveImage();
201+
202+
Assert.That(true == true); // TODO : Check RGB values
203+
}
204+
205+
[Test]
206+
public void TestRotate90Degrees()
207+
{
208+
string outputFilePath = "../../Output/Rotate-90_degrees.jpg";
209+
210+
ASPNetImage.NetImage thisImage = new NetImage();
211+
thisImage.LoadImage("../../Resources/1024x768.png");
212+
thisImage.Filename = outputFilePath;
213+
thisImage.RotateImage(90);
214+
thisImage.SaveImage();
215+
216+
int width, height;
217+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
218+
219+
Assert.That(width == 768 && height == 1024, "Image dimensions do not match");
220+
}
221+
222+
[Test]
223+
public void TestRotate90DegreesWithCrop()
224+
{
225+
string outputFilePath = "../../Output/Rotate-90_degrees_with_crop.jpg";
226+
int originalWidth = 0;
227+
int originalHeight = 0;
228+
229+
ASPNetImage.NetImage thisImage = new NetImage();
230+
thisImage.LoadImage("../../Resources/1024x768.png");
231+
thisImage.GetImageSize(out originalWidth, out originalHeight);
232+
thisImage.Filename = outputFilePath;
233+
thisImage.RotateImage(90);
234+
thisImage.ConstrainResize = true;
235+
thisImage.Resize(originalWidth, originalHeight);
236+
thisImage.SaveImage();
237+
238+
int width, height;
239+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
240+
241+
Assert.That(width == originalWidth && height == originalHeight, "Image dimensions do not match");
242+
}
243+
244+
[Test]
245+
public void TestRotate180Degrees()
246+
{
247+
string outputFilePath = "../../Output/Rotate-180_degrees.jpg";
248+
249+
ASPNetImage.NetImage thisImage = new NetImage();
250+
thisImage.LoadImage("../../Resources/1024x768.png");
251+
thisImage.Filename = outputFilePath;
252+
thisImage.RotateImage(180);
253+
thisImage.SaveImage();
254+
255+
int width, height;
256+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
257+
258+
Assert.That(width == 1024 && height == 768, "Image dimensions do not match");
259+
}
260+
261+
[Test]
262+
public void TestFlipHorizontal()
263+
{
264+
string outputFilePath = "../../Output/Flip-Horizontal.jpg";
265+
266+
ASPNetImage.NetImage thisImage = new NetImage();
267+
thisImage.LoadImage("../../Resources/1024x768.png");
268+
thisImage.Filename = outputFilePath;
269+
thisImage.FlipImage(NetImage.FlipDirections.Horizontal);
270+
thisImage.SaveImage();
271+
272+
int width, height;
273+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
274+
275+
Assert.That(width == 1024 && height == 768, "Image dimensions do not match");
276+
}
277+
278+
[Test]
279+
public void TestFlipVertical()
280+
{
281+
string outputFilePath = "../../Output/Flip-Vertical.jpg";
282+
283+
ASPNetImage.NetImage thisImage = new NetImage();
284+
thisImage.LoadImage("../../Resources/1024x768.png");
285+
thisImage.Filename = outputFilePath;
286+
thisImage.FlipImage(NetImage.FlipDirections.Vertical);
287+
thisImage.SaveImage();
288+
289+
int width, height;
290+
thisImage.GetImageFileSize(outputFilePath, out width, out height);
291+
292+
Assert.That(width == 1024 && height == 768, "Image dimensions do not match");
293+
}
294+
}
295+
}

UnitTests/Program.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
2.35 KB
Loading
3.39 KB
Loading
-329 KB
Binary file not shown.
-150 KB
Binary file not shown.

UnitTests/UnitTests.csproj

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
<ProductVersion>8.0.50727</ProductVersion>
66
<SchemaVersion>2.0</SchemaVersion>
77
<ProjectGuid>{EFB48FB4-4982-49E4-A007-98BFCE885C70}</ProjectGuid>
8-
<OutputType>Exe</OutputType>
8+
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>UnitTests</RootNamespace>
1111
<AssemblyName>UnitTests</AssemblyName>
12+
<StartupObject>
13+
</StartupObject>
1214
</PropertyGroup>
1315
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1416
<DebugSymbols>true</DebugSymbols>
@@ -28,20 +30,27 @@
2830
<WarningLevel>4</WarningLevel>
2931
</PropertyGroup>
3032
<ItemGroup>
33+
<Reference Include="nunit.framework, Version=2.4.6.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
3134
<Reference Include="System" />
3235
<Reference Include="System.Data" />
3336
<Reference Include="System.Xml" />
3437
</ItemGroup>
3538
<ItemGroup>
36-
<Compile Include="Program.cs" />
39+
<Compile Include="ASPNetImageTest.cs" />
3740
<Compile Include="Properties\AssemblyInfo.cs" />
3841
</ItemGroup>
3942
<ItemGroup>
43+
<Content Include="Resources\1024x768-black.png" />
44+
<Content Include="Resources\1024x768-white.png" />
4045
<Content Include="Resources\1024x768.png" />
4146
<Content Include="Resources\1440x900.png" />
4247
<Content Include="Resources\900x1440.png" />
43-
<Content Include="Resources\bee-pdphoto.org.jpg" />
44-
<Content Include="Resources\skyline-pdphoto.org.jpg" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<ProjectReference Include="..\ASPNetImage\ASPNetImage.csproj">
51+
<Project>{B56B76E7-0BF9-4439-8AEB-91C6709628C5}</Project>
52+
<Name>ASPNetImage</Name>
53+
</ProjectReference>
4554
</ItemGroup>
4655
<ItemGroup>
4756
<Folder Include="Output\" />

0 commit comments

Comments
 (0)