Code Monkey home page Code Monkey logo

Comments (2)

Hefaistos68 avatar Hefaistos68 commented on June 13, 2024 1

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
}

from samples.

JimBobSquarePants avatar JimBobSquarePants commented on June 13, 2024

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

from samples.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.