Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are there any examples that uses SkiaSharp to save and manipulate the PDF image? #81

Open
MarioGK opened this issue Dec 6, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@MarioGK
Copy link

MarioGK commented Dec 6, 2022

I tried to search an example that uses SkiaSharp to manipulate images instead of System.Drawings because i am using linux.

public static (byte[] content, int width, int height) ToImage(this byte[] fileBytes, int page = 0)
    {
        var docReader = DocLib.Instance.GetDocReader(fileBytes,DefaultPageDimensions);
        var pageReader = docReader.GetPageReader(page);
        
        var image = pageReader.GetImage();

        if (image == null)
        {
            throw new Exception("Could not find image in PDF");
        }
        
        var width = pageReader.GetPageWidth();
        var height = pageReader.GetPageHeight();

        return (image, width, height);
    }

    public static SKBitmap ToSkiaImage(this byte[] fileBytes, int page = 0)
    {
        var (imageContent, width, height) = fileBytes.ToImage(page);

        var bitmap = new SKBitmap(width, height);
        
        var array = new byte[bitmap.RowBytes * bitmap.Height];
        for (var i = 0; i < imageContent.Length; i++)
        {
            var color = new SKColor(imageContent[i]);
            var num = i % width;
            var num2 = i / width;
            array[bitmap.RowBytes * num2 + 4 * num] = color.Blue;
            array[bitmap.RowBytes * num2 + 4 * num + 1] = color.Green;
            array[bitmap.RowBytes * num2 + 4 * num + 2] = color.Red;
            array[bitmap.RowBytes * num2 + 4 * num + 3] = color.Alpha;
        }

        var pixels = bitmap.GetPixels();
        Marshal.Copy(array, 0, pixels, array.Length);
        
        return bitmap;
    }

This is as far as i got.

@MarioGK
Copy link
Author

MarioGK commented Dec 6, 2022

    public static SKBitmap ToSkiaImage(this byte[] fileBytes, int page = 0)
    {
        var (imageContent, width, height) = fileBytes.ToImage(page);
        var memoryStream = new MemoryStream(imageContent);
        var skData = SKData.Create(memoryStream);
        var bitmap = SKBitmap.Decode(skData, new SKImageInfo(width, height, SKColorType.Bgra8888));
        
        return bitmap;
    }

I tried with this method as well, and the bitmap return null

@t0ane
Copy link

t0ane commented Jan 24, 2023

Not far from my working solution

        public static SKBitmap ToSkiaImage(byte[] bytes, int width, int height)
        {
            var bmp = new SKBitmap(new SKImageInfo(width, height,SKColorType.Bgra8888));
            
            Marshal.Copy(bytes, 0, bmp.GetPixels(), bytes.Length);

            return bmp;
        }

@Modest-as
Copy link
Member

Triaged and approved, will add an example

@Modest-as Modest-as added the enhancement New feature or request label May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants