public NBitmap LoadBitmapFromStream(Stream stream) { return(nativeCodec.LoadBitmapFromStream(stream, source => { NBitmap bitmap = new NBitmap(source.Width, source.Height); bitmap.WithPinnedPixels(ptr => source.CopyToBitmap(new Rectangle(0, 0, source.Width, source.Height), ptr, 4 * source.Width)); return bitmap; })); }
public void CopyToBitmap(Point sourceLocation, NBitmap dest, Point destLocation, Size size) { // todo: check other conditions and and test validation if (size.Width <= 0 || size.Height <= 0) { // nothing to copy return; } if (sourceLocation.X < 0 || sourceLocation.Y < 0 || sourceLocation.X + size.Width > Width || sourceLocation.Y + size.Height > Height) { throw new ArgumentException($"Source area is outside of the image: ({new Rectangle(sourceLocation, size)})."); } dest.WithPinnedPixels(destPtr => { int bitmapOffset = (destLocation.Y * dest.Width + destLocation.X) * 4; NativeImage.CopyToBitmap(new Rectangle(sourceLocation, size), destPtr + bitmapOffset, dest.Width * 4); }); }