From a614c3062eeebe22d8303783589c22d6e898ce3f Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Mon, 19 Dec 2016 20:38:17 +0100 Subject: [PATCH] Added an extra constructor to image that accepts a byte array. --- src/ImageSharp/Image.cs | 12 ++++++++ src/ImageSharp/Image/Image.cs | 17 ++++++++++++ tests/ImageSharp.Tests/Image/ImageTests.cs | 32 ++++++++++++++++++++++ tests/ImageSharp.Tests/TestFile.cs | 8 +++--- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.cs diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index 9dbfacbba..8c03d64e9 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -45,6 +45,18 @@ namespace ImageSharp { } + /// + /// Initializes a new instance of the class. + /// + /// + /// The byte array containing image information. + /// + /// Thrown if the is null. + public Image(byte[] bytes) + : base(bytes) + { + } + /// /// Initializes a new instance of the class /// by making a copy from another image. diff --git a/src/ImageSharp/Image/Image.cs b/src/ImageSharp/Image/Image.cs index 6dfd1c6db..cec75102a 100644 --- a/src/ImageSharp/Image/Image.cs +++ b/src/ImageSharp/Image/Image.cs @@ -72,6 +72,23 @@ namespace ImageSharp this.Load(stream); } + /// + /// Initializes a new instance of the class. + /// + /// + /// The byte array containing image information. + /// + /// Thrown if the is null. + public Image(byte[] bytes) + { + Guard.NotNull(bytes, nameof(bytes)); + + using (MemoryStream stream = new MemoryStream(bytes, false)) + { + this.Load(stream); + } + } + /// /// Initializes a new instance of the class /// by making a copy from another image. diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs new file mode 100644 index 000000000..2dd73a0b5 --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -0,0 +1,32 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + + using Xunit; + + /// + /// Tests the class. + /// + public class ImageTests + { + [Fact] + public void ConstructorByteArray() + { + Assert.Throws(() => + { + new Image((byte[])null); + }); + + TestFile file = TestImages.Bmp.Car; + var image = new Image(file.Bytes); + + Assert.Equal(600, image.Width); + Assert.Equal(450, image.Height); + } + } +} diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index f7e7f8517..89a724191 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -16,12 +16,12 @@ namespace ImageSharp.Tests { this.file = file; - using (FileStream stream = File.OpenRead(file)) - { - this.image = new Image(stream); - } + this.Bytes = File.ReadAllBytes(file); + this.image = new Image(this.Bytes); } + public byte[] Bytes { get; } + public string FileName { get