diff --git a/cYo.Common/Drawing/BitmapExtensions.cs b/cYo.Common/Drawing/BitmapExtensions.cs index de71fc2..878942b 100644 --- a/cYo.Common/Drawing/BitmapExtensions.cs +++ b/cYo.Common/Drawing/BitmapExtensions.cs @@ -83,9 +83,10 @@ private static Bitmap MagicScalerResampler(Bitmap bmp, Size size, BitmapResampli //var IsAlpha = ((ImageFlags)bmp.Flags & ImageFlags.HasAlpha) != 0; var IsAlpha = bmp.HasTransparency(); - byte[] imgArray = IsAlpha ? bmp.ImageToBytes(ImageFormat.Png) : bmp.ImageToJpegBytes(); + byte[] imgArray = IsAlpha ? bmp.ImageToBytes(ImageFormat.Png) : bmp.ImageToJpegBytes(); + //IPixelSource imgArray = new CustomPixelSource(bmp, format); - Task.Run(() => MagicImageProcessor.ProcessImage(imgArray, ms, ps)).Wait(); + Task.Run(() => MagicImageProcessor.ProcessImage(imgArray, ms, ps)).Wait(); bmp?.Dispose(); return Image.FromStream(ms) as Bitmap; diff --git a/cYo.Common/Drawing/CustomPixelSource.cs b/cYo.Common/Drawing/CustomPixelSource.cs new file mode 100644 index 0000000..b3d8465 --- /dev/null +++ b/cYo.Common/Drawing/CustomPixelSource.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; +using System.IO; +using System.Linq; +using System.Runtime; +using System.Threading; +using System.Threading.Tasks; +using PhotoSauce.MagicScaler; + +namespace cYo.Common.Drawing +{ + public class CustomPixelSource : BitmapPixelSource + { + private readonly Bitmap _bitmap; + private readonly BitmapData _bitmapData; + private readonly int dataLength; + private byte[] byteArray = null; + + public CustomPixelSource(Bitmap bitmap, PixelFormat format = PixelFormat.Format32bppArgb) + : base(ConvertPixelFormat(format), bitmap.Width, bitmap.Height, GetStride(bitmap)) + { + _bitmap = bitmap; + var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); + _bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat); + dataLength = Math.Abs(base.Stride) * bitmap.Height; + } + + private static Guid ConvertPixelFormat(PixelFormat format) + { + switch (format) + { + case PixelFormat.Format24bppRgb: + return PixelFormats.Bgr24bpp; + case PixelFormat.Format8bppIndexed: + return PixelFormats.Grey8bpp; + default: + return PixelFormats.Bgra32bpp; + } + } + + private static int GetStride(Bitmap bitmap) + { + var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); + BitmapData _bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat); + + try + { + return _bitmapData.Stride; + } + finally + { + bitmap.UnlockBits(_bitmapData); + } + } + + private ReadOnlySpan GetSpan() + { + if (byteArray == null) + { + // Store bitmap data inside byte array + byteArray = new byte[dataLength]; + + // Copy the bitmap data to the byte array. + System.Runtime.InteropServices.Marshal.Copy(_bitmapData.Scan0, byteArray, 0, dataLength); + + // Return a ReadOnlySpan from the byte array. + return new ReadOnlySpan(byteArray); + } + return byteArray; + } + + protected override ReadOnlySpan Span => GetSpan(); + } +} + diff --git a/cYo.Common/cYo.Common.csproj b/cYo.Common/cYo.Common.csproj index ef5b79c..200cd14 100644 --- a/cYo.Common/cYo.Common.csproj +++ b/cYo.Common/cYo.Common.csproj @@ -168,6 +168,7 @@ +