Skip to content

Memory Buffer API #9

@SkyeHoefling

Description

@SkyeHoefling

Description

Add memory buffer APIs that can return an array or a stream

New APIs

IImage

byte[] ToArray(int quality = 90)
Stream AsStream(int quality = 90)
ReadOnlySpan<byte> AsReadOnlySpan(int quality = 90)

Usage - ToArray()

byte[] buffer;
using (var image = new HeifImage("MyImage.heic"))
using (var primary = image.PrimaryImage())
{
  buffer = primary.ToArray();
}

// do something with buffer

Usage - AsStream

Stream stream;
using (var image = new Heifimage("MyImage.heic"))
using (var primary = image.PrimaryImage())
{
  stream = primary.AsStream();
}

// do something with stream

// don't forget to dispose of your stream
stream?.Dispose();

Usage - AsReadOnlySpan

This one is a little tricky as Span<T> gives us access to native memory. If we allow C# to operate on native memory we should ensure it happens within the IDisposable using block otherwise there will be a memory leak.

ReadOnlySpan<byte> span;
using (var image = new HeifImage("MyImage.heic"))
using (var primary = image.PrimaryImage())
{
  ReadOnlySpan<byte> span = primary.AsReadOnlySpan();

  // do something with span
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions