Comments (2)
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.
That would be considered an advanced scenario. You can use the WrapMemory APIs to access Bitmap buffers.
from samples.
Related Issues (10)
- Gif manipulation sample HOT 2
- AvatarWithRoundedCorner not transparent HOT 3
- The magic of IPathCollection HOT 1
- TextGraphicsOptions HOT 1
- Crop image sample HOT 1
- Fonts package referenced in sample projects is outdated HOT 4
- Compilation error SixLabors.ImageSharp
- Compilation error
- Drawing text sample outdated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from samples.