Skip to content

Commit

Permalink
add: 生成格子图的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuuu committed Nov 13, 2024
1 parent fb88a52 commit d6c0dcd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
68 changes: 68 additions & 0 deletions Image2Display/Image2Display/Helpers/FontConvert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Image2Display.Models;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Image2Display.Helpers
{
public class FontConvert
{
/// <summary>
/// 获取格子像素图片
/// </summary>
/// <param name="data">每个点的数据</param>
/// <param name="width">像素高度</param>
/// <param name="height">像素宽度</param>
/// <param name="image">图片缓冲区,没给的话会新建一个</param>
/// <returns>图片缓冲</returns>
public static ImageData GetImage(byte[] data, int width, int height,ImageData? image = null)
{
//像素尺寸
var pixelSize = 8;
//间隔尺寸
var padding = 1;
//间隔颜色
var paddingColor = new Rgba32(45, 25, 70, 255);

//计算一下实际尺寸
var realWidth = width * pixelSize + (width - 1) * padding;
var realHeight = height * pixelSize + (height - 1) * padding;
//保证缓冲区存在
image ??= new ImageData(realWidth, realHeight);
//如果尺寸不一致,改一下尺寸
if (image.Width != realWidth || image.Height != realHeight)
{
image.Reset(realWidth, realHeight);
}
//背景色黑色
image.DrawRect(0, 0, realWidth, realHeight, new Rgba32(0, 0, 0, 255));

//画横向的格子线
for (var i = 0; i < width - 1; i++)
{
image.DrawRect((i * (pixelSize + padding)) + pixelSize, 0, padding, realHeight, paddingColor);
}
//画纵向的格子线
for (var i = 0; i < height - 1; i++)
{
image.DrawRect(0, (i * (pixelSize + padding)) + pixelSize, realWidth, padding, paddingColor);
}

//画像素
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var index = y * width + x;
var color = new Rgba32(data[index], data[index], data[index], 255);
image.DrawRect(x * (pixelSize + padding), y * (pixelSize + padding), pixelSize, pixelSize, color);
}
}

return image;
}
}
}
19 changes: 19 additions & 0 deletions Image2Display/Image2Display/Models/ImageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ public ImageData(ImageData img)
Raw = Image.Load<Rgba32>(stream);
}

public bool Reset(int width, int height)
{
Raw?.Dispose();
Raw = new Image<Rgba32>(width, height);
return true;
}

/// <summary>
/// 画一个矩形
/// </summary>
public bool DrawRect(int x, int y, int width, int height, Rgba32 color)
{
if (x < 0 || y < 0 || width < 0 || height < 0)
return false;
if (x + width > Raw.Width || y + height > Raw.Height)
return false;
Raw.Mutate(ctx => ctx.Fill(color, new Rectangle(x, y, width, height)));
return true;
}

/// <summary>
/// 裁剪图片区域
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Image2Display.Helpers;
using Image2Display.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -96,11 +97,12 @@ public partial class FontConvertViewModel : ViewModelBase
[ObservableProperty]
private string _PreviewText = Utils.GetI18n<string>("PreviewText");

private ImageData? ImageCache = null;

public FontConvertViewModel()
{
//TODO 初始化图片成格子图
var pic = new ImageData(100,100);
pic.AddBackGround(100,100,new SixLabors.ImageSharp.PixelFormats.Rgba32(0,0,0));
//初始化图片成格子图
var pic = FontConvert.GetImage(new byte[100], 10, 10, ImageCache);
OriginalImage = new Bitmap(pic.GetStream());

//用skia接口获取系统字体列表
Expand Down
2 changes: 1 addition & 1 deletion Image2Display/Image2Display/Views/FontConvertView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Margin="10"
BorderBrush="{DynamicResource SurfaceStrokeColorDefaultBrush}"
BorderThickness="2">
<Image Source="{Binding OriginalImage}" />
<Image RenderOptions.BitmapInterpolationMode="None" Source="{Binding OriginalImage}" />
</Border>
</Panel>
<!-- 切换预览字符按钮 -->
Expand Down

0 comments on commit d6c0dcd

Please sign in to comment.