Skip to content

Commit 1dfd820

Browse files
Merge pull request #142 from ArtifexSoftware/maksym-dev-support-skiasharp
added updates for cross-platform support
2 parents 58fb483 + dfa916a commit 1dfd820

32 files changed

+428
-212
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
### [1.3.2] - 2025-12-10
4+
- Upgraded SkiaSharp version (2.88.8 -> 3.119.1)
5+
- Included SkiaSharp.NativeAssets.Linux
6+
7+
### [1.3.2-rc.3] - 2025-12-05
8+
- Converted from System.Drawing.Bitmap to SkiaSharp.SKBitmap for cross-platform support
9+
- Replaced System.Drawing.Common package with SkiaSharp (2.88.8)
10+
- Updated memory operations to work on both Windows and Linux
11+
- Fixed color accuracy by correctly handling BGR format from Ghostscript
12+
- Updated platform-specific structures for Linux compatibility

Ghostscript.NET.DisplayTest/FMain.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using Ghostscript.NET;
1010
using Ghostscript.NET.Viewer;
1111
using Ghostscript.NET.Interpreter;
12+
using SkiaSharp;
13+
using SkiaSharp.Views.Desktop;
1214

1315
namespace Ghostscript.NET.DisplayTest
1416
{
@@ -17,6 +19,7 @@ public partial class FMain : Form
1719
private GhostscriptViewer _viewer;
1820
private FPreview _preview = new FPreview();
1921
private StdIOHandler _stdioHandler;
22+
private SKBitmap _currentBitmap = null;
2023

2124
public FMain()
2225
{
@@ -48,19 +51,17 @@ private void FMain_Load(object sender, EventArgs e)
4851

4952
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
5053
{
51-
_preview.pbDisplay.Invalidate();
52-
_preview.pbDisplay.Update();
54+
_preview.UpdateImage(e.Image);
5355
}
5456

5557
void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
5658
{
57-
_preview.pbDisplay.Invalidate();
58-
_preview.pbDisplay.Update();
59+
_preview.UpdateImage(e.Image);
5960
}
6061

6162
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
6263
{
63-
_preview.pbDisplay.Image = e.Image;
64+
_preview.UpdateImage(e.Image);
6465
}
6566

6667
private void btnRun_Click(object sender, EventArgs e)

Ghostscript.NET.DisplayTest/FPreview.Designer.cs

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Ghostscript.NET.DisplayTest/FPreview.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,77 @@
66
using System.Linq;
77
using System.Text;
88
using System.Windows.Forms;
9+
using SkiaSharp;
10+
using SkiaSharp.Views.Desktop;
911

1012
namespace Ghostscript.NET.DisplayTest
1113
{
1214
public partial class FPreview : Form
1315
{
16+
private SKBitmap _currentBitmap = null;
17+
1418
public FPreview()
1519
{
1620
InitializeComponent();
1721

1822
this.Left = 0;
1923
this.Top = 0;
24+
25+
pbDisplay.PaintSurface += PbDisplay_PaintSurface;
26+
}
27+
28+
private void PbDisplay_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
29+
{
30+
var canvas = e.Surface.Canvas;
31+
canvas.Clear(SKColors.White);
32+
33+
if (_currentBitmap != null)
34+
{
35+
var destRect = SKRect.Create(pbDisplay.Width, pbDisplay.Height);
36+
var sourceRect = SKRect.Create(_currentBitmap.Width, _currentBitmap.Height);
37+
canvas.DrawBitmap(_currentBitmap, sourceRect, destRect);
38+
}
39+
}
40+
41+
public void UpdateImage(SKBitmap bitmap)
42+
{
43+
_currentBitmap = bitmap;
44+
ResizeCanvas(bitmap);
45+
RefreshCanvas();
2046
}
2147

2248
private void FPreview_Load(object sender, EventArgs e)
2349
{
2450

2551
}
52+
53+
private void ResizeCanvas(SKBitmap bitmap)
54+
{
55+
if (bitmap == null)
56+
{
57+
return;
58+
}
59+
60+
if (pbDisplay.Width == bitmap.Width && pbDisplay.Height == bitmap.Height)
61+
{
62+
return;
63+
}
64+
65+
pbDisplay.SuspendLayout();
66+
pbDisplay.Width = bitmap.Width;
67+
pbDisplay.Height = bitmap.Height;
68+
pbDisplay.ResumeLayout();
69+
}
70+
71+
private void RefreshCanvas()
72+
{
73+
if (!pbDisplay.IsHandleCreated)
74+
{
75+
return;
76+
}
77+
78+
pbDisplay.Invalidate();
79+
pbDisplay.Update();
80+
}
2681
}
2782
}

Ghostscript.NET.DisplayTest/Ghostscript.NET.DisplayTest.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0-windows</TargetFramework>
4-
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
5-
<OutputType>WinExe</OutputType>
4+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
5+
<OutputType>Exe</OutputType>
6+
<Platforms>AnyCPU;x64</Platforms>
67
<SccProjectName>
78
</SccProjectName>
89
<SccLocalPath>
@@ -34,4 +35,8 @@
3435
<ItemGroup>
3536
<Content Include="_res\ghostscript-dotnet.png" />
3637
</ItemGroup>
38+
<ItemGroup>
39+
<PackageReference Include="SkiaSharp" Version="3.119.1" />
40+
<PackageReference Include="SkiaSharp.Views.WindowsForms" Version="3.119.1" />
41+
</ItemGroup>
3742
</Project>

Ghostscript.NET.Samples/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2525
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

27-
using System;
28-
using System.Collections.Generic;
2927
using Ghostscript.NET;
28+
using Ghostscript.NET.Processor;
29+
using Ghostscript.NET.Rasterizer;
3030
using Ghostscript.NET.Samples;
31+
using SkiaSharp;
32+
using System;
33+
using System.Collections.Generic;
34+
using System.Diagnostics;
35+
using System.IO;
3136

3237
Console.WriteLine("Ghostscript.NET Samples");
3338

@@ -58,4 +63,4 @@
5863
Console.WriteLine($"Sample '{sample.GetType().Name}' run successful!");
5964
}
6065

61-
Console.ReadLine();
66+
Console.ReadKey();

Ghostscript.NET.Samples/Samples/RasterizerCropSample.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

2727
using System;
28-
using System.Drawing;
29-
using System.Drawing.Imaging;
3028
using System.IO;
29+
using SkiaSharp;
3130

3231
// required Ghostscript.NET namespaces
3332
using Ghostscript.NET;
@@ -63,8 +62,16 @@ public void Start()
6362
{
6463
string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
6564

66-
Image img = rasterizer.GetPage(desired_dpi, pageNumber);
67-
img.Save(pageFilePath, ImageFormat.Png);
65+
SKBitmap img = rasterizer.GetPage(desired_dpi, pageNumber);
66+
if (img != null)
67+
{
68+
using (var image = SKImage.FromBitmap(img))
69+
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
70+
using (var stream = File.OpenWrite(pageFilePath))
71+
{
72+
data.SaveTo(stream);
73+
}
74+
}
6875

6976
Console.WriteLine(pageFilePath);
7077
}

Ghostscript.NET.Samples/Samples/RasterizerSample1.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.IO;
3131
using Ghostscript.NET.Rasterizer;
3232
using Ghostscript.NET.Samples.StdIOHandlers;
33+
using SkiaSharp;
3334

3435
namespace Ghostscript.NET.Samples
3536
{
@@ -64,7 +65,15 @@ public void Sample1()
6465
var pageFilePath = Path.Combine(outputPath, string.Format("Page-{0}.png", pageNumber));
6566

6667
var img = rasterizer.GetPage(desired_dpi, pageNumber);
67-
img.Save(pageFilePath, ImageFormat.Png);
68+
if (img != null)
69+
{
70+
using (var image = SKImage.FromBitmap(img))
71+
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
72+
using (var stream = File.OpenWrite(pageFilePath))
73+
{
74+
data.SaveTo(stream);
75+
}
76+
}
6877

6978
Console.WriteLine(pageFilePath);
7079
}
@@ -92,7 +101,15 @@ public void Sample2()
92101
var pageFilePath = Path.Combine(outputPath, string.Format("Page-{0}.png", pageNumber));
93102

94103
var img = rasterizer.GetPage(desired_dpi, pageNumber);
95-
img.Save(pageFilePath, ImageFormat.Png);
104+
if (img != null)
105+
{
106+
using (var image = SKImage.FromBitmap(img))
107+
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
108+
using (var stream = File.OpenWrite(pageFilePath))
109+
{
110+
data.SaveTo(stream);
111+
}
112+
}
96113

97114
Console.WriteLine(pageFilePath);
98115
}

Ghostscript.NET.Samples/Samples/RasterizerSample2.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
// required Ghostscript.NET namespaces
3333
using Ghostscript.NET;
3434
using Ghostscript.NET.Rasterizer;
35+
using SkiaSharp;
3536

3637
namespace Ghostscript.NET.Samples
3738
{
@@ -67,8 +68,16 @@ public void Start()
6768
{
6869
string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
6970

70-
Image img = rasterizer.GetPage(desired_dpi, pageNumber);
71-
img.Save(pageFilePath, ImageFormat.Png);
71+
var img = rasterizer.GetPage(desired_dpi, pageNumber);
72+
if (img != null)
73+
{
74+
using (var image = SKImage.FromBitmap(img))
75+
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
76+
using (var stream = File.OpenWrite(pageFilePath))
77+
{
78+
data.SaveTo(stream);
79+
}
80+
}
7281

7382
Console.WriteLine(pageFilePath);
7483
}

Ghostscript.NET.Samples/Samples/ViewerSample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
using System;
2828
using System.Collections.Generic;
29-
using System.Drawing;
29+
using SkiaSharp;
3030

3131
// required Ghostscript.NET namespaces
3232
using Ghostscript.NET;
@@ -38,7 +38,7 @@ public class ViewerSample : ISample
3838
{
3939
private GhostscriptVersionInfo _lastInstalledVersion = null;
4040
private GhostscriptViewer _viewer = null;
41-
private Bitmap _pdfPage = null;
41+
private SKBitmap _pdfPage = null;
4242

4343
public void Start()
4444
{
@@ -92,7 +92,7 @@ void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
9292
// it by calling PictureBox.Invalidate() and PictureBox.Update()
9393
// methods. We dont need to set image reference again because
9494
// Ghostscript.NET is changing Image object directly in the
95-
// memory and does not create new Bitmap instance.
95+
// memory and does not create new SKBitmap instance.
9696
}
9797

9898
// this is the last raised event after complete page is rasterized

0 commit comments

Comments
 (0)