Skip to content

Commit

Permalink
add: 图片数据类型
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuuu committed Jul 31, 2023
1 parent f553a8b commit fadef6d
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Image2Display/I2D_Test/ImageDataTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Image2Display.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace I2D_Test
{
public class ImageDataTest
{
[Theory]
[InlineData(1, 1)]
[InlineData(100, 100)]
[InlineData(1000, 20)]
public void NewSize(int w,int h)
{
_ = new ImageData(w, h);
}

[Theory]
[InlineData(1, 1,10,10)]
[InlineData(100, 100, 10, 10)]
public void CropOk(int x, int y,int w, int h)
{
var img = new ImageData(1000, 1000);
Assert.True(img.Crop(x, y, w, h));
}
[Theory]
[InlineData(100, 100, 10, 10000)]
[InlineData(1000, 20, 10, 10)]
public void CropFail(int x, int y, int w, int h)
{
var img = new ImageData(1000, 1000);
Assert.False(img.Crop(x, y, w, h));
}

[Theory]
[InlineData(1, 1)]
[InlineData(100, 2000)]
[InlineData(1000, 20)]
public void Resize(int w, int h)
{
var img = new ImageData(1000, 1000);
Assert.True(img.Resize(w, h));
}

[Theory]
[InlineData(1, 1)]
[InlineData(100, 100)]
[InlineData(1000, 20)]
public void Expand(int w, int h)
{
var img = new ImageData(1000, 1000);
var r = img.Expand(w, h);
if (w > img.Width || h > img.Height)
Assert.True(r);
else
Assert.False(r);
}
}
}
91 changes: 91 additions & 0 deletions Image2Display/Image2Display/Models/ImageData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Image2Display.Models
{
public class ImageData
{
/// <summary>
/// 图片的原始数据
/// </summary>
private Image<Rgba32> Raw;

/// <summary>
/// 图片宽度
/// </summary>
public int Width => Raw.Width;
/// <summary>
/// 图片高度
/// </summary>
public int Height => Raw.Height;

/// <summary>
/// 新建空的图片数据
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public ImageData(int width, int height) => Raw = new Image<Rgba32>(width, height);

/// <summary>
/// 从已有图片数据导入图片
/// </summary>
/// <param name="path">文件路径</param>
public ImageData(string path) => Raw = Image.Load<Rgba32>(path);


/// <summary>
/// 裁剪图片区域
/// </summary>
/// <param name="x">左上角起始点位置x</param>
/// <param name="y">左上角起始点位置y</param>
/// <param name="width">裁剪区域宽度</param>
/// <param name="height">裁剪区域高度</param>
/// <returns>是否成功</returns>
public bool Crop(int x, int y, int width, int height)
{
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.Crop(new Rectangle(x,y,width,height)));
return true;
}

/// <summary>
/// 调整图片大小(会被拉伸)
/// </summary>
/// <param name="width">目标宽度</param>
/// <param name="height">目标高度</param>
/// <returns>是否成功</returns>
public bool Resize(int width, int height)
{
if (width < 0 || height < 0)
return false;
Raw.Mutate(ctx => ctx.Resize(width, height));
return true;
}


/// <summary>
/// 拓展图片大小(向右下方拓展空白区域)
/// </summary>
/// <param name="width">目标宽度</param>
/// <param name="height">目标高度</param>
/// <returns>是否成功</returns>
public bool Expand(int width, int height)
{
if (width < Width || height < Height)//不能小于当前尺寸
return false;
var n = new Image<Rgba32>(width, height);
n.Mutate(ctx => ctx.DrawImage(Raw, new Point(0, 0), 1));
Raw = n;
return true;
}
}
}
5 changes: 5 additions & 0 deletions Image2Display/Image2Display/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public partial class SettingsViewModel : ViewModelBase
[ObservableProperty]
private string[] _languagesList = SettingModel.SupportLanguages;

/// <summary>
/// 被选中的语言index
/// 不会在别处被更改,所以不需要notify
/// </summary>
public int LanguagesSelected
{
get
Expand All @@ -49,6 +53,7 @@ public int LanguagesSelected
{
Utils.Settings.Language = LanguagesList[value];
Utils.ChangeLanguage(Utils.Settings.Language);
Utils.SaveSettings();
}
}

Expand Down

0 comments on commit fadef6d

Please sign in to comment.