forked from yangzhongke/NetAutoGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapData.cs
More file actions
86 lines (78 loc) · 2.74 KB
/
Copy pathBitmapData.cs
File metadata and controls
86 lines (78 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;
using SkiaSharp;
namespace NetAutoGUI;
/// <summary>
/// Managed Bitmap, there is no need to dispose it explicitly.
/// </summary>
/// <param name="Data">bitmap format data of an image</param>
/// <param name="Width">Width of the image</param>
/// <param name="Height">Heigh of the image</param>
public record BitmapData(byte[] Data, int Width, int Height)
{
public static Func<string, BitmapData> LoadFromFileFunc { get; set; }
public static BitmapData FromFile(string imageFile)
{
if (LoadFromFileFunc == null) throw new InvalidOperationException("LoadFromFileFunc is not set");
return LoadFromFileFunc(imageFile);
}
private static void CreateDir(string filename)
{
new FileInfo(filename).Directory!.Create();
}
/// <summary>
/// Save the image into a local file.
/// </summary>
/// <param name="filename">file name</param>
/// <param name="imgType">saved image format</param>
public void Save(string filename, ImageType? imgType = null)
{
CreateDir(filename);
using var output = File.OpenWrite(filename);
if (imgType == null) imgType = InferImageType(filename);
Save(output, imgType.Value);
}
/// <summary>
/// Save the image into a stream
/// </summary>
/// <param name="outStream">the output stream</param>
/// <param name="imgType">saved image format</param>
public void Save(Stream outStream, ImageType imgType)
{
using var image = SKImage.FromEncodedData(Data);
using var encodedImg = image.Encode(ToImageFormat(imgType), 100);
encodedImg.SaveTo(outStream);
}
private static ImageType InferImageType(string filename)
{
var ext = Path.GetExtension(filename);
if (ext == null) throw new ArgumentOutOfRangeException(nameof(filename), "no file extension");
ext = ext.ToLower();
switch (ext)
{
case ".jpg":
case ".jpeg":
return ImageType.Jpg;
case ".png":
return ImageType.Png;
case ".webp":
return ImageType.WebP;
default:
throw new ArgumentOutOfRangeException(nameof(filename), $"unexpected file extension:{ext}");
}
}
private static SKEncodedImageFormat ToImageFormat(ImageType imgType)
{
switch (imgType)
{
case ImageType.WebP:
return SKEncodedImageFormat.Webp;
case ImageType.Jpg:
return SKEncodedImageFormat.Jpeg;
case ImageType.Png:
return SKEncodedImageFormat.Png;
default:
throw new ArgumentOutOfRangeException(nameof(imgType));
}
}
}