Skip to content

Commit fadef6d

Browse files
committed
add: 图片数据类型
1 parent f553a8b commit fadef6d

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Image2Display.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace I2D_Test
9+
{
10+
public class ImageDataTest
11+
{
12+
[Theory]
13+
[InlineData(1, 1)]
14+
[InlineData(100, 100)]
15+
[InlineData(1000, 20)]
16+
public void NewSize(int w,int h)
17+
{
18+
_ = new ImageData(w, h);
19+
}
20+
21+
[Theory]
22+
[InlineData(1, 1,10,10)]
23+
[InlineData(100, 100, 10, 10)]
24+
public void CropOk(int x, int y,int w, int h)
25+
{
26+
var img = new ImageData(1000, 1000);
27+
Assert.True(img.Crop(x, y, w, h));
28+
}
29+
[Theory]
30+
[InlineData(100, 100, 10, 10000)]
31+
[InlineData(1000, 20, 10, 10)]
32+
public void CropFail(int x, int y, int w, int h)
33+
{
34+
var img = new ImageData(1000, 1000);
35+
Assert.False(img.Crop(x, y, w, h));
36+
}
37+
38+
[Theory]
39+
[InlineData(1, 1)]
40+
[InlineData(100, 2000)]
41+
[InlineData(1000, 20)]
42+
public void Resize(int w, int h)
43+
{
44+
var img = new ImageData(1000, 1000);
45+
Assert.True(img.Resize(w, h));
46+
}
47+
48+
[Theory]
49+
[InlineData(1, 1)]
50+
[InlineData(100, 100)]
51+
[InlineData(1000, 20)]
52+
public void Expand(int w, int h)
53+
{
54+
var img = new ImageData(1000, 1000);
55+
var r = img.Expand(w, h);
56+
if (w > img.Width || h > img.Height)
57+
Assert.True(r);
58+
else
59+
Assert.False(r);
60+
}
61+
}
62+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using SixLabors.ImageSharp;
2+
using SixLabors.ImageSharp.PixelFormats;
3+
using SixLabors.ImageSharp.Processing;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Image2Display.Models
11+
{
12+
public class ImageData
13+
{
14+
/// <summary>
15+
/// 图片的原始数据
16+
/// </summary>
17+
private Image<Rgba32> Raw;
18+
19+
/// <summary>
20+
/// 图片宽度
21+
/// </summary>
22+
public int Width => Raw.Width;
23+
/// <summary>
24+
/// 图片高度
25+
/// </summary>
26+
public int Height => Raw.Height;
27+
28+
/// <summary>
29+
/// 新建空的图片数据
30+
/// </summary>
31+
/// <param name="width">宽度</param>
32+
/// <param name="height">高度</param>
33+
public ImageData(int width, int height) => Raw = new Image<Rgba32>(width, height);
34+
35+
/// <summary>
36+
/// 从已有图片数据导入图片
37+
/// </summary>
38+
/// <param name="path">文件路径</param>
39+
public ImageData(string path) => Raw = Image.Load<Rgba32>(path);
40+
41+
42+
/// <summary>
43+
/// 裁剪图片区域
44+
/// </summary>
45+
/// <param name="x">左上角起始点位置x</param>
46+
/// <param name="y">左上角起始点位置y</param>
47+
/// <param name="width">裁剪区域宽度</param>
48+
/// <param name="height">裁剪区域高度</param>
49+
/// <returns>是否成功</returns>
50+
public bool Crop(int x, int y, int width, int height)
51+
{
52+
if (x < 0 || y < 0 || width < 0 || height < 0)
53+
return false;
54+
if (x + width > Raw.Width || y + height > Raw.Height)
55+
return false;
56+
Raw.Mutate(ctx => ctx.Crop(new Rectangle(x,y,width,height)));
57+
return true;
58+
}
59+
60+
/// <summary>
61+
/// 调整图片大小(会被拉伸)
62+
/// </summary>
63+
/// <param name="width">目标宽度</param>
64+
/// <param name="height">目标高度</param>
65+
/// <returns>是否成功</returns>
66+
public bool Resize(int width, int height)
67+
{
68+
if (width < 0 || height < 0)
69+
return false;
70+
Raw.Mutate(ctx => ctx.Resize(width, height));
71+
return true;
72+
}
73+
74+
75+
/// <summary>
76+
/// 拓展图片大小(向右下方拓展空白区域)
77+
/// </summary>
78+
/// <param name="width">目标宽度</param>
79+
/// <param name="height">目标高度</param>
80+
/// <returns>是否成功</returns>
81+
public bool Expand(int width, int height)
82+
{
83+
if (width < Width || height < Height)//不能小于当前尺寸
84+
return false;
85+
var n = new Image<Rgba32>(width, height);
86+
n.Mutate(ctx => ctx.DrawImage(Raw, new Point(0, 0), 1));
87+
Raw = n;
88+
return true;
89+
}
90+
}
91+
}

Image2Display/Image2Display/ViewModels/SettingsViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public partial class SettingsViewModel : ViewModelBase
3030
[ObservableProperty]
3131
private string[] _languagesList = SettingModel.SupportLanguages;
3232

33+
/// <summary>
34+
/// 被选中的语言index
35+
/// 不会在别处被更改,所以不需要notify
36+
/// </summary>
3337
public int LanguagesSelected
3438
{
3539
get
@@ -49,6 +53,7 @@ public int LanguagesSelected
4953
{
5054
Utils.Settings.Language = LanguagesList[value];
5155
Utils.ChangeLanguage(Utils.Settings.Language);
56+
Utils.SaveSettings();
5257
}
5358
}
5459

0 commit comments

Comments
 (0)