Skip to content

Commit 1215e4b

Browse files
committed
搭建转换对接逻辑
1 parent 839048f commit 1215e4b

3 files changed

Lines changed: 84 additions & 4 deletions

File tree

Workbench/Wmf/SkiaWmfRenderer/samples/sample/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
// See https://aka.ms/new-console-template for more information
22

3-
using System.Diagnostics;
43
using Oxage.Wmf;
4+
using Oxage.Wmf.Records;
5+
6+
using SkiaSharp;
7+
8+
using SkiaWmfRenderer;
9+
10+
using System.Diagnostics;
511
using System.Drawing;
612
using System.Drawing.Imaging;
713
using System.Text;
8-
using Oxage.Wmf.Records;
9-
using SkiaSharp;
1014

1115
var file = @"C:\lindexi\wmf公式\image17.wmf";
16+
17+
SkiaWmfRenderHelper.TryConvertToPng(new FileInfo(file), new FileInfo("foo.png"));
18+
1219
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
1320

1421
var image = Image.FromFile(file);
Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
1-
using System;
1+
using Oxage.Wmf;
2+
3+
using SkiaSharp;
4+
5+
using System;
26
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
38
using System.Linq;
49
using System.Text;
510
using System.Threading.Tasks;
11+
using System.Xml;
612

713
namespace SkiaWmfRenderer;
14+
815
public static class SkiaWmfRenderHelper
916
{
17+
public static bool TryConvertToPng(FileInfo wmfFile, FileInfo outputPngFile)
18+
{
19+
if (!TryRender(wmfFile, 0, 0, out var skBitmap))
20+
{
21+
return false;
22+
}
23+
24+
using var outputStream = outputPngFile.OpenWrite();
25+
skBitmap.Encode(outputStream, SKEncodedImageFormat.Png, 100);
26+
27+
return true;
28+
}
29+
30+
public static bool TryRender(FileInfo wmfFile, int requestWidth, int requestHeight, [NotNullWhen(true)] out SKBitmap? skBitmap)
31+
{
32+
skBitmap = null;
33+
34+
using var fileStream = wmfFile.OpenRead();
35+
var wmfDocument = new WmfDocument();
36+
try
37+
{
38+
wmfDocument.Load(fileStream);
39+
}
40+
catch (WmfException e)
41+
{
42+
return false;
43+
}
44+
45+
var wmfRenderer = new WmfRenderer()
46+
{
47+
WmfDocument = wmfDocument,
48+
RequestWidth = requestWidth,
49+
RequestHeight = requestHeight,
50+
};
51+
52+
return wmfRenderer.TryRender(out skBitmap);
53+
}
1054
}
55+
56+
class WmfRenderer
57+
{
58+
public required WmfDocument WmfDocument { get; init; }
59+
public required int RequestWidth { get; init; }
60+
public required int RequestHeight { get; init; }
61+
62+
public bool TryRender([NotNullWhen(true)] out SKBitmap? skBitmap)
63+
{
64+
skBitmap = null;
65+
66+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
67+
68+
var format = WmfDocument.Format;
69+
70+
var x = Math.Min(format.Left, format.Right);
71+
var y = Math.Min(format.Top, format.Bottom);
72+
73+
var width = Math.Abs(format.Right - format.Left);
74+
var height = Math.Abs(format.Bottom - format.Top);
75+
76+
return skBitmap == null;
77+
}
78+
}

Workbench/Wmf/SkiaWmfRenderer/src/SkiaWmfRenderer/SkiaWmfRenderer.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@
1010
<ProjectReference Include="..\wieslawsoltes-wmf\src\library\Library.csproj" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<PackageReference Include="SkiaSharp" Version="3.119.0" />
15+
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.7" />
16+
</ItemGroup>
17+
1318
</Project>

0 commit comments

Comments
 (0)