Browse Source

Added an extra constructor to image that accepts a byte array.

af/merge-core
Dirk Lemstra 9 years ago
parent
commit
a614c3062e
  1. 12
      src/ImageSharp/Image.cs
  2. 17
      src/ImageSharp/Image/Image.cs
  3. 32
      tests/ImageSharp.Tests/Image/ImageTests.cs
  4. 8
      tests/ImageSharp.Tests/TestFile.cs

12
src/ImageSharp/Image.cs

@ -45,6 +45,18 @@ namespace ImageSharp
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
/// <param name="bytes">
/// The byte array containing image information.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception>
public Image(byte[] bytes)
: base(bytes)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class
/// by making a copy from another image.

17
src/ImageSharp/Image/Image.cs

@ -72,6 +72,23 @@ namespace ImageSharp
this.Load(stream);
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TColor, TPacked}"/> class.
/// </summary>
/// <param name="bytes">
/// The byte array containing image information.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception>
public Image(byte[] bytes)
{
Guard.NotNull(bytes, nameof(bytes));
using (MemoryStream stream = new MemoryStream(bytes, false))
{
this.Load(stream);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TColor, TPacked}"/> class
/// by making a copy from another image.

32
tests/ImageSharp.Tests/Image/ImageTests.cs

@ -0,0 +1,32 @@
// <copyright file="PixelAccessorTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using Xunit;
/// <summary>
/// Tests the <see cref="Image{TPacked,TPixel}"/> class.
/// </summary>
public class ImageTests
{
[Fact]
public void ConstructorByteArray()
{
Assert.Throws<ArgumentNullException>(() =>
{
new Image((byte[])null);
});
TestFile file = TestImages.Bmp.Car;
var image = new Image<Color, uint>(file.Bytes);
Assert.Equal(600, image.Width);
Assert.Equal(450, image.Height);
}
}
}

8
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

Loading…
Cancel
Save