SkiaSharp - 生成图片 #10

Closed
opened 2 years ago by Sirius · 0 comments
Sirius commented 2 years ago
Owner

SkiaSharp

对谷歌的Skia图形库的.NET封装。

重要的是它不依赖Windows GDI。

下面代码是在一张A4纸上渲染一些按表格排列文字,并在右下角绘制一个二维码

        // A4 dimensions in pixels at 300 DPI
        const int width = 2480;
        const int height = 3508;

        using var surface = SKSurface.Create(new SKImageInfo(width, height));
        var canvas = surface.Canvas;
        canvas.Clear(SKColors.White);

        // Define the font paint
        var fontPaint = new SKPaint
        {
            Color = SKColors.Black,
            TextSize = 30,
            IsAntialias = true
        };

        // Draw a simple table with 5 rows and 4 columns
        int rows = 5;
        int columns = 4;
        int cellWidth = width / columns;
        int cellHeight = 300; // arbitrary cell height

        // Draw some text inside the table cells
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < columns; col++)
            {
                string text = $"Cell {row + 1},{col + 1}";
                float x = col * cellWidth + 10; // 10 pixels padding
                float y = row * cellHeight + 40; // 40 pixels from top of cell
                canvas.DrawText(text, x, y, fontPaint);
            }
        }

        // Generate a QR code
        string qr_text = "https://bing.com";
        var qr_size = 300;
        using var generator = new QRCodeGenerator();
        var sku = generator.CreateQrCode(qr_text, ECCLevel.H);
        var skImageInfo = new SKImageInfo(qr_size, qr_size);
        using var skSurface = SKSurface.Create(skImageInfo);
        skSurface.Canvas.Render(sku, skImageInfo.Width, skImageInfo.Height);
        using var skImage = skSurface.Snapshot();
        // Draw the QR code in the bottom right corner
        canvas.DrawImage(skImage, width - qr_size - 10, height - qr_size - 10);

        // Save the image
        using var image = surface.Snapshot();
        using var data = image.Encode(SKEncodedImageFormat.Png, 100);
        var base64 = Convert.ToBase64String(data.ToArray());
        return $"data:image/png;base64,{base64}";
# SkiaSharp 对谷歌的Skia图形库的.NET封装。 重要的是它不依赖Windows GDI。 下面代码是在一张A4纸上渲染一些按表格排列文字,并在右下角绘制一个二维码 ```csharp // A4 dimensions in pixels at 300 DPI const int width = 2480; const int height = 3508; using var surface = SKSurface.Create(new SKImageInfo(width, height)); var canvas = surface.Canvas; canvas.Clear(SKColors.White); // Define the font paint var fontPaint = new SKPaint { Color = SKColors.Black, TextSize = 30, IsAntialias = true }; // Draw a simple table with 5 rows and 4 columns int rows = 5; int columns = 4; int cellWidth = width / columns; int cellHeight = 300; // arbitrary cell height // Draw some text inside the table cells for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { string text = $"Cell {row + 1},{col + 1}"; float x = col * cellWidth + 10; // 10 pixels padding float y = row * cellHeight + 40; // 40 pixels from top of cell canvas.DrawText(text, x, y, fontPaint); } } // Generate a QR code string qr_text = "https://bing.com"; var qr_size = 300; using var generator = new QRCodeGenerator(); var sku = generator.CreateQrCode(qr_text, ECCLevel.H); var skImageInfo = new SKImageInfo(qr_size, qr_size); using var skSurface = SKSurface.Create(skImageInfo); skSurface.Canvas.Render(sku, skImageInfo.Width, skImageInfo.Height); using var skImage = skSurface.Snapshot(); // Draw the QR code in the bottom right corner canvas.DrawImage(skImage, width - qr_size - 10, height - qr_size - 10); // Save the image using var image = surface.Snapshot(); using var data = image.Encode(SKEncodedImageFormat.Png, 100); var base64 = Convert.ToBase64String(data.ToArray()); return $"data:image/png;base64,{base64}"; ```
Sirius added the
dotnet
label 2 years ago
Sirius closed this issue 2 years ago
Sirius added the
nuget
label 2 years ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Sirius/note#10
Loading…
There is no content yet.