コード例 #1
0
        public static Bgra32ImageBuffer ReadImageBuffer(this OpenSlideImage image, int level, long x, long y, long w, long h)
        {
            var buffer = new Bgra32ImageBuffer(checked ((int)w), checked ((int)h));

            image.ReadRegion(level, x, y, w, h, ref buffer.GetPinnableReference());
            return(buffer);
        }
コード例 #2
0
        private static Image <Bgra32> GenerateThumbnail(OpenSlideImage image, int maxWidth, int maxHeight)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            (long width, long height) = image.Dimensions;

            // 决定合适的level
            double downsampleWidth  = width / (double)maxWidth;
            double downsampleHeight = height / (double)maxHeight;
            double downsample       = Math.Max(downsampleWidth, downsampleHeight);
            int    level            = image.GetBestLevelForDownsample(downsample);

            (long levelWidth, long levelHeight) = image.GetLevelDimensions(level);

            // 计算目标大小
            int targetWidth, targetHeight;

            if (downsampleWidth > downsampleHeight)
            {
                // width缩小较大,将maxWidth作为标准
                targetWidth  = maxWidth;
                targetHeight = (int)(height / downsampleWidth);
            }
            else
            {
                // height缩小较大,将maxHeight作为标准
                targetWidth  = (int)(width / downsampleHeight);
                targetHeight = maxHeight;
            }

            Image <Bgra32> output;

            checked
            {
                output = new Image <Bgra32>((int)levelWidth, (int)levelHeight);
            }
            image.ReadRegion(
                level, 0, 0, levelWidth, levelHeight,
                ref Unsafe.As <Bgra32, byte>(ref MemoryMarshal.GetReference(output.GetPixelSpan())));
            output.Mutate(ctx =>
            {
                ctx.Resize(targetWidth, targetHeight);
            });
            return(output);
        }