Skip to content

Commit 93f861b

Browse files
author
kiddailey
committed
- Implemented ClearImage method
- Implemented FillRect method - Added support for saving BMP, GIF, and PNG formats
1 parent 7f5c0cd commit 93f861b

File tree

9 files changed

+189
-16
lines changed

9 files changed

+189
-16
lines changed

ASPNetImage.suo

9.82 KB
Binary file not shown.

ASPNetImage/Source/NetImage.cs

Lines changed: 128 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Drawing.Imaging;
33
using System.Drawing;
44
using System.IO;
5+
using System.Drawing.Drawing2D;
56

67
namespace ASPNetImage
78
{
@@ -254,6 +255,17 @@ public string Filename
254255
}
255256
}
256257

258+
/// <summary>
259+
/// Gets the classes .NET image instance
260+
/// </summary>
261+
public Image RawNetImage
262+
{
263+
get
264+
{
265+
return this._image;
266+
}
267+
}
268+
257269
/// <summary>
258270
/// Gets or sets the image format to be used when saving the image. Not currently
259271
/// supported and only JPG format is output.
@@ -461,10 +473,6 @@ public void Chord(int intX1, int intY1, int intX2, int intY2, int intX3, int int
461473
{
462474
}
463475

464-
public void ClearImage()
465-
{
466-
}
467-
468476
public void ClearTexture()
469477
{
470478
}
@@ -509,10 +517,6 @@ public void FillPath()
509517
{
510518
}
511519

512-
public void FillRect(int intLeft, int intTop, int intRight, int intBottom)
513-
{
514-
}
515-
516520
public void FishEye(int intDegree)
517521
{
518522
}
@@ -532,6 +536,20 @@ public int GetPixel(int intX, int intY)
532536

533537
public void GradientOneWay(int intBeginColor, int intEndColor, int intDirection)
534538
{
539+
//Graphics graphicsDest = Graphics.FromImage(this._image);
540+
//LinearGradientBrush thisBrush;
541+
542+
//switch (intDirection)
543+
//{
544+
// case 0:
545+
// // up
546+
// thisBrush = new LinearGradientBrush(
547+
// new Point(this._image.Width / 2, this._image.Height - 1),
548+
// new Point(this._image.Width / 2, 0),
549+
// Color.FromArgb(intBeginColor),
550+
// Color.FromArgb(intEndColor));
551+
552+
//}
535553
}
536554

537555
public void GradientTwoWay(int intBeginColor, int intEndColor, int intDirection, int intInOut)
@@ -670,6 +688,22 @@ public void Wave(int intGraphicSize, int intWaveSize)
670688

671689
#endregion
672690

691+
/// <summary>
692+
/// Clears the entire image with the current BackgroundColor
693+
/// </summary>
694+
public void ClearImage()
695+
{
696+
Graphics graphicsDest = Graphics.FromImage(this._image);
697+
graphicsDest.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
698+
Color brushColor = Color.FromArgb(this.BackgroundColor);
699+
Brush coloredBrush = new SolidBrush(brushColor);
700+
graphicsDest.FillRectangle(coloredBrush, 0, 0, this._image.Width, this._image.Height);
701+
graphicsDest.DrawImage(this._image, 0, 0);
702+
703+
graphicsDest.Dispose();
704+
coloredBrush.Dispose();
705+
}
706+
673707
/// <summary>
674708
/// Crops the image with the specified dimensions
675709
/// </summary>
@@ -690,6 +724,26 @@ public void CropImage(int startX, int startY, int width, int height)
690724
graphicsCrop.Dispose();
691725
}
692726

727+
/// <summary>
728+
/// Fills the specified rectangle with the current PenColor
729+
/// </summary>
730+
/// <param name="intLeft"></param>
731+
/// <param name="intTop"></param>
732+
/// <param name="intRight"></param>
733+
/// <param name="intBottom"></param>
734+
public void FillRect(int intLeft, int intTop, int intRight, int intBottom)
735+
{
736+
Graphics graphicsDest = Graphics.FromImage(this._image);
737+
graphicsDest.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
738+
Color brushColor = Color.FromArgb(this.PenColor);
739+
Brush coloredBrush = new SolidBrush(brushColor);
740+
graphicsDest.FillRectangle(coloredBrush, intLeft, intTop, intRight - intLeft, intBottom - intTop);
741+
graphicsDest.DrawImage(this._image, 0, 0);
742+
743+
graphicsDest.Dispose();
744+
coloredBrush.Dispose();
745+
}
746+
693747
/// <summary>
694748
/// Flips the image vertically (intDirection == 2) or horizontally (intDirection == 1)
695749
/// </summary>
@@ -893,18 +947,76 @@ public bool SaveImage()
893947
EncoderParameter imageEncoderParameter;
894948
EncoderParameters imageEncoderParameters;
895949

896-
imageCodecInfo = GetEncoderInfo("image/jpeg");
897-
imageEncoder = Encoder.Quality;
898-
imageEncoderParameters = new EncoderParameters(1);
899-
imageEncoderParameter = new EncoderParameter(imageEncoder, this.JPEGQuality);
900-
imageEncoderParameters.Param[0] = imageEncoderParameter;
950+
switch (this.ImageFormat)
951+
{
952+
case ImageFormats.BMP:
953+
954+
this.Filename = Path.ChangeExtension(this.Filename, "bmp");
955+
956+
try
957+
{
958+
this._image.Save(this.Filename, System.Drawing.Imaging.ImageFormat.Bmp);
959+
}
960+
catch (Exception e)
961+
{
962+
this._error = e.ToString();
963+
}
964+
break;
965+
966+
case ImageFormats.GIF:
967+
968+
this.Filename = Path.ChangeExtension(this.Filename, "gif");
969+
970+
try
971+
{
972+
this._image.Save(this.Filename, System.Drawing.Imaging.ImageFormat.Gif);
973+
}
974+
catch (Exception e)
975+
{
976+
this._error = e.ToString();
977+
}
978+
break;
979+
980+
case ImageFormats.PNG:
981+
982+
this.Filename = Path.ChangeExtension(this.Filename, "png");
983+
984+
try
985+
{
986+
this._image.Save(this.Filename, System.Drawing.Imaging.ImageFormat.Png);
987+
}
988+
catch (Exception e)
989+
{
990+
this._error = e.ToString();
991+
}
992+
993+
break;
994+
995+
case ImageFormats.JPEG:
996+
default:
901997

902-
this.Filename = Path.ChangeExtension(this.Filename, "jpg");
998+
imageCodecInfo = GetEncoderInfo("image/jpeg");
999+
imageEncoder = Encoder.Quality;
1000+
imageEncoderParameters = new EncoderParameters(1);
1001+
imageEncoderParameter = new EncoderParameter(imageEncoder, this.JPEGQuality);
1002+
imageEncoderParameters.Param[0] = imageEncoderParameter;
1003+
1004+
this.Filename = Path.ChangeExtension(this.Filename, "jpg");
1005+
1006+
try
1007+
{
1008+
this._image.Save(this.Filename, imageCodecInfo, imageEncoderParameters);
1009+
}
1010+
catch (Exception e)
1011+
{
1012+
this._error = e.ToString();
1013+
}
1014+
1015+
break;
1016+
}
9031017

9041018
try
9051019
{
906-
this._image.Save(this.Filename, imageCodecInfo, imageEncoderParameters);
907-
9081020
if (File.Exists(this.Filename))
9091021
{
9101022
if (this.AutoClear)
0 Bytes
Binary file not shown.
2 KB
Binary file not shown.
-12 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
2 KB
Binary file not shown.

UnitTests/ASPNetImageTest.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Text;
44
using ASPNetImage;
55
using NUnit.Framework;
6+
using System.Security.Cryptography;
7+
using System.Drawing;
68

79
namespace UnitTests
810
{
@@ -67,6 +69,64 @@ public void TestAutoClear()
6769
}
6870
}
6971

72+
/// <summary>
73+
/// Tests the ClearImage method by taking an all white image, clearing it to all black,
74+
/// and then compare it to a known all-black image using a SHA hash comparison
75+
/// </summary>
76+
[Test]
77+
public void TestClearImage()
78+
{
79+
string outputFilePath = "../../Output/ClearImage.png";
80+
81+
ASPNetImage.NetImage thisImage = new NetImage();
82+
thisImage.LoadImage("../../Resources/1024x768-white.png");
83+
thisImage.ImageFormat = NetImage.ImageFormats.PNG;
84+
thisImage.Filename = outputFilePath;
85+
thisImage.BackgroundColor = System.Drawing.Color.FromArgb(0, 0, 0).ToArgb();
86+
thisImage.ClearImage();
87+
thisImage.AutoClear = false;
88+
thisImage.SaveImage();
89+
90+
ASPNetImage.NetImage clearedImage = new NetImage();
91+
clearedImage.LoadImage("../../Resources/1024x768-black.png");
92+
93+
Bitmap modifiedImage = new Bitmap(thisImage.RawNetImage);
94+
Bitmap comparisonImage = new Bitmap(clearedImage.RawNetImage);
95+
96+
ImageConverter thisConverter = new ImageConverter();
97+
byte[] modifiedImageBytes = new byte[0];
98+
byte[] comparisonImageBytes = new byte[0];
99+
modifiedImageBytes = (byte[])thisConverter.ConvertTo(modifiedImage, modifiedImageBytes.GetType());
100+
comparisonImageBytes = (byte[])thisConverter.ConvertTo(comparisonImage, comparisonImageBytes.GetType());
101+
102+
SHA256Managed hasher = new SHA256Managed();
103+
byte[] modifiedImageHash = hasher.ComputeHash(modifiedImageBytes);
104+
byte[] comparisonImageHash = hasher.ComputeHash(comparisonImageBytes);
105+
106+
bool isMatch = true;
107+
for (int i = 0; i < modifiedImageHash.Length && i < comparisonImageHash.Length && isMatch; i++)
108+
{
109+
if (modifiedImageHash[i] != comparisonImageHash[i])
110+
isMatch = false;
111+
}
112+
113+
Assert.IsTrue(isMatch);
114+
}
115+
116+
[Test]
117+
public void TestFillRect()
118+
{
119+
string outputFilePath = "../../Output/FillRect.png";
120+
121+
ASPNetImage.NetImage thisImage = new NetImage();
122+
thisImage.LoadImage("../../Resources/1024x768-white.png");
123+
thisImage.ImageFormat = NetImage.ImageFormats.PNG;
124+
thisImage.Filename = outputFilePath;
125+
thisImage.PenColor = System.Drawing.Color.FromArgb(0, 0, 0).ToArgb();
126+
thisImage.FillRect(10, 10, 20, 20);
127+
thisImage.SaveImage();
128+
}
129+
70130
[Test]
71131
public void TestCrop()
72132
{

UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
</Reference>
3737
<Reference Include="System" />
3838
<Reference Include="System.Data" />
39+
<Reference Include="System.Drawing" />
3940
<Reference Include="System.Xml" />
4041
</ItemGroup>
4142
<ItemGroup>

0 commit comments

Comments
 (0)