Skip to content

Commit 0cf5bf4

Browse files
Overloads AnyBitmap constructors and From methods
Provides overloaded constructors and From methods for AnyBitmap, allowing to omit the preserveOriginalFormat parameter. The default value for preserveOriginalFormat is true. This change maintains backwards compatibility while simplifying the API for common use cases.
1 parent d84fd0c commit 0cf5bf4

1 file changed

Lines changed: 208 additions & 45 deletions

File tree

  • IronSoftware.Drawing/IronSoftware.Drawing.Common

IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs

Lines changed: 208 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,17 @@ public T ToBitmap<T>()
463463
/// Create a new Bitmap from a a Byte Span.
464464
/// </summary>
465465
/// <param name="span">A Byte Span of image data in any common format.</param>
466-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
467-
/// Default is true. Set to false to load as Rgba32.</param>
468-
public static AnyBitmap FromSpan(ReadOnlySpan<byte> span, bool preserveOriginalFormat = true)
466+
public static AnyBitmap FromSpan(ReadOnlySpan<byte> span)
467+
{
468+
return new AnyBitmap(span, true);
469+
}
470+
471+
/// <summary>
472+
/// Create a new Bitmap from a a Byte Span.
473+
/// </summary>
474+
/// <param name="span">A Byte Span of image data in any common format.</param>
475+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
476+
public static AnyBitmap FromSpan(ReadOnlySpan<byte> span, bool preserveOriginalFormat)
469477
{
470478
return new AnyBitmap(span, preserveOriginalFormat);
471479
}
@@ -474,22 +482,41 @@ public static AnyBitmap FromSpan(ReadOnlySpan<byte> span, bool preserveOriginalF
474482
/// Create a new Bitmap from a a Byte Array.
475483
/// </summary>
476484
/// <param name="bytes">A ByteArray of image data in any common format.</param>
477-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
478-
/// Default is true. Set to false to load as Rgba32.</param>
479-
public static AnyBitmap FromBytes(byte[] bytes, bool preserveOriginalFormat = true)
485+
public static AnyBitmap FromBytes(byte[] bytes)
486+
{
487+
return new AnyBitmap(bytes, true);
488+
}
489+
490+
/// <summary>
491+
/// Create a new Bitmap from a a Byte Array.
492+
/// </summary>
493+
/// <param name="bytes">A ByteArray of image data in any common format.</param>
494+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
495+
public static AnyBitmap FromBytes(byte[] bytes, bool preserveOriginalFormat)
480496
{
481497
return new AnyBitmap(bytes, preserveOriginalFormat);
482498
}
483499

484500
/// <summary>
485501
/// Create a new Bitmap from a <see cref="Stream"/> (bytes).
486502
/// </summary>
487-
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
488-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
503+
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
489504
/// Default is true. Set to false to load as Rgba32.</param>
490505
/// <seealso cref="FromStream(Stream, bool)"/>
491506
/// <seealso cref="AnyBitmap"/>
492-
public static AnyBitmap FromStream(MemoryStream stream, bool preserveOriginalFormat = true)
507+
public static AnyBitmap FromStream(MemoryStream stream)
508+
{
509+
return new AnyBitmap(stream, true);
510+
}
511+
512+
/// <summary>
513+
/// Create a new Bitmap from a <see cref="Stream"/> (bytes).
514+
/// </summary>
515+
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
516+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
517+
/// <seealso cref="FromStream(Stream, bool)"/>
518+
/// <seealso cref="AnyBitmap"/>
519+
public static AnyBitmap FromStream(MemoryStream stream, bool preserveOriginalFormat)
493520
{
494521
return new AnyBitmap(stream, preserveOriginalFormat);
495522
}
@@ -498,11 +525,21 @@ public static AnyBitmap FromStream(MemoryStream stream, bool preserveOriginalFor
498525
/// Create a new Bitmap from a <see cref="Stream"/> (bytes).
499526
/// </summary>
500527
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
501-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
502-
/// Default is true. Set to false to load as Rgba32.</param>
503528
/// <seealso cref="FromStream(MemoryStream, bool)"/>
504529
/// <seealso cref="AnyBitmap"/>
505-
public static AnyBitmap FromStream(Stream stream, bool preserveOriginalFormat = true)
530+
public static AnyBitmap FromStream(Stream stream)
531+
{
532+
return new AnyBitmap(stream, true);
533+
}
534+
535+
/// <summary>
536+
/// Create a new Bitmap from a <see cref="Stream"/> (bytes).
537+
/// </summary>
538+
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
539+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
540+
/// <seealso cref="FromStream(MemoryStream, bool)"/>
541+
/// <seealso cref="AnyBitmap"/>
542+
public static AnyBitmap FromStream(Stream stream, bool preserveOriginalFormat)
506543
{
507544
return new AnyBitmap(stream, preserveOriginalFormat);
508545
}
@@ -511,10 +548,19 @@ public static AnyBitmap FromStream(Stream stream, bool preserveOriginalFormat =
511548
/// Construct a new Bitmap from binary data (byte span).
512549
/// </summary>
513550
/// <param name="span">A byte span of image data in any common format.</param>
514-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
515-
/// Default is true. Set to false to load as Rgba32.</param>
516551
/// <seealso cref="AnyBitmap"/>
517-
public AnyBitmap(ReadOnlySpan<byte> span, bool preserveOriginalFormat = true)
552+
public AnyBitmap(ReadOnlySpan<byte> span)
553+
{
554+
LoadImage(span, true);
555+
}
556+
557+
/// <summary>
558+
/// Construct a new Bitmap from binary data (byte span).
559+
/// </summary>
560+
/// <param name="span">A byte span of image data in any common format.</param>
561+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
562+
/// <seealso cref="AnyBitmap"/>
563+
public AnyBitmap(ReadOnlySpan<byte> span, bool preserveOriginalFormat)
518564
{
519565
LoadImage(span, preserveOriginalFormat);
520566
}
@@ -523,11 +569,21 @@ public AnyBitmap(ReadOnlySpan<byte> span, bool preserveOriginalFormat = true)
523569
/// Construct a new Bitmap from binary data (bytes).
524570
/// </summary>
525571
/// <param name="bytes">A ByteArray of image data in any common format.</param>
526-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
527-
/// Default is true. Set to false to load as Rgba32.</param>
528-
/// <seealso cref="FromBytes"/>
572+
/// <seealso cref="FromBytes(byte[])"/>
573+
/// <seealso cref="AnyBitmap"/>
574+
public AnyBitmap(byte[] bytes)
575+
{
576+
LoadImage(bytes, true);
577+
}
578+
579+
/// <summary>
580+
/// Construct a new Bitmap from binary data (bytes).
581+
/// </summary>
582+
/// <param name="bytes">A ByteArray of image data in any common format.</param>
583+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
584+
/// <seealso cref="FromBytes(byte[], bool)"/>
529585
/// <seealso cref="AnyBitmap"/>
530-
public AnyBitmap(byte[] bytes, bool preserveOriginalFormat = true)
586+
public AnyBitmap(byte[] bytes, bool preserveOriginalFormat)
531587
{
532588
LoadImage(bytes, preserveOriginalFormat);
533589
}
@@ -536,8 +592,18 @@ public AnyBitmap(byte[] bytes, bool preserveOriginalFormat = true)
536592
/// Construct a new Bitmap from a <see cref="Stream"/> (bytes).
537593
/// </summary>
538594
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
539-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
540-
/// Default is true. Set to false to load as Rgba32.</param>
595+
/// <seealso cref="FromStream(Stream, bool)"/>
596+
/// <seealso cref="AnyBitmap"/>
597+
public AnyBitmap(MemoryStream stream)
598+
{
599+
LoadImage(stream.ToArray(), true);
600+
}
601+
602+
/// <summary>
603+
/// Construct a new Bitmap from a <see cref="Stream"/> (bytes).
604+
/// </summary>
605+
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
606+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
541607
/// <seealso cref="FromStream(Stream, bool)"/>
542608
/// <seealso cref="AnyBitmap"/>
543609
public AnyBitmap(MemoryStream stream, bool preserveOriginalFormat = true)
@@ -549,11 +615,21 @@ public AnyBitmap(MemoryStream stream, bool preserveOriginalFormat = true)
549615
/// Construct a new Bitmap from a <see cref="Stream"/> (bytes).
550616
/// </summary>
551617
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
552-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
553-
/// Default is true. Set to false to load as Rgba32.</param>
554618
/// <seealso cref="FromStream(MemoryStream, bool)"/>
555619
/// <seealso cref="AnyBitmap"/>
556-
public AnyBitmap(Stream stream, bool preserveOriginalFormat = true)
620+
public AnyBitmap(Stream stream)
621+
{
622+
LoadImage(stream, true);
623+
}
624+
625+
/// <summary>
626+
/// Construct a new Bitmap from a <see cref="Stream"/> (bytes).
627+
/// </summary>
628+
/// <param name="stream">A <see cref="Stream"/> of image data in any common format.</param>
629+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
630+
/// <seealso cref="FromStream(MemoryStream, bool)"/>
631+
/// <seealso cref="AnyBitmap"/>
632+
public AnyBitmap(Stream stream, bool preserveOriginalFormat)
557633
{
558634
LoadImage(stream, preserveOriginalFormat);
559635
}
@@ -574,11 +650,21 @@ public AnyBitmap(AnyBitmap original, int width, int height)
574650
/// Construct a new Bitmap from a file.
575651
/// </summary>
576652
/// <param name="file">A fully qualified file path./</param>
577-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
578-
/// Default is true. Set to false to load as Rgba32.</param>
579-
/// <seealso cref="FromFile"/>
653+
/// <seealso cref="FromFile(string)"/>
580654
/// <seealso cref="AnyBitmap"/>
581-
public AnyBitmap(string file, bool preserveOriginalFormat = true)
655+
public AnyBitmap(string file)
656+
{
657+
LoadImage(File.ReadAllBytes(file), true);
658+
}
659+
660+
/// <summary>
661+
/// Construct a new Bitmap from a file.
662+
/// </summary>
663+
/// <param name="file">A fully qualified file path./</param>
664+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
665+
/// <seealso cref="FromFile(string, bool)"/>
666+
/// <seealso cref="AnyBitmap"/>
667+
public AnyBitmap(string file, bool preserveOriginalFormat)
582668
{
583669
LoadImage(File.ReadAllBytes(file), preserveOriginalFormat);
584670
}
@@ -587,11 +673,29 @@ public AnyBitmap(string file, bool preserveOriginalFormat = true)
587673
/// Construct a new Bitmap from a Uri
588674
/// </summary>
589675
/// <param name="uri">The uri of the image.</param>
590-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
591-
/// Default is true. Set to false to load as Rgba32.</param>
592-
/// <seealso cref="FromUriAsync"/>
676+
/// <seealso cref="FromUriAsync(Uri)"/>
593677
/// <seealso cref="AnyBitmap"/>
594-
public AnyBitmap(Uri uri, bool preserveOriginalFormat = true)
678+
public AnyBitmap(Uri uri)
679+
{
680+
try
681+
{
682+
using Stream stream = LoadUriAsync(uri).GetAwaiter().GetResult();
683+
LoadImage(stream, true);
684+
}
685+
catch (Exception e)
686+
{
687+
throw new NotSupportedException("Error while loading AnyBitmap from Uri", e);
688+
}
689+
}
690+
691+
/// <summary>
692+
/// Construct a new Bitmap from a Uri
693+
/// </summary>
694+
/// <param name="uri">The uri of the image.</param>
695+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
696+
/// <seealso cref="FromUriAsync(Uri, bool)"/>
697+
/// <seealso cref="AnyBitmap"/>
698+
public AnyBitmap(Uri uri, bool preserveOriginalFormat)
595699
{
596700
try
597701
{
@@ -619,11 +723,28 @@ public AnyBitmap(int width, int height, Color backgroundColor = null)
619723
/// Create a new Bitmap from a file.
620724
/// </summary>
621725
/// <param name="file">A fully qualified file path.</param>
622-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
623-
/// Default is true. Set to false to load as Rgba32.</param>
624-
/// <seealso cref="FromFile"/>
726+
/// <seealso cref="FromFile(string)"/>
625727
/// <seealso cref="AnyBitmap"/>
626-
public static AnyBitmap FromFile(string file, bool preserveOriginalFormat = true)
728+
public static AnyBitmap FromFile(string file)
729+
{
730+
if (file.ToLower().EndsWith(".svg"))
731+
{
732+
return LoadSVGImage(file, true);
733+
}
734+
else
735+
{
736+
return new AnyBitmap(file, true);
737+
}
738+
}
739+
740+
/// <summary>
741+
/// Create a new Bitmap from a file.
742+
/// </summary>
743+
/// <param name="file">A fully qualified file path.</param>
744+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
745+
/// <seealso cref="FromFile(string, bool)"/>
746+
/// <seealso cref="AnyBitmap"/>
747+
public static AnyBitmap FromFile(string file, bool preserveOriginalFormat)
627748
{
628749
if (file.ToLower().EndsWith(".svg"))
629750
{
@@ -639,13 +760,33 @@ public static AnyBitmap FromFile(string file, bool preserveOriginalFormat = true
639760
/// Construct a new Bitmap from a Uri
640761
/// </summary>
641762
/// <param name="uri">The uri of the image.</param>
642-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
643-
/// Default is true. Set to false to load as Rgba32.</param>
644763
/// <returns></returns>
645764
/// <seealso cref="AnyBitmap"/>
646-
/// <seealso cref="FromUri"/>
647-
/// <seealso cref="FromUriAsync"/>
648-
public static async Task<AnyBitmap> FromUriAsync(Uri uri, bool preserveOriginalFormat = true)
765+
/// <seealso cref="FromUri(Uri)"/>
766+
/// <seealso cref="FromUriAsync(Uri)"/>
767+
public static async Task<AnyBitmap> FromUriAsync(Uri uri)
768+
{
769+
try
770+
{
771+
using Stream stream = await LoadUriAsync(uri);
772+
return new AnyBitmap(stream, true);
773+
}
774+
catch (Exception e)
775+
{
776+
throw new NotSupportedException("Error while loading AnyBitmap from Uri", e);
777+
}
778+
}
779+
780+
/// <summary>
781+
/// Construct a new Bitmap from a Uri
782+
/// </summary>
783+
/// <param name="uri">The uri of the image.</param>
784+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
785+
/// <returns></returns>
786+
/// <seealso cref="AnyBitmap"/>
787+
/// <seealso cref="FromUri(Uri, bool)"/>
788+
/// <seealso cref="FromUriAsync(Uri, bool)"/>
789+
public static async Task<AnyBitmap> FromUriAsync(Uri uri, bool preserveOriginalFormat)
649790
{
650791
try
651792
{
@@ -662,15 +803,13 @@ public static async Task<AnyBitmap> FromUriAsync(Uri uri, bool preserveOriginalF
662803
/// Construct a new Bitmap from a Uri
663804
/// </summary>
664805
/// <param name="uri">The uri of the image.</param>
665-
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.
666-
/// Default is true. Set to false to load as Rgba32.</param>
667806
/// <returns></returns>
668807
/// <seealso cref="AnyBitmap"/>
669-
/// <seealso cref="FromUriAsync"/>
808+
/// <seealso cref="FromUriAsync(Uri)"/>
670809
#if NET6_0_OR_GREATER
671810
[Obsolete("FromUri(Uri) is obsolete for net60 or greater because it uses WebClient which is obsolete. Consider using FromUriAsync(Uri) method.")]
672811
#endif
673-
public static AnyBitmap FromUri(Uri uri, bool preserveOriginalFormat = true)
812+
public static AnyBitmap FromUri(Uri uri)
674813
{
675814
try
676815
{
@@ -683,6 +822,30 @@ public static AnyBitmap FromUri(Uri uri, bool preserveOriginalFormat = true)
683822
}
684823
}
685824

825+
/// <summary>
826+
/// Construct a new Bitmap from a Uri
827+
/// </summary>
828+
/// <param name="uri">The uri of the image.</param>
829+
/// <param name="preserveOriginalFormat">Determine whether to load <see cref="SixLabors.ImageSharp.Image"/> as its original pixel format or Rgba32.</param>
830+
/// <returns></returns>
831+
/// <seealso cref="AnyBitmap"/>
832+
/// <seealso cref="FromUriAsync(Uri, bool)"/>
833+
#if NET6_0_OR_GREATER
834+
[Obsolete("FromUri(Uri) is obsolete for net60 or greater because it uses WebClient which is obsolete. Consider using FromUriAsync(Uri) method.")]
835+
#endif
836+
public static AnyBitmap FromUri(Uri uri, bool preserveOriginalFormat)
837+
{
838+
try
839+
{
840+
using WebClient client = new();
841+
return new AnyBitmap(client.OpenRead(uri), preserveOriginalFormat);
842+
}
843+
catch (Exception e)
844+
{
845+
throw new NotSupportedException("Error while loading AnyBitmap from Uri", e);
846+
}
847+
}
848+
686849
/// <summary>
687850
/// Creates an AnyBitmap object from a buffer of RGB pixel data.
688851
/// </summary>

0 commit comments

Comments
 (0)