mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using NAPS2.Images.Bitwise;
|
|
|
|
namespace NAPS2.Images.Gdi;
|
|
|
|
#if NET6_0_OR_GREATER
|
|
[System.Runtime.Versioning.SupportedOSPlatform("windows7.0")]
|
|
#endif
|
|
public class GdiImageLockState : ImageLockState
|
|
{
|
|
public static GdiImageLockState Create(Bitmap bitmap, LockMode lockMode, out BitwiseImageData imageData)
|
|
{
|
|
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), lockMode.AsImageLockMode(),
|
|
bitmap.PixelFormat);
|
|
var subPixelType = bitmap.PixelFormat switch
|
|
{
|
|
PixelFormat.Format32bppArgb => SubPixelType.Bgra,
|
|
PixelFormat.Format24bppRgb => SubPixelType.Bgr,
|
|
PixelFormat.Format8bppIndexed => SubPixelType.Gray,
|
|
PixelFormat.Format1bppIndexed => SubPixelType.Bit,
|
|
_ => throw new InvalidOperationException("Unsupported pixel format")
|
|
};
|
|
imageData = new BitwiseImageData(bitmapData.Scan0,
|
|
new PixelInfo(bitmap.Width, bitmap.Height, subPixelType, bitmapData.Stride));
|
|
return new GdiImageLockState(bitmap, bitmapData);
|
|
}
|
|
|
|
private readonly Bitmap _bitmap;
|
|
private readonly BitmapData _bitmapData;
|
|
private bool _disposed;
|
|
|
|
private GdiImageLockState(Bitmap bitmap, BitmapData bitmapData)
|
|
{
|
|
_bitmap = bitmap;
|
|
_bitmapData = bitmapData;
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
lock (this)
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
}
|
|
_bitmap.UnlockBits(_bitmapData);
|
|
}
|
|
} |