naps2/NAPS2.Images.Gdi/GdiExtensions.cs
Ben Olden-Cooligan d8ee7621cb Use IDisposable for IMemoryImage locking instead of manual unlock
This makes things easier now that "using var" is a thing.
2022-07-01 12:39:26 -07:00

60 lines
1.8 KiB
C#

using System.Drawing;
using System.Drawing.Imaging;
namespace NAPS2.Images.Gdi;
public static class GdiExtensions
{
public static Bitmap AsBitmap(this IMemoryImage image)
{
var gdiImage = image as GdiImage ?? throw new ArgumentException("Expected a GdiImage", nameof(image));
return gdiImage.Bitmap;
}
public static ImageFormat AsImageFormat(this ImageFileFormat imageFileFormat)
{
switch (imageFileFormat)
{
case ImageFileFormat.Bmp:
return ImageFormat.Bmp;
case ImageFileFormat.Jpeg:
return ImageFormat.Jpeg;
case ImageFileFormat.Png:
return ImageFormat.Png;
}
throw new ArgumentException("Unsupported image format", nameof(imageFileFormat));
}
public static ImageFileFormat AsImageFileFormat(this ImageFormat imageFormat)
{
if (Equals(imageFormat, ImageFormat.Bmp))
{
return ImageFileFormat.Bmp;
}
if (Equals(imageFormat, ImageFormat.Jpeg))
{
return ImageFileFormat.Jpeg;
}
if (Equals(imageFormat, ImageFormat.Png))
{
return ImageFileFormat.Png;
}
return ImageFileFormat.Unspecified;
}
public static PixelFormat AsPixelFormat(this ImagePixelFormat pixelFormat)
{
switch (pixelFormat)
{
case ImagePixelFormat.BW1:
// TODO: Maybe it makes sense to have WB1 format too
return PixelFormat.Format1bppIndexed;
case ImagePixelFormat.RGB24:
return PixelFormat.Format24bppRgb;
case ImagePixelFormat.ARGB32:
return PixelFormat.Format32bppArgb;
}
throw new ArgumentException("Unsupported pixel format: " + pixelFormat);
}
}