diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs b/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs index 5f3a22d..6a756a6 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs @@ -463,9 +463,17 @@ public T ToBitmap() /// Create a new Bitmap from a a Byte Span. /// /// A Byte Span of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. - public static AnyBitmap FromSpan(ReadOnlySpan span, bool preserveOriginalFormat = true) + public static AnyBitmap FromSpan(ReadOnlySpan span) + { + return new AnyBitmap(span, true); + } + + /// + /// Create a new Bitmap from a a Byte Span. + /// + /// A Byte Span of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + public static AnyBitmap FromSpan(ReadOnlySpan span, bool preserveOriginalFormat) { return new AnyBitmap(span, preserveOriginalFormat); } @@ -474,9 +482,17 @@ public static AnyBitmap FromSpan(ReadOnlySpan span, bool preserveOriginalF /// Create a new Bitmap from a a Byte Array. /// /// A ByteArray of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. - public static AnyBitmap FromBytes(byte[] bytes, bool preserveOriginalFormat = true) + public static AnyBitmap FromBytes(byte[] bytes) + { + return new AnyBitmap(bytes, true); + } + + /// + /// Create a new Bitmap from a a Byte Array. + /// + /// A ByteArray of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + public static AnyBitmap FromBytes(byte[] bytes, bool preserveOriginalFormat) { return new AnyBitmap(bytes, preserveOriginalFormat); } @@ -484,12 +500,23 @@ public static AnyBitmap FromBytes(byte[] bytes, bool preserveOriginalFormat = tr /// /// Create a new Bitmap from a (bytes). /// - /// A of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. + /// A of image data in any common format. /// Default is true. Set to false to load as Rgba32. /// /// - public static AnyBitmap FromStream(MemoryStream stream, bool preserveOriginalFormat = true) + public static AnyBitmap FromStream(MemoryStream stream) + { + return new AnyBitmap(stream, true); + } + + /// + /// Create a new Bitmap from a (bytes). + /// + /// A of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + public static AnyBitmap FromStream(MemoryStream stream, bool preserveOriginalFormat) { return new AnyBitmap(stream, preserveOriginalFormat); } @@ -498,11 +525,21 @@ public static AnyBitmap FromStream(MemoryStream stream, bool preserveOriginalFor /// Create a new Bitmap from a (bytes). /// /// A of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. /// /// - public static AnyBitmap FromStream(Stream stream, bool preserveOriginalFormat = true) + public static AnyBitmap FromStream(Stream stream) + { + return new AnyBitmap(stream, true); + } + + /// + /// Create a new Bitmap from a (bytes). + /// + /// A of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + public static AnyBitmap FromStream(Stream stream, bool preserveOriginalFormat) { return new AnyBitmap(stream, preserveOriginalFormat); } @@ -511,10 +548,19 @@ public static AnyBitmap FromStream(Stream stream, bool preserveOriginalFormat = /// Construct a new Bitmap from binary data (byte span). /// /// A byte span of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. /// - public AnyBitmap(ReadOnlySpan span, bool preserveOriginalFormat = true) + public AnyBitmap(ReadOnlySpan span) + { + LoadImage(span, true); + } + + /// + /// Construct a new Bitmap from binary data (byte span). + /// + /// A byte span of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + /// + public AnyBitmap(ReadOnlySpan span, bool preserveOriginalFormat) { LoadImage(span, preserveOriginalFormat); } @@ -523,11 +569,21 @@ public AnyBitmap(ReadOnlySpan span, bool preserveOriginalFormat = true) /// Construct a new Bitmap from binary data (bytes). /// /// A ByteArray of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. - /// + /// + /// + public AnyBitmap(byte[] bytes) + { + LoadImage(bytes, true); + } + + /// + /// Construct a new Bitmap from binary data (bytes). + /// + /// A ByteArray of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + /// /// - public AnyBitmap(byte[] bytes, bool preserveOriginalFormat = true) + public AnyBitmap(byte[] bytes, bool preserveOriginalFormat) { LoadImage(bytes, preserveOriginalFormat); } @@ -536,8 +592,18 @@ public AnyBitmap(byte[] bytes, bool preserveOriginalFormat = true) /// Construct a new Bitmap from a (bytes). /// /// A of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. + /// + /// + public AnyBitmap(MemoryStream stream) + { + LoadImage(stream.ToArray(), true); + } + + /// + /// Construct a new Bitmap from a (bytes). + /// + /// A of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. /// /// public AnyBitmap(MemoryStream stream, bool preserveOriginalFormat = true) @@ -549,11 +615,21 @@ public AnyBitmap(MemoryStream stream, bool preserveOriginalFormat = true) /// Construct a new Bitmap from a (bytes). /// /// A of image data in any common format. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. /// /// - public AnyBitmap(Stream stream, bool preserveOriginalFormat = true) + public AnyBitmap(Stream stream) + { + LoadImage(stream, true); + } + + /// + /// Construct a new Bitmap from a (bytes). + /// + /// A of image data in any common format. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + public AnyBitmap(Stream stream, bool preserveOriginalFormat) { LoadImage(stream, preserveOriginalFormat); } @@ -574,11 +650,21 @@ public AnyBitmap(AnyBitmap original, int width, int height) /// Construct a new Bitmap from a file. /// /// A fully qualified file path./ - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. - /// + /// /// - public AnyBitmap(string file, bool preserveOriginalFormat = true) + public AnyBitmap(string file) + { + LoadImage(File.ReadAllBytes(file), true); + } + + /// + /// Construct a new Bitmap from a file. + /// + /// A fully qualified file path./ + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + public AnyBitmap(string file, bool preserveOriginalFormat) { LoadImage(File.ReadAllBytes(file), preserveOriginalFormat); } @@ -587,11 +673,29 @@ public AnyBitmap(string file, bool preserveOriginalFormat = true) /// Construct a new Bitmap from a Uri /// /// The uri of the image. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. - /// + /// /// - public AnyBitmap(Uri uri, bool preserveOriginalFormat = true) + public AnyBitmap(Uri uri) + { + try + { + using Stream stream = LoadUriAsync(uri).GetAwaiter().GetResult(); + LoadImage(stream, true); + } + catch (Exception e) + { + throw new NotSupportedException("Error while loading AnyBitmap from Uri", e); + } + } + + /// + /// Construct a new Bitmap from a Uri + /// + /// The uri of the image. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + public AnyBitmap(Uri uri, bool preserveOriginalFormat) { try { @@ -619,11 +723,28 @@ public AnyBitmap(int width, int height, Color backgroundColor = null) /// Create a new Bitmap from a file. /// /// A fully qualified file path. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. - /// + /// /// - public static AnyBitmap FromFile(string file, bool preserveOriginalFormat = true) + public static AnyBitmap FromFile(string file) + { + if (file.ToLower().EndsWith(".svg")) + { + return LoadSVGImage(file, true); + } + else + { + return new AnyBitmap(file, true); + } + } + + /// + /// Create a new Bitmap from a file. + /// + /// A fully qualified file path. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + public static AnyBitmap FromFile(string file, bool preserveOriginalFormat) { if (file.ToLower().EndsWith(".svg")) { @@ -639,13 +760,33 @@ public static AnyBitmap FromFile(string file, bool preserveOriginalFormat = true /// Construct a new Bitmap from a Uri /// /// The uri of the image. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. /// /// - /// - /// - public static async Task FromUriAsync(Uri uri, bool preserveOriginalFormat = true) + /// + /// + public static async Task FromUriAsync(Uri uri) + { + try + { + using Stream stream = await LoadUriAsync(uri); + return new AnyBitmap(stream, true); + } + catch (Exception e) + { + throw new NotSupportedException("Error while loading AnyBitmap from Uri", e); + } + } + + /// + /// Construct a new Bitmap from a Uri + /// + /// The uri of the image. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + /// + /// + public static async Task FromUriAsync(Uri uri, bool preserveOriginalFormat) { try { @@ -662,15 +803,13 @@ public static async Task FromUriAsync(Uri uri, bool preserveOriginalF /// Construct a new Bitmap from a Uri /// /// The uri of the image. - /// Determine whether to load as its original pixel format or Rgba32. - /// Default is true. Set to false to load as Rgba32. /// /// - /// + /// #if NET6_0_OR_GREATER [Obsolete("FromUri(Uri) is obsolete for net60 or greater because it uses WebClient which is obsolete. Consider using FromUriAsync(Uri) method.")] #endif - public static AnyBitmap FromUri(Uri uri, bool preserveOriginalFormat = true) + public static AnyBitmap FromUri(Uri uri) { try { @@ -683,6 +822,30 @@ public static AnyBitmap FromUri(Uri uri, bool preserveOriginalFormat = true) } } + /// + /// Construct a new Bitmap from a Uri + /// + /// The uri of the image. + /// Determine whether to load as its original pixel format or Rgba32. + /// + /// + /// +#if NET6_0_OR_GREATER + [Obsolete("FromUri(Uri) is obsolete for net60 or greater because it uses WebClient which is obsolete. Consider using FromUriAsync(Uri) method.")] +#endif + public static AnyBitmap FromUri(Uri uri, bool preserveOriginalFormat) + { + try + { + using WebClient client = new(); + return new AnyBitmap(client.OpenRead(uri), preserveOriginalFormat); + } + catch (Exception e) + { + throw new NotSupportedException("Error while loading AnyBitmap from Uri", e); + } + } + /// /// Creates an AnyBitmap object from a buffer of RGB pixel data. /// diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common/IronSoftware.Drawing.Common.csproj b/IronSoftware.Drawing/IronSoftware.Drawing.Common/IronSoftware.Drawing.Common.csproj index ec774df..ef6c50a 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common/IronSoftware.Drawing.Common.csproj +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common/IronSoftware.Drawing.Common.csproj @@ -35,8 +35,8 @@ - - + + diff --git a/NuGet/IronSoftware.Drawing.nuspec b/NuGet/IronSoftware.Drawing.nuspec index 0946583..7183b4e 100644 --- a/NuGet/IronSoftware.Drawing.nuspec +++ b/NuGet/IronSoftware.Drawing.nuspec @@ -40,7 +40,8 @@ Supports: For general support and technical inquiries, please email us at: support@ironsoftware.com IronSoftware.System.Drawing is an open-source solution for .NET developers to replace System.Drawing.Common with a universal and flexible library. - Updates internal dependencies. - - Disable warning messages from BitMiracle.LibTiff.NET. + - Fixes the issue of some images are auto-rotated when loading as AnyBitmap. + - Adds an option called preserveOriginalFormat to download input image as Rgba32 via AnyBitmap. Copyright © Iron Software 2022-2025 Images, Bitmap, SkiaSharp, SixLabors, BitMiracle, Maui, SVG, TIFF, TIF, GIF, JPEG, PNG, Color, Rectangle, Drawing, C#, VB.NET, ASPX, create, render, generate, standard, netstandard2.0, core, netcore