I'm trying to replace System.Drawing in a project where I use LockBits():
csharp
var data = bitmap.LockBits(
new Rectangle(0, 0, clipWidth, clipHeight),
ImageLockMode.ReadWrite,
bitmap.PixelFormat);
Then I write to that bitmap using a native library. Is there any way to achive this with ImageSharp?
@marcpabst We plan to expose safe, and low level pixel manipulation API-s based on System.Memory types Span<T> and Memory<T>, enabling interop scenarios like yours. See #565 for details.
Meanwhile you can use a temporary API at your own risk. I'm about to deprecate it as soon as the Span<T> / Memory<T> stuff get's into the final code. This API may not be part of our final 1.0 release:
```C#
using SixLabors.ImageSharp.Advanced;
using (Image
{
fixed (Rgba32* pixelPtr = &image.DangerousGetPinnableReferenceToPixelBuffer())
{
// interop code on pixelPtr
}
}
**Note 1:** this code is utilizing [`ref` locals](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/ref-returns)!
**Note 2:** The final `System.Memory` based version would look like this:
```C#
fixed (Rgba32* pixelPtr =&MemoryMarshal.GetReference(image.PixelMemory))
{
// interop code on pixelPtr
}
Hey @marcpabst
Glad we were able to help you. Just a note to say would you please use the Gitter channel for future questions? We're trying to stay on top of the issue tracker here and limiting it to issues only. 馃槈
Thanks!
Thanks for your help! I will absolutly use the Gitter channel in the future!
Most helpful comment
@marcpabst We plan to expose safe, and low level pixel manipulation API-s based on
System.MemorytypesSpan<T>andMemory<T>, enabling interop scenarios like yours. See #565 for details.Meanwhile you can use a temporary API at your own risk. I'm about to deprecate it as soon as the
Span<T>/Memory<T>stuff get's into the final code. This API may not be part of our final1.0release:```C#
using SixLabors.ImageSharp.Advanced;
using (Image image = LoadMyImage())
{
fixed (Rgba32* pixelPtr = &image.DangerousGetPinnableReferenceToPixelBuffer())
{
// interop code on pixelPtr
}
}