Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple samples #15

Closed
Hefaistos68 opened this issue Jan 19, 2021 · 2 comments
Closed

Simple samples #15

Hefaistos68 opened this issue Jan 19, 2021 · 2 comments

Comments

@Hefaistos68
Copy link

What about some simple examples that can be used day-by-day, like how to convert from System.Drawing formats to ImageSharp formats and vice versa?
Like how to construct a SixLabors.ImageSharp.Image from System.Drawing.Bitmap in memory?
I am having a hard time using ImageSharp for saving/loading/processing while having to display the results in the System.Drawing space.

@JimBobSquarePants
Copy link
Member

That would be considered an advanced scenario. You can use the WrapMemory APIs to access Bitmap buffers.

@Hefaistos68
Copy link
Author

I have put together a few extension methods to convert to and from Bitmap and Image formats, also for byte[]. Maybe can be added to the samples.

internal static class ImageExtensions
{
	#region Public Methods

	/// <summary>
	/// Extension method that converts a Image to an byte array
	/// </summary>
	/// <param name="imageIn">The Image to convert</param>
	/// <returns>An byte array containing the JPG format Image</returns>
	public static byte[] ToArray(this SixLabors.ImageSharp.Image imageIn)
	{
		using(MemoryStream ms = new MemoryStream())
		{
			imageIn.Save(ms, JpegFormat.Instance);
			return ms.ToArray();
		}
	}

	/// <summary>
	/// Extension method that converts a Image to an byte array
	/// </summary>
	/// <param name="imageIn">The Image to convert</param>
	/// <param name="fmt"></param>
	/// <returns>An byte array containing the JPG format Image</returns>
	public static byte[] ToArray(this SixLabors.ImageSharp.Image imageIn, IImageFormat fmt)
	{
		using(MemoryStream ms = new MemoryStream())
		{
			imageIn.Save(ms, fmt);
			return ms.ToArray();
		}
	}

	/// <summary>
	/// Extension method that converts a Image to an byte array
	/// </summary>
	/// <param name="imageIn">The Image to convert</param>
	/// <returns>An byte array containing the JPG format Image</returns>
	public static byte[] ToArray(this global::System.Drawing.Image imageIn)
	{
		return ToArray(imageIn, ImageFormat.Png);
	}

	/// <summary>
	/// Converts the image data into a byte array.
	/// </summary>
	/// <param name="imageIn">The image to convert to an array</param>
	/// <param name="fmt">The format to save the image in</param>
	/// <returns>An array of bytes</returns>
	public static byte[] ToArray(this global::System.Drawing.Image imageIn, ImageFormat fmt)
	{
		using(MemoryStream ms = new MemoryStream())
		{
			imageIn.Save(ms, fmt);
			return ms.ToArray();
		}
	}

	/// <summary>
	/// Extension method that converts a byte array with JPG data to an Image
	/// </summary>
	/// <param name="byteArrayIn">The byte array with JPG data</param>
	/// <returns>The reconstructed Image</returns>
	public static Image ToImage(this byte[] byteArrayIn)
	{
		using(MemoryStream ms = new MemoryStream(byteArrayIn))
		{
			Image returnImage = Image.Load(ms);
			return returnImage;
		}
	}

	public static global::System.Drawing.Image ToNetImage(this byte[] byteArrayIn)
	{
		using(MemoryStream ms = new MemoryStream(byteArrayIn))
		{
			global::System.Drawing.Image returnImage = global::System.Drawing.Image.FromStream(ms);
			return returnImage;
		}
	}

	#endregion Public Methods
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants