From d19cb4ac6a77617593bb8b411dcd7fd33d612961 Mon Sep 17 00:00:00 2001 From: Sebastian Battig Date: Mon, 22 Jul 2019 13:37:32 -0300 Subject: [PATCH 01/10] Added memory management using using clause for resources that need disposal. Added Ellipse and Polygon methods. Changed Rectangle method to also call FillRect Updated color management to take as input VBScript compatible color (first and third byte flipped when RGB is encoded) --- ASPNetImage/Source/NetImage.cs | 963 ++++++++++++++++----------------- 1 file changed, 471 insertions(+), 492 deletions(-) diff --git a/ASPNetImage/Source/NetImage.cs b/ASPNetImage/Source/NetImage.cs index ea7a39c..4facc4f 100644 --- a/ASPNetImage/Source/NetImage.cs +++ b/ASPNetImage/Source/NetImage.cs @@ -22,7 +22,7 @@ namespace ASPNetImage /// /// ASPImage Copyright ServerObjects Inc. /// - [ProgId("AspImage.Image")] + [ProgId("AspNetImage.NetImage")] public class NetImage { // ==================================================================== @@ -40,22 +40,23 @@ public NetImage() _penStyle = 0; _penColor = 0; _textAngle = 0; + _brushColor = 0; _registeredTo = "Open Source"; _maxY = 0; _maxX = 0; - _progressiveJPEGEncoding = false; + ProgressiveJPEGEncoding = false; _jpegQuality = 100; - _imageFormat = ImageFormats.JPEG; - _fontSize = 12; - _fontName = "MS Sans Serif"; + ImageFormat = ImageFormats.JPEG; + FontSize = 12; + FontName = "MS Sans Serif"; _fontColor = 0; _filename = ""; _error = ""; - _constrainResize = true; + ConstrainResize = true; _backgroundColor = Color.FromArgb(255, 0, 0, 0).ToArgb(); // Default to black - _bold = false; - _autoClear = true; - _antiAliasText = false; + Bold = false; + AutoClear = true; + AntiAliasText = false; } #endregion @@ -76,7 +77,6 @@ public NetImage() catch { } - GC.Collect(); } #endregion @@ -164,22 +164,14 @@ public enum TextAlignments : int // ==================================================================== #region Private Properties - private bool _autoClear; - private bool _antiAliasText; private int _backgroundColor; - private bool _bold; - private bool _constrainResize; private string _error; private string _filename; private int _fontColor; - private string _fontName; - private int _fontSize; - private ImageFormats _imageFormat; private int _jpegQuality; - private bool _progressiveJPEGEncoding; private int _maxX; private int _maxY; - private System.Drawing.Image _image; + private Image _image; private string _registeredTo; private Single _textAngle; private int _penColor; @@ -188,9 +180,25 @@ public enum TextAlignments : int private int _x; private int _y; private int _dpi; + private int _brushColor; #endregion + private static uint FlipFirstAndThirdDWORDBytes(int value) + { + return (((0xFF & (uint) value) << 0x10) | (0xFF00 & (uint) value) | ((0xFF0000 & (uint) value) >> 0x10)); + } + + public static int VBScriptRGBToDotNETARGB(int value) + { + return (int) (FlipFirstAndThirdDWORDBytes(value) | 0xFF000000); + } + + public static int DotNETARGBToVBScriptRGB(int value) + { + return (int) (FlipFirstAndThirdDWORDBytes(value) & 0x00FFFFFF); + } + // ==================================================================== #region Public Properties @@ -198,7 +206,6 @@ public enum TextAlignments : int #region Legacy Properties To Be Implemented public bool AutoSize = true; - public int BrushColor = 0; public int BrushStyle = 0; public bool Italic = false; public int PadSize = 0; @@ -216,33 +223,13 @@ public enum TextAlignments : int /// /// Gets or sets whether anti-aliased text is added on the image /// - public bool AntiAliasText - { - get - { - return _antiAliasText; - } - set - { - _antiAliasText = value; - } - } + public bool AntiAliasText { get; set; } /// /// Gets or sets whether the image should be disposed of from memory after /// a successful save to disk. /// - public bool AutoClear - { - get - { - return _autoClear; - } - set - { - _autoClear = value; - } - } + public bool AutoClear { get; set; } /// /// Integer value specifies the background color for NEW image manipulations. @@ -250,59 +237,33 @@ public bool AutoClear /// public int BackgroundColor { - get - { - return _backgroundColor; - } - set - { - // Set alpha channel to opaque, VBSCRIPT constants set it to 0 so the fill - // does not work because 0 means transparent - // Only solid colors are currently supported - _backgroundColor = (int)((uint)value | 0xFF000000); - } + get => DotNETARGBToVBScriptRGB(_backgroundColor); + set => _backgroundColor = VBScriptRGBToDotNETARGB(value); + } + + public int BrushColor + { + get => DotNETARGBToVBScriptRGB(_brushColor); + set => _brushColor = VBScriptRGBToDotNETARGB(value); } /// /// True/false value determines if font is bold or not /// - public bool Bold - { - get - { - return _bold; - } - set - { - _bold = value; - } - } + public bool Bold { get; set; } /// /// Gets or sets whether the resize method should constrain the image's original /// aspect ratio and if so, center-crops the image if needed to fit the dimensions /// - public bool ConstrainResize - { - get - { - return _constrainResize; - } - set - { - _constrainResize = value; - } - } + public bool ConstrainResize { get; set; } /// /// Set DPI /// public int DPI { - get - { - return _dpi; - } + get => _dpi; set { _dpi = value; @@ -313,12 +274,20 @@ public int DPI } var tempImage = new Bitmap(_image); - tempImage.SetResolution(value, value); - if (Math.Abs(tempImage.HorizontalResolution - tempImage.VerticalResolution) <= 0.0) + try { - _dpi = Convert.ToInt32(tempImage.HorizontalResolution); - _image.Dispose(); - _image = tempImage; + tempImage.SetResolution(value, value); + if (Math.Abs(tempImage.HorizontalResolution - tempImage.VerticalResolution) <= 0.0) + { + _dpi = Convert.ToInt32(tempImage.HorizontalResolution); + _image.Dispose(); + _image = tempImage; + } + } + catch + { + tempImage.Dispose(); + throw; } } } @@ -326,39 +295,20 @@ public int DPI /// /// Gets the last error string that occurred with the object /// - public string Error - { - get - { - return _error; - } - } + public string Error => _error; /// /// For compatibility, returns a future date that the component supposedly expires /// - public string Expires - { - get - { - // TODO: Verify date format returned from original component - return DateTime.Now.AddYears(50).ToString(CultureInfo.InvariantCulture); - } - } + public string Expires => DateTime.Now.AddYears(50).ToString(CultureInfo.InvariantCulture); /// /// Gets or sets the full path and filename used by the SaveImage() method /// public string Filename { - get - { - return _filename; - } - set - { - _filename = value.Trim(); - } + get => _filename; + set => _filename = value.Trim(); } /// @@ -366,45 +316,19 @@ public string Filename /// public int FontColor { - get - { - return _fontColor; - } - set - { - _fontColor = value; - } + get => DotNETARGBToVBScriptRGB(_fontColor); + set => _fontColor = VBScriptRGBToDotNETARGB(value); } /// /// The string FontName specifies the name of the font /// - public string FontName - { - get - { - return _fontName; - } - set - { - _fontName = value; - } - } + public string FontName { get; set; } /// /// The integer FontSize specifies the size of the font /// - public int FontSize - { - get - { - return _fontSize; - } - set - { - _fontSize = value; - } - } + public int FontSize { get; set; } /// /// Returns the raw binary data for the image. @@ -414,51 +338,41 @@ public byte[] Image { get { - var ms = new MemoryStream(); - switch (ImageFormat) + using (var ms = new MemoryStream()) { - case ImageFormats.BMP: - _image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); - break; - case ImageFormats.GIF: - _image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); - break; - case ImageFormats.PNG: - _image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); - break; - case ImageFormats.JPEG: - default: - _image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); - break; + switch (ImageFormat) + { + case ImageFormats.BMP: + _image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); + break; + case ImageFormats.GIF: + _image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); + break; + case ImageFormats.PNG: + _image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); + break; + case ImageFormats.JPEG: + default: + _image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); + break; + } + + return ms.ToArray(); } - return ms.ToArray(); } } /// /// Gets or sets the image format to be used when saving the image. /// - public ImageFormats ImageFormat - { - get - { - return _imageFormat; - } - set - { - _imageFormat = value; - } - } + public ImageFormats ImageFormat { get; set; } /// /// Gets or sets the quality percentage for saved JPEG images /// public int JPEGQuality { - get - { - return _jpegQuality; - } + get => _jpegQuality; set { _jpegQuality = value; @@ -479,15 +393,7 @@ public int JPEGQuality /// public int MaxX { - get - { - if (_image != null) - { - return _image.Width; - } - - return 0; - } + get => _image?.Width ?? 0; set { _maxX = value; @@ -529,14 +435,8 @@ public int MaxY /// public int PenColor { - get - { - return _penColor; - } - set - { - _penColor = value; - } + get => DotNETARGBToVBScriptRGB(_penColor); + set => _penColor = VBScriptRGBToDotNETARGB(value); } /// @@ -551,10 +451,7 @@ public int PenColor /// public int PenStyle { - get - { - return _penStyle; - } + get => _penStyle; set { if (value >= 0 && value <= 4) @@ -575,10 +472,7 @@ public int PenStyle /// public int PenWidth { - get - { - return _penWidth; - } + get => _penWidth; set { _penWidth = value; @@ -594,42 +488,20 @@ public int PenWidth /// Gets or sets whether progressive JPEG encoding should be used when saving /// the image. Not currently supported. /// - public bool ProgressiveJPEGEncoding - { - get - { - return _progressiveJPEGEncoding; - } - set - { - _progressiveJPEGEncoding = value; - } - } + public bool ProgressiveJPEGEncoding { get; set; } /// /// Gets the classes .NET image instance /// - public Image RawNetImage - { - get - { - return _image; - } - } + public Image RawNetImage => _image; /// /// Gets or sets who the component is registered to. Provided for compatibility /// public string RegisteredTo { - get - { - return _registeredTo; - } - set - { - _registeredTo = value.Trim(); - } + get => _registeredTo; + set => _registeredTo = value.Trim(); } /// @@ -637,10 +509,7 @@ public string RegisteredTo /// public Single TextAngle { - get - { - return _textAngle; - } + get => _textAngle; set { _textAngle = value; @@ -653,29 +522,20 @@ public Single TextAngle { _textAngle = 360; } - } + } } /// /// Returns the current version number of the component /// - public string Version - { - get - { - return "2.3.1.0 (ASPNetImage v0.3)"; - } - } + public string Version => "2.3.1.0 (ASPNetImage v0.3)"; /// /// Gets or sets the current X coordinate of the pen /// public Int32 X { - get - { - return _x; - } + get => _x; set { _x = value; @@ -696,10 +556,7 @@ public Int32 X /// public Int32 Y { - get - { - return _y; - } + get => _y; set { _y = value; @@ -755,31 +612,44 @@ private void AdjustBrightness(float adjustmentValue) // Based off James Craig's ColorMatrix blog entry at: // http://www.gutgames.com/post/Adjusting-Brightness-of-an-Image-in-C.aspx - var tempImage = new Bitmap(_image); - float finalValue = adjustmentValue / 255.0f; - var newBitmap = new Bitmap(tempImage.Width, tempImage.Height); - Graphics newGraphics = Graphics.FromImage(newBitmap); - - float[][] floatColorMatrix = { - new float[] {1, 0, 0, 0, 0}, - new float[] {0, 1, 0, 0, 0}, - new float[] {0, 0, 1, 0, 0}, - new float[] {0, 0, 0, 1, 0}, - new float[] {finalValue, finalValue, finalValue, 1, 1} - }; - var colorMtrx = new ColorMatrix(floatColorMatrix); - var imageAttr = new ImageAttributes(); - imageAttr.SetColorMatrix(colorMtrx); + using (var tempImage = new Bitmap(_image)) + { + float finalValue = adjustmentValue / 255.0f; + var newBitmap = new Bitmap(tempImage.Width, tempImage.Height); + try + { + using (Graphics newGraphics = Graphics.FromImage(newBitmap)) + { - newGraphics.DrawImage(tempImage, new Rectangle(0, 0, tempImage.Width, tempImage.Height), 0, 0, tempImage.Width, tempImage.Height, GraphicsUnit.Pixel, imageAttr); + float[][] floatColorMatrix = + { + new float[] {1, 0, 0, 0, 0}, + new float[] {0, 1, 0, 0, 0}, + new float[] {0, 0, 1, 0, 0}, + new float[] {0, 0, 0, 1, 0}, + new float[] {finalValue, finalValue, finalValue, 1, 1} + }; + var colorMtrx = new ColorMatrix(floatColorMatrix); + using (var imageAttr = new ImageAttributes()) + { + imageAttr.SetColorMatrix(colorMtrx); - // Get rid of the old image data first - _image.Dispose(); - _image = newBitmap; + newGraphics.DrawImage(tempImage, new Rectangle(0, 0, tempImage.Width, tempImage.Height), 0, + 0, + tempImage.Width, tempImage.Height, GraphicsUnit.Pixel, imageAttr); - // Cleanup - imageAttr.Dispose(); - newGraphics.Dispose(); + // Get rid of the old image data first + _image.Dispose(); + _image = newBitmap; + } + } + } + catch + { + newBitmap.Dispose(); + throw; + } + } } /// @@ -790,9 +660,11 @@ private void AdjustBrightness(float adjustmentValue) /// private byte[] ImageToByteArray(Image imageIn, ImageFormat imgFormat) { - var ms = new MemoryStream(); - imageIn.Save(ms, imgFormat); - return ms.ToArray(); + using (var ms = new MemoryStream()) + { + imageIn.Save(ms, imgFormat); + return ms.ToArray(); + } } /// @@ -802,9 +674,11 @@ private byte[] ImageToByteArray(Image imageIn, ImageFormat imgFormat) /// private Image ByteArrayToImage(byte[] byteArrayIn) { - var ms = new MemoryStream(byteArrayIn); - Image returnImage = System.Drawing.Image.FromStream(ms); - return returnImage; + using (var ms = new MemoryStream(byteArrayIn)) + { + Image returnImage = System.Drawing.Image.FromStream(ms); + return returnImage; + } } /// @@ -824,22 +698,27 @@ private Graphics GetGraphicsImage(Image image, string tipo) } catch (Exception) { - var ms = new MemoryStream(); - switch (tipo.ToLower()) + using (var ms = new MemoryStream()) { - case "gif": - image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); - break; - case "png": - image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); - break; - default: - image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); - break; - } + switch (tipo.ToLower()) + { + case "gif": + image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); + break; + case "png": + image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); + break; + default: + image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); + break; + } - Image img = System.Drawing.Image.FromStream(ms); - gra = Graphics.FromImage(img); + Image + img = System.Drawing.Image + .FromStream( + ms); // ToDo: What about the lifecycle of the img object? Will the Graphics object gra Dispose() it? + gra = Graphics.FromImage(img); + } } return gra; @@ -914,6 +793,21 @@ public void EndPath() public void Ellipse(int intX1, int intY1, int intX2, int intY2) { + using (var graphics = Graphics.FromImage(_image)) + { + var brushColor = Color.FromArgb(_backgroundColor); + using (Brush coloredBrush = new SolidBrush(brushColor)) + { + var pen = new Pen(Color.FromArgb(VBScriptRGBToDotNETARGB(_penColor))) + { + Width = PenWidth, + DashStyle = (DashStyle) PenStyle + }; + var rect = new Rectangle(intX1, intY1, intX2 - intX1 + 1, intY2 - intY1 + 1); + graphics.DrawEllipse(pen, rect); + graphics.FillEllipse(coloredBrush, rect); + } + } } public void FillPath() @@ -967,15 +861,37 @@ public void Pie(int intX1, int intY1, int intX2, int intY2, int intX3, int intY3 { } - public void PolyBezier(int[,] aryPoints) + public void PolyBezier(object[] aryPoints) { } - public void Polygon(int[,] aryPoints) + public void Polygon(object aryPoints) { + using (var graphics = Graphics.FromImage(_image)) + { + var brushColor = Color.FromArgb(_brushColor); + using (Brush coloredBrush = new SolidBrush(brushColor)) + { + var pen = new Pen(Color.FromArgb(VBScriptRGBToDotNETARGB(_penColor))) + { + Width = PenWidth, + DashStyle = (DashStyle) PenStyle + }; + + var points = new Point[((object[,]) aryPoints).Length / 2]; + for (var i = 0; i < ((object[,]) aryPoints).Length / 2; i++) + { + points[i].X = Convert.ToInt32(((object[,]) aryPoints)[i, 0]); + points[i].Y = Convert.ToInt32(((object[,]) aryPoints)[i, 1]); + } + + graphics.DrawPolygon(pen, points); + graphics.FillPolygon(coloredBrush, points); + } + } } - public void PolyLine(int[,] aryPoint) + public void PolyLine([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_I4)] int[,] aryPoint) { } @@ -994,6 +910,17 @@ public bool SaveAnimation() public void SetPixel(int intX, int intY, int intColor) { + using (var pen = new Pen(Color.FromArgb(VBScriptRGBToDotNETARGB(intColor))) + { + Width = PenWidth, + DashStyle = (DashStyle) PenStyle + }) + { + using (var graphics = Graphics.FromImage(_image)) + { + graphics.DrawLine(pen, new Point(intX, intY), new Point(intX, intY)); + } + } } public void Sharpen(int intValue) @@ -1031,26 +958,26 @@ public void Wave(int intGraphicSize, int intWaveSize) public bool AddImage(string strFileName, int intX, int intY) { FileStream fileStream = null; - Graphics graphicsDest = Graphics.FromImage(_image); - - try - { - fileStream = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - Image addedImage = System.Drawing.Image.FromStream(fileStream); - graphicsDest.DrawImage(addedImage, intX, intY); - } - catch (Exception e) + using (var graphicsDest = Graphics.FromImage(_image)) { - _error = e.ToString(); - } - finally - { - graphicsDest.Dispose(); - // release image file in the file system - if (fileStream != null) + + try { - fileStream.Close(); - fileStream.Dispose(); + fileStream = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + Image addedImage = System.Drawing.Image.FromStream(fileStream); + graphicsDest.DrawImage(addedImage, intX, intY); + } + catch (Exception e) + { + _error = e.ToString(); + } + finally + { + if (fileStream != null) + { + fileStream.Close(); + fileStream.Dispose(); + } } } @@ -1065,8 +992,7 @@ public bool AddImage(string strFileName, int intX, int intY) /// public bool AddImage(byte[] imageToAdd, int intX, int intY) { - Graphics graphicsDest = Graphics.FromImage(_image); - + var graphicsDest = Graphics.FromImage(_image); try { graphicsDest.DrawImage(ByteArrayToImage(imageToAdd), intX, intY); @@ -1121,15 +1047,16 @@ public void ClearImage() _image = bitmapDest; } - graphicsDest = Graphics.FromImage(_image); - graphicsDest.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; - Color brushColor = Color.FromArgb(BackgroundColor); - Brush coloredBrush = new SolidBrush(brushColor); - graphicsDest.FillRectangle(coloredBrush, 0, 0, _image.Width, _image.Height); - graphicsDest.DrawImage(_image, 0, 0); - - graphicsDest.Dispose(); - coloredBrush.Dispose(); + using (graphicsDest = Graphics.FromImage(_image)) + { + graphicsDest.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; + var brushColor = Color.FromArgb(_backgroundColor); + using (Brush coloredBrush = new SolidBrush(brushColor)) + { + graphicsDest.FillRectangle(coloredBrush, 0, 0, _image.Width, _image.Height); + graphicsDest.DrawImage(_image, 0, 0); + } + } } /// @@ -1144,18 +1071,23 @@ public void Contrast(int intDegree) intDegree = 100; float factor = (float)Math.Pow((100.0 + intDegree) / 100.0, 2.0); - var graphicsDest = Graphics.FromImage(_image); - var imageAttr = new ImageAttributes(); - var colorMtrx = new ColorMatrix(new float[][]{ new float[]{factor,0f,0f,0f,0f}, - new float[]{0f,factor,0f,0f,0f}, - new float[]{0f,0f,factor,0f,0f}, - new float[]{0f,0f,0f,1f,0f}, - new float[]{0.001f,0.001f,0.001f,0f,1f}}); - imageAttr.SetColorMatrix(colorMtrx); - graphicsDest.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel, imageAttr); - - graphicsDest.Dispose(); - imageAttr.Dispose(); + using (var graphicsDest = Graphics.FromImage(_image)) + { + using (var imageAttr = new ImageAttributes()) + { + var colorMtrx = new ColorMatrix(new float[][] + { + new float[] {factor, 0f, 0f, 0f, 0f}, + new float[] {0f, factor, 0f, 0f, 0f}, + new float[] {0f, 0f, factor, 0f, 0f}, + new float[] {0f, 0f, 0f, 1f, 0f}, + new float[] {0.001f, 0.001f, 0.001f, 0f, 1f} + }); + imageAttr.SetColorMatrix(colorMtrx); + graphicsDest.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, + _image.Height, GraphicsUnit.Pixel, imageAttr); + } + } } /// @@ -1163,20 +1095,25 @@ public void Contrast(int intDegree) /// public void CreateGrayScale() { - Graphics graphicsDest = Graphics.FromImage(_image); - - var imageAttr = new ImageAttributes(); - var colorMtrx = new ColorMatrix(new float[][]{ new float[]{0.299f, 0.299f, 0.299f, 0, 0}, - new float[]{0.587f, 0.587f, 0.587f, 0, 0}, - new float[]{0.114f, 0.114f, 0.114f, 0, 0}, - new float[]{ 0, 0, 0, 1, 0}, - new float[]{ 0, 0, 0, 0, 0}}); - - imageAttr.SetColorMatrix(colorMtrx); - graphicsDest.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel, imageAttr); + using (Graphics graphicsDest = Graphics.FromImage(_image)) + { - graphicsDest.Dispose(); - imageAttr.Dispose(); + using (var imageAttr = new ImageAttributes()) + { + var colorMtrx = new ColorMatrix(new float[][] + { + new float[] {0.299f, 0.299f, 0.299f, 0, 0}, + new float[] {0.587f, 0.587f, 0.587f, 0, 0}, + new float[] {0.114f, 0.114f, 0.114f, 0, 0}, + new float[] {0, 0, 0, 1, 0}, + new float[] {0, 0, 0, 0, 0} + }); + + imageAttr.SetColorMatrix(colorMtrx); + graphicsDest.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, + _image.Height, GraphicsUnit.Pixel, imageAttr); + } + } } /// @@ -1184,19 +1121,24 @@ public void CreateGrayScale() /// public void CreateNegative() { - Graphics graphicsDest = Graphics.FromImage(this._image); - - var imageAttr = new ImageAttributes(); - var colorMtrx = new ColorMatrix(new float[][]{ new float[]{-1, 0, 0, 0, 0}, - new float[]{0, -1, 0, 0, 0}, - new float[]{0, 0, -1, 0, 0}, - new float[]{0, 0, 0, 1, 0}, - new float[]{1, 1, 1, 0, 1}}); - imageAttr.SetColorMatrix(colorMtrx); - graphicsDest.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel, imageAttr); + using (Graphics graphicsDest = Graphics.FromImage(this._image)) + { - graphicsDest.Dispose(); - imageAttr.Dispose(); + using (var imageAttr = new ImageAttributes()) + { + var colorMtrx = new ColorMatrix(new float[][] + { + new float[] {-1, 0, 0, 0, 0}, + new float[] {0, -1, 0, 0, 0}, + new float[] {0, 0, -1, 0, 0}, + new float[] {0, 0, 0, 1, 0}, + new float[] {1, 1, 1, 0, 1} + }); + imageAttr.SetColorMatrix(colorMtrx); + graphicsDest.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, + _image.Height, GraphicsUnit.Pixel, imageAttr); + } + } } /// @@ -1209,15 +1151,23 @@ public void CreateNegative() public void CropImage(int startX, int startY, int width, int height) { var croppedImage = new Bitmap(width, height); - Graphics graphicsCrop = Graphics.FromImage(croppedImage); - graphicsCrop.Clear(Color.FromArgb(BackgroundColor)); - var recDest = new Rectangle(0, 0, width, height); - graphicsCrop.DrawImage(_image, recDest, startX, startY, width, height, GraphicsUnit.Pixel); - - _image.Dispose(); - _image = croppedImage; + try + { + using (Graphics graphicsCrop = Graphics.FromImage(croppedImage)) + { + graphicsCrop.Clear(Color.FromArgb(_backgroundColor)); + var recDest = new Rectangle(0, 0, width, height); + graphicsCrop.DrawImage(_image, recDest, startX, startY, width, height, GraphicsUnit.Pixel); - graphicsCrop.Dispose(); + _image.Dispose(); + _image = croppedImage; + } + } + catch + { + croppedImage.Dispose(); + throw; + } } /// @@ -1240,41 +1190,43 @@ public void DarkenImage(int percentage) /// public void Emboss() { - Graphics graphicsDest = Graphics.FromImage(_image); - var imgTemp = new Bitmap(_image); - imgTemp.SetResolution(_image.HorizontalResolution, _image.VerticalResolution); - var rect = new Rectangle(0, 0, imgTemp.Width, imgTemp.Height); - BitmapData bmpData = imgTemp.LockBits(rect, ImageLockMode.ReadWrite, _image.PixelFormat); - IntPtr ptr = bmpData.Scan0; - int numBytes = imgTemp.Width * imgTemp.Height * 3; - int rowLenght = imgTemp.Width * 3; - var rgbValues = new byte[numBytes]; - Marshal.Copy(ptr, rgbValues, 0, numBytes); - - for (int i = 0; i < rgbValues.Length; i += 3) + using (Graphics graphicsDest = Graphics.FromImage(_image)) { - if (i < rgbValues.Length - rowLenght - 3) + using (var imgTemp = new Bitmap(_image)) { - byte b = (byte)(rgbValues[i] - rgbValues[rowLenght + i + 3] + (byte)128); - rgbValues[i] = (byte)Math.Min((byte)Math.Abs(b), (byte)255); + imgTemp.SetResolution(_image.HorizontalResolution, _image.VerticalResolution); + var rect = new Rectangle(0, 0, imgTemp.Width, imgTemp.Height); + BitmapData bmpData = imgTemp.LockBits(rect, ImageLockMode.ReadWrite, _image.PixelFormat); + IntPtr ptr = bmpData.Scan0; + int numBytes = imgTemp.Width * imgTemp.Height * 3; + int rowLenght = imgTemp.Width * 3; + var rgbValues = new byte[numBytes]; + Marshal.Copy(ptr, rgbValues, 0, numBytes); + + for (int i = 0; i < rgbValues.Length; i += 3) + { + if (i < rgbValues.Length - rowLenght - 3) + { + byte b = (byte) (rgbValues[i] - rgbValues[rowLenght + i + 3] + (byte) 128); + rgbValues[i] = (byte) Math.Min((byte) Math.Abs(b), (byte) 255); - b = (byte)(rgbValues[i + 1] - rgbValues[rowLenght + i + 4] + (byte)128); - rgbValues[i + 1] = (byte)Math.Min((byte)Math.Abs(b), (byte)255); + b = (byte) (rgbValues[i + 1] - rgbValues[rowLenght + i + 4] + (byte) 128); + rgbValues[i + 1] = (byte) Math.Min((byte) Math.Abs(b), (byte) 255); - b = (byte)(rgbValues[i + 2] - rgbValues[rowLenght + i + 5] + (byte)128); - rgbValues[i + 2] = (byte)Math.Min((byte)Math.Abs(b), (byte)255); - } - else - { - rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = 128; + b = (byte) (rgbValues[i + 2] - rgbValues[rowLenght + i + 5] + (byte) 128); + rgbValues[i + 2] = (byte) Math.Min((byte) Math.Abs(b), (byte) 255); + } + else + { + rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = 128; + } + } + + Marshal.Copy(rgbValues, 0, ptr, numBytes); + imgTemp.UnlockBits(bmpData); + graphicsDest.DrawImage(imgTemp, 0, 0); } } - - Marshal.Copy(rgbValues, 0, ptr, numBytes); - imgTemp.UnlockBits(bmpData); - graphicsDest.DrawImage(imgTemp, 0, 0); - graphicsDest.Dispose(); - imgTemp.Dispose(); } /// @@ -1286,15 +1238,17 @@ public void Emboss() /// public void FillRect(int intLeft, int intTop, int intRight, int intBottom) { - Graphics graphicsDest = Graphics.FromImage(_image); - graphicsDest.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; - Color brushColor = Color.FromArgb(PenColor); - Brush coloredBrush = new SolidBrush(brushColor); - graphicsDest.FillRectangle(coloredBrush, intLeft, intTop, intRight - intLeft + 1, intBottom - intTop + 1); - graphicsDest.DrawImage(_image, 0, 0); - - graphicsDest.Dispose(); - coloredBrush.Dispose(); + using (Graphics graphicsDest = Graphics.FromImage(_image)) + { + graphicsDest.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; + Color brushColor = Color.FromArgb(_backgroundColor); + using (Brush coloredBrush = new SolidBrush(brushColor)) + { + graphicsDest.FillRectangle(coloredBrush, intLeft, intTop, intRight - intLeft + 1, + intBottom - intTop + 1); + graphicsDest.DrawImage(_image, 0, 0); + } + } } /// @@ -1324,15 +1278,15 @@ public void GetImageFileSize(string filePath, out int width, out int height) width = 0; height = 0; - var thisImage = System.Drawing.Image.FromFile(filePath); - - if (thisImage != null) + using (var thisImage = System.Drawing.Image.FromFile(filePath)) { - width = thisImage.Width; - height = thisImage.Height; - } - thisImage.Dispose(); + if (thisImage != null) + { + width = thisImage.Width; + height = thisImage.Height; + } + } } /// @@ -1362,7 +1316,7 @@ public int GetPixel(int x, int y) { if (_image != null && x < _image.Width && y <= _image.Height) { - return ((Bitmap)_image).GetPixel(x, y).ToArgb(); + return DotNETARGBToVBScriptRGB(((Bitmap)_image).GetPixel(x, y).ToArgb()); } return 0; @@ -1375,19 +1329,21 @@ public int GetPixel(int x, int y) /// public void LineTo(int x, int y) { - Graphics graphicsDest = Graphics.FromImage(_image); - - var pen = new Pen(Color.FromArgb((Int32)BitConverter.GetBytes(PenColor)[2], (Int32)BitConverter.GetBytes(PenColor)[1], (Int32)BitConverter.GetBytes(PenColor)[0])) + using (Graphics graphicsDest = Graphics.FromImage(_image)) { - Width = PenWidth, - DashStyle = (DashStyle)PenStyle - }; - graphicsDest.DrawLine(pen, X, Y, x, y); + var pen = new Pen(Color.FromArgb(_penColor)) + { + Width = PenWidth, + DashStyle = (DashStyle) PenStyle + }; + + graphicsDest.DrawLine(pen, X, Y, x, y); - // Update pen coordinates - _x = x; - _y = y; + // Update pen coordinates + _x = x; + _y = y; + } } /// @@ -1505,15 +1461,18 @@ public bool LoadImage(string imageFilePath) /// public void Rectangle(int intX1, int intY1, int intX2, int intY2) { - Graphics graphicsDest = GetGraphicsImage(_image, ImageFormat.ToString()); - - var pen = new Pen(Color.FromArgb((Int32)BitConverter.GetBytes(PenColor)[2], (Int32)BitConverter.GetBytes(PenColor)[1], (Int32)BitConverter.GetBytes(PenColor)[0])) + using (Graphics graphicsDest = GetGraphicsImage(_image, ImageFormat.ToString())) { - Width = PenWidth, - DashStyle = (DashStyle)PenStyle - }; - graphicsDest.DrawRectangle(pen, intX1, intY1, intX2, intY2); + var pen = new Pen(Color.FromArgb(_penColor)) + { + Width = PenWidth, + DashStyle = (DashStyle) PenStyle + }; + + graphicsDest.DrawRectangle(pen, intX1, intY1, intX2 - intX1 + 1, intY2 - intY1 + 1); + FillRect(intX1, intY1, intX2, intY2); + } } /// @@ -1551,29 +1510,43 @@ public void Resize(int width, int height) } } - // Check again since we may have changed our scale dimensions and only - // rescale the image if needed. - if (originalWidth != newWidth || originalHeight != newHeight) + try { - resizedImage = new Bitmap(newWidth, newHeight); - graphicsDest = Graphics.FromImage(resizedImage); - graphicsDest.DrawImage(_image, 0, 0, newWidth + 1, newHeight + 1); + // Check again since we may have changed our scale dimensions and only + // rescale the image if needed. + if (originalWidth != newWidth || originalHeight != newHeight) + { + resizedImage = new Bitmap(newWidth, newHeight); + try + { + graphicsDest = Graphics.FromImage(resizedImage); + graphicsDest.DrawImage(_image, 0, 0, newWidth + 1, newHeight + 1); - _image.Dispose(); - _image = resizedImage; - } + _image.Dispose(); + _image = resizedImage; + } + catch + { + resizedImage.Dispose(); + throw; + } + } - // Crop the image to the final size if necessary - if ((newHeight > height) || (newWidth > width)) - { - // Use the center of the image as our basis for cropping - int cropY = (newHeight == height ? 0 : Convert.ToInt32((newHeight - height) / 2)); - int cropX = (newWidth == width ? 0 : Convert.ToInt32((newWidth - width) / 2)); + // Crop the image to the final size if necessary + if ((newHeight > height) || (newWidth > width)) + { + // Use the center of the image as our basis for cropping + int cropY = (newHeight == height ? 0 : Convert.ToInt32((newHeight - height) / 2)); + int cropX = (newWidth == width ? 0 : Convert.ToInt32((newWidth - width) / 2)); - CropImage(cropX, cropY, width, height); + CropImage(cropX, cropY, width, height); + } + } + catch + { + if (graphicsDest != null) + graphicsDest.Dispose(); } - - if (graphicsDest != null) graphicsDest.Dispose(); } } @@ -1744,13 +1717,14 @@ public bool SaveImage() /// public int TextHeight(string strValue) { - Graphics graphicsDest = Graphics.FromImage(_image); - var font = new Font(FontName, Convert.ToSingle(FontSize)); - SizeF size = graphicsDest.MeasureString(strValue, font); - graphicsDest.Dispose(); - font.Dispose(); - - return Convert.ToInt32(size.Height); + using (Graphics graphicsDest = Graphics.FromImage(_image)) + { + using (var font = new Font(FontName, Convert.ToSingle(FontSize))) + { + SizeF size = graphicsDest.MeasureString(strValue, font); + return Convert.ToInt32(size.Height); + } + } } /// @@ -1764,51 +1738,55 @@ public int TextHeight(string strValue) /// public void TextOut(string strText, int intX, int intY, bool bol3D) { - Graphics graphicsDest = Graphics.FromImage(_image); - if (AntiAliasText) - graphicsDest.TextRenderingHint = TextRenderingHint.AntiAlias; - - var font = new Font(FontName, Convert.ToSingle(FontSize)); - Color color = Color.FromArgb((Int32)BitConverter.GetBytes(FontColor)[2], (Int32)BitConverter.GetBytes(FontColor)[1], (Int32)BitConverter.GetBytes(FontColor)[0]); - Brush brush = new SolidBrush(color); - if (TextAngle == 0) + using (Graphics graphicsDest = Graphics.FromImage(_image)) { - graphicsDest.DrawString(strText, font, brush, new Point(intX, intY)); - } - else - { - // reduce angle to 0°-360° range - Single angle = TextAngle; - if (angle > 360) - { - while (angle > 360) - { - angle = angle - 360; - } - } - else if (angle < 0) + if (AntiAliasText) + graphicsDest.TextRenderingHint = TextRenderingHint.AntiAlias; + + using (var font = new Font(FontName, Convert.ToSingle(FontSize))) { - while (angle < 0) + Color color = Color.FromArgb(_fontColor); + using (Brush brush = new SolidBrush(color)) { - angle = angle + 360; + if (TextAngle == 0) + { + graphicsDest.DrawString(strText, font, brush, new Point(intX, intY)); + } + else + { + // reduce angle to 0°-360° range + Single angle = TextAngle; + if (angle > 360) + { + while (angle > 360) + { + angle = angle - 360; + } + } + else if (angle < 0) + { + while (angle < 0) + { + angle = angle + 360; + } + } + + // save graphics state + GraphicsState state = graphicsDest.Save(); + // clear any traformations already in progress + graphicsDest.ResetTransform(); + // rotate + graphicsDest.RotateTransform(angle); + // translate origin to compensate rotation + graphicsDest.TranslateTransform(intX, intY, MatrixOrder.Append); + // write text + graphicsDest.DrawString(strText, font, brush, 0, 0); + // restore graphic state + graphicsDest.Restore(state); + } } } - // save graphics state - GraphicsState state = graphicsDest.Save(); - // clear any traformations already in progress - graphicsDest.ResetTransform(); - // rotate - graphicsDest.RotateTransform(angle); - // translate origin to compensate rotation - graphicsDest.TranslateTransform(intX, intY, MatrixOrder.Append); - // write text - graphicsDest.DrawString(strText, font, brush, 0, 0); - // restore graphic state - graphicsDest.Restore(state); } - graphicsDest.Dispose(); - brush.Dispose(); - font.Dispose(); } /// @@ -1818,13 +1796,14 @@ public void TextOut(string strText, int intX, int intY, bool bol3D) /// public int TextWidth(string strValue) { - Graphics graphicsDest = Graphics.FromImage(_image); - var font = new Font(FontName, Convert.ToSingle(FontSize)); - SizeF size = graphicsDest.MeasureString(strValue, font); - graphicsDest.Dispose(); - font.Dispose(); - - return Convert.ToInt32(size.Width); + using (Graphics graphicsDest = Graphics.FromImage(_image)) + { + using (var font = new Font(FontName, Convert.ToSingle(FontSize))) + { + SizeF size = graphicsDest.MeasureString(strValue, font); + return Convert.ToInt32(size.Width); + } + } } #endregion From 5a1543d2917f8c6fcdd5b7ff9402b469bef70748 Mon Sep 17 00:00:00 2001 From: Sebastian Battig Date: Mon, 22 Jul 2019 13:38:51 -0300 Subject: [PATCH 02/10] Changed encryption key file and added support for x64 target as pure 64 bits assembly --- ASPNetImage/ASPNetImage.csproj | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/ASPNetImage/ASPNetImage.csproj b/ASPNetImage/ASPNetImage.csproj index c39331a..ce8a65b 100644 --- a/ASPNetImage/ASPNetImage.csproj +++ b/ASPNetImage/ASPNetImage.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -11,8 +11,9 @@ ASPNetImage ASPNetImage true - ASPNetImage.snk - v2.0 + Beeline.snk + v4.6.1 + true @@ -23,6 +24,7 @@ prompt 4 true + false pdbonly @@ -32,6 +34,29 @@ prompt 4 true + false + + + true + bin\x64Debug\ + DEBUG;TRACE + true + full + x64 + prompt + MinimumRecommendedRules.ruleset + false + + + bin\x64Release\ + TRACE + true + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + false @@ -44,7 +69,7 @@ - + From b63e3b306e6509cd98f9a8c5cba0735ebfa0b3e4 Mon Sep 17 00:00:00 2001 From: Sebastian Battig Date: Mon, 22 Jul 2019 13:39:02 -0300 Subject: [PATCH 03/10] Added a couple of ignores --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6fd8a5b..fa686b4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.user *.vsmdi *.testsettings -*.suo +/.vs/ +/UnitTests/Connected Services/ From 73de6b354d6fdb04815279b63259aa67bdaaaabc Mon Sep 17 00:00:00 2001 From: Sebastian Battig Date: Mon, 22 Jul 2019 13:39:13 -0300 Subject: [PATCH 04/10] Added x64 targets --- ASPNetImage.sln | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ASPNetImage.sln b/ASPNetImage.sln index d9b609d..7554fc7 100644 --- a/ASPNetImage.sln +++ b/ASPNetImage.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.705 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASPNetImage", "ASPNetImage\ASPNetImage.csproj", "{B56B76E7-0BF9-4439-8AEB-91C6709628C5}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{EFB48FB4-4982-49E4-A007-98BFCE885C70}" @@ -8,19 +10,32 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Debug|x64.ActiveCfg = Debug|x64 + {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Debug|x64.Build.0 = Debug|x64 {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Release|Any CPU.ActiveCfg = Release|Any CPU {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Release|Any CPU.Build.0 = Release|Any CPU + {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Release|x64.ActiveCfg = Release|x64 + {B56B76E7-0BF9-4439-8AEB-91C6709628C5}.Release|x64.Build.0 = Release|x64 {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Debug|x64.ActiveCfg = Debug|x64 + {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Debug|x64.Build.0 = Debug|x64 {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Release|Any CPU.ActiveCfg = Release|Any CPU {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Release|Any CPU.Build.0 = Release|Any CPU + {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Release|x64.ActiveCfg = Release|x64 + {EFB48FB4-4982-49E4-A007-98BFCE885C70}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3323FF13-FA07-4E7B-BA1F-546A874D6E0E} + EndGlobalSection EndGlobal From f709c2faba603db58eff0b43ef1b35c727a1f1be Mon Sep 17 00:00:00 2001 From: Sebastian Battig Date: Mon, 22 Jul 2019 13:39:50 -0300 Subject: [PATCH 05/10] Modified tests to support new mode where colors taken in and out of the component are treated as VBScript compatible RGB colors (first and third bytes are flipped) --- UnitTests/ASPNetImageTest.cs | 44 ++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/UnitTests/ASPNetImageTest.cs b/UnitTests/ASPNetImageTest.cs index cb5c93c..b2087a1 100644 --- a/UnitTests/ASPNetImageTest.cs +++ b/UnitTests/ASPNetImageTest.cs @@ -41,10 +41,10 @@ public void TestPropertyAntiAliasText() public void TestPropertyBackgroundColor() { var thisImage = new NetImage(); - var testValue = System.Drawing.Color.FromArgb(255, 0, 255, 255).ToArgb(); // Solid Cyan - thisImage.BackgroundColor = System.Drawing.Color.FromArgb(127, 0, 255, 255).ToArgb(); // Try setting it to 50% transparent cyan + var testValue = 0xFFFF00FF; // Solid Cyan + thisImage.BackgroundColor = 0x7FFF00FF; // Try setting it to 50% transparent cyan - Assert.AreEqual(testValue, thisImage.BackgroundColor); + Assert.AreEqual(testValue & 0x00FFFFFF, thisImage.BackgroundColor); } [Test] @@ -90,9 +90,9 @@ public void TestPropertyFontColor() { var thisImage = new NetImage(); const int testValue = -8421505; //System.Drawing.Color.FromArgb(255, 127, 127, 127).ToArgb(); - thisImage.FontColor = System.Drawing.Color.FromArgb(255, 127, 127, 127).ToArgb(); + thisImage.FontColor = NetImage.DotNETARGBToVBScriptRGB(System.Drawing.Color.FromArgb(255, 127, 127, 127).ToArgb()); - Assert.AreEqual(testValue, thisImage.FontColor); + Assert.AreEqual(testValue, NetImage.VBScriptRGBToDotNETARGB(thisImage.FontColor)); } [Test] @@ -225,9 +225,10 @@ public void TestPropertyImage() [Test] public void TestPropertyPenColor() { - var thisImage = new NetImage { PenColor = System.Drawing.Color.FromArgb(50, 255, 0, 0).ToArgb() }; + var penColor = 0xFFFE10; + var thisImage = new NetImage { PenColor = penColor }; - Assert.AreEqual(System.Drawing.Color.FromArgb(50, 255, 0, 0).ToArgb(), thisImage.PenColor); + Assert.AreEqual(penColor, thisImage.PenColor); } [Test] @@ -515,7 +516,7 @@ public void TestFillRect() thisImage.LoadImage("../../Resources/1024x768-white.png"); thisImage.ImageFormat = NetImage.ImageFormats.PNG; thisImage.Filename = outputFilePath; - thisImage.PenColor = Color.Red.ToArgb(); + thisImage.BackgroundColor = NetImage.DotNETARGBToVBScriptRGB(Color.Red.ToArgb()); thisImage.FillRect(10, 10, 20, 20); thisImage.AutoClear = false; // Don't clear on save so we can still access raw image thisImage.SaveImage(); @@ -597,21 +598,21 @@ public void TestGetPixel() Assert.AreEqual(0, pixelColor); // Initialize image - thisImage.BackgroundColor = System.Drawing.Color.FromArgb(50, 255, 0, 0).ToArgb(); + thisImage.BackgroundColor = NetImage.DotNETARGBToVBScriptRGB(0x32FF0000); thisImage.MaxX = 1024; thisImage.MaxY = 768; pixelColor = thisImage.GetPixel(640, 480); // Backgrounds can only be solid colors - Assert.AreEqual(System.Drawing.Color.FromArgb(255, 255, 0, 0).ToArgb(), pixelColor); + Assert.AreEqual(System.Drawing.Color.FromArgb(255, 255, 0, 0).ToArgb(), NetImage.VBScriptRGBToDotNETARGB(pixelColor)); // Verify color change - thisImage.BackgroundColor = System.Drawing.Color.FromArgb(50, 0, 0, 255).ToArgb(); + thisImage.BackgroundColor = NetImage.DotNETARGBToVBScriptRGB(System.Drawing.Color.FromArgb(50, 0, 0, 255).ToArgb()); thisImage.ClearImage(); pixelColor = thisImage.GetPixel(640, 480); - Assert.AreEqual(System.Drawing.Color.FromArgb(255, 0, 0, 255).ToArgb(), pixelColor); + Assert.AreEqual(System.Drawing.Color.FromArgb(255, 0, 0, 255).ToArgb(), NetImage.VBScriptRGBToDotNETARGB(pixelColor)); } [Test] @@ -622,7 +623,7 @@ public void TestLineTo() var blueColor = System.Drawing.Color.FromArgb(255, 0, 0, 255).ToArgb(); var thisImage = new NetImage(); - thisImage.BackgroundColor = System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb(); + thisImage.BackgroundColor = NetImage.DotNETARGBToVBScriptRGB(System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb()); thisImage.MaxX = 1024; thisImage.MaxY = 1024; thisImage.Filename = outputFilePath; @@ -631,11 +632,11 @@ public void TestLineTo() thisImage.X = 0; thisImage.Y = 0; - thisImage.PenColor = redColor; + thisImage.PenColor = NetImage.DotNETARGBToVBScriptRGB(redColor); thisImage.PenWidth = 1; thisImage.LineTo(1023, 1023); - thisImage.PenColor = blueColor; + thisImage.PenColor = NetImage.DotNETARGBToVBScriptRGB(blueColor); thisImage.LineTo(1023, 0); thisImage.SaveImage(); @@ -643,13 +644,13 @@ public void TestLineTo() // Verify diagonal pixels for (int x = 0; x <= 1022; x++) // Only to 1022 because blue line covers up bottom right pixel { - Assert.AreEqual(redColor, thisImage.GetPixel(x, x)); + Assert.AreEqual(redColor, NetImage.VBScriptRGBToDotNETARGB(thisImage.GetPixel(x, x))); } // Verify vertical pixels for (int y = 0; y <= 1023; y++) { - Assert.AreEqual(blueColor, thisImage.GetPixel(1023, y)); + Assert.AreEqual(blueColor, NetImage.VBScriptRGBToDotNETARGB(thisImage.GetPixel(1023, y))); } } @@ -856,6 +857,15 @@ public void TestTextWidth() // ==================================================================== #region Miscellaneous Tests + [Test] + public void TestCreateCOM() + { + var type = Type.GetTypeFromProgID("AspNetImage.NetImage"); + Assert.AreNotEqual(null, type, "type is null"); + var obj = Activator.CreateInstance(type); + Assert.AreNotEqual(null, obj, "obj is null"); + } + [Test] public void TestGIMPImage() { From 327ebf10eb87e26bee974f83fc3da3d530f1a32e Mon Sep 17 00:00:00 2001 From: Sebastian Battig Date: Mon, 22 Jul 2019 13:40:03 -0300 Subject: [PATCH 06/10] Changed target .net to 4.6.1 --- UnitTests/UnitTests.csproj | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 3e5f1f3..5864bef 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -12,7 +12,8 @@ UnitTests - v2.0 + v4.6.1 + true @@ -22,6 +23,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -30,6 +32,27 @@ TRACE prompt 4 + false + + + true + bin\x64Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + false + + + bin\x64Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + false @@ -61,6 +84,9 @@ + + +