public Size MagicScalerResize()
        {
            // stride is width * bytes per pixel (3 for BGR), rounded up to the nearest multiple of 4
            const uint stride  = ResizedWidth * 3u + 3u & ~3u;
            const uint bufflen = ResizedHeight * stride;

            var settings = new ProcessImageSettings
            {
                Width   = ResizedWidth,
                Height  = ResizedHeight,
                Sharpen = false
            };

            using (var pixels = new TestPatternPixelSource(Width, Height, PixelFormats.Bgr24bpp))
                using (var pipeline = MagicImageProcessor.BuildPipeline(pixels, settings))
                {
                    var rect   = new Rectangle(0, 0, ResizedWidth, ResizedHeight);
                    var buffer = ArrayPool <byte> .Shared.Rent((int)bufflen);

                    pipeline.PixelSource.CopyPixels(rect, (int)stride, new Span <byte>(buffer, 0, (int)bufflen));
                    ArrayPool <byte> .Shared.Return(buffer);

                    return(rect.Size);
                }
        }
Esempio n. 2
0
        public void MagicScalerResize()
        {
            // stride is width * bytes per pixel (3 for BGR), rounded up to the nearest multiple of 4
            const uint stride  = ResizedWidth * 3u + 3u & ~3u;
            const uint bufflen = ResizedHeight * stride;

            var pixels   = new TestPatternPixelSource(Width, Height, PixelFormats.Bgr24bpp);
            var settings = new ProcessImageSettings
            {
                Width  = ResizedWidth,
                Height = ResizedHeight
            };

            using (var pipeline = MagicImageProcessor.BuildPipeline(pixels, settings))
            {
                var rect   = new System.Drawing.Rectangle(0, 0, ResizedWidth, ResizedHeight);
                var buffer = Marshal.AllocHGlobal((int)bufflen);
                pipeline.PixelSource.CopyPixels(rect, stride, bufflen, buffer);
                Marshal.FreeHGlobal(buffer);
            }
        }
Esempio n. 3
0
        private static byte[] DecodeImage(Stream input, Size areaSize, int orientation)
        {
            // decode the image
            var processedData = new MemoryStream();

            using (var pipeline = MagicImageProcessor.BuildPipeline(input, new ProcessImageSettings
            {
                Width = areaSize.Width,
                Height = areaSize.Height,
                ResizeMode = CropScaleMode.Max,
                Interpolation = InterpolationSettings.Lanczos,
                SaveFormat = FileFormat.Bmp,
                ColorProfileMode = ColorProfileMode.Normalize,
                OrientationMode = OrientationMode.Ignore // we will use custom transform
            }))
            {
                pipeline.AddTransform(new FormatConversionTransform(PixelFormats.Bgra32bpp));
                pipeline.AddTransform(new OrientationTransform((Orientation)orientation));
                pipeline.ExecutePipeline(processedData);
            }

            return(processedData.ToArray());
        }
Esempio n. 4
0
        public FileInfo Rotate(FileInfo toRotate, Orientation orientation)
        {
            if (!toRotate.Exists)
            {
                return(null);
            }

            var newFile = Path.Combine(toRotate.Directory?.FullName ?? string.Empty, $"{Guid.NewGuid()}.jpg");

            var newFileInfo = new FileInfo(newFile);

            if (newFileInfo.Exists)
            {
                newFileInfo.Delete();
            }

            using var pl =
                      MagicImageProcessor.BuildPipeline(toRotate.FullNameWithLongFilePrefix(), new ProcessImageSettings());
            pl.AddTransform(new OrientationTransform(orientation));
            using var outStream = new FileStream(newFileInfo.FullName, FileMode.Create);

            pl.WriteOutput(outStream);

            pl.Dispose();
            outStream.Dispose();

            var finalFileName = toRotate.FullName;

            toRotate.Delete();

            FileManagement.MoveFileAndLog(newFileInfo.FullName, finalFileName);

            newFileInfo = new FileInfo(finalFileName);

            return(newFileInfo);
        }