Ejemplo n.º 1
0
    private static void DrawAttachedPicture(ThumbnailsRenderContext ctx, MediaStream attachedPicStream)
    {
        using var attachedPicture = attachedPicStream.ReadAttachedPicture();
        var attachedPictureVipsImage = Image.ThumbnailStream(
            attachedPicture,
            (int)(ThumbnailUtils.DefaultMaxWidth * ctx.Density),
            height: (int)(ThumbnailUtils.DefaultMaxHeight * ctx.Density),
            noRotate: false);

        attachedPictureVipsImage = attachedPictureVipsImage.Colourspace(Enums.Interpretation.Srgb).Cast(Enums.BandFormat.Uchar);
        if (!attachedPictureVipsImage.HasAlpha())
        {
            attachedPictureVipsImage = attachedPictureVipsImage.Bandjoin(255);
        }

        var imageWidth  = attachedPictureVipsImage.Width;
        var imageHeight = attachedPictureVipsImage.Height;

        var sourceImageDataPtr = attachedPictureVipsImage.WriteToMemory(out _);

        attachedPictureVipsImage.Close();

        try
        {
            using var colorspace = SKColorSpace.CreateSrgb();
            var sourceImageInfo = new SKImageInfo(
                imageWidth,
                imageHeight,
                SKColorType.Rgba8888,
                SKAlphaType.Unpremul,
                colorspace);

            using var image =
                      SKImage.FromPixels(sourceImageInfo, sourceImageDataPtr, sourceImageInfo.RowBytes);
            _cachedDecorationImage ??= SKImage.FromEncodedData(ReadDecorationImage());
            ThumbnailUtils.DrawShadowView(
                ctx,
                new SkImageView(image),
                _cachedDecorationImage,
                new SKColor(0, 0, 0),
                minSize: new SKSize(24, 24));
        }
        finally
        {
            NetVips.NetVips.Free(sourceImageDataPtr);
        }
    }