VectSharp  2.2.1
A light library for C# vector graphics
Raster.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Affero General Public License as
7  published by the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Affero General Public License for more details.
13 
14  You should have received a copy of the GNU Affero General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>
16 */
17 
18 using MuPDFCore;
19 using System;
20 using System.IO;
21 using System.Runtime.InteropServices;
22 using VectSharp.PDF;
23 
25 {
26  /// <summary>
27  /// Contains methods to render a page to a PNG image.
28  /// </summary>
29  public static class Raster
30  {
31 
32  /// <summary>
33  /// Render the page to a PNG file.
34  /// </summary>
35  /// <param name="page">The <see cref="Page"/> to render.</param>
36  /// <param name="fileName">The full path to the file to save. If it exists, it will be overwritten.</param>
37  /// <param name="scale">The scale to be used when rasterising the page. This will determine the width and height of the image file.</param>
38  public static void SaveAsPNG(this Page page, string fileName, double scale = 1)
39  {
40  Document doc = new Document();
41  doc.Pages.Add(page);
42 
43  MemoryStream ms = new MemoryStream();
44  doc.SaveAsPDF(ms);
45 
46  using (MuPDFContext context = new MuPDFContext())
47  {
48  using (MuPDFDocument muDoc = new MuPDFDocument(context, ref ms, InputFileTypes.PDF))
49  {
50  muDoc.SaveImage(0, scale, MuPDFCore.PixelFormats.RGBA, fileName, RasterOutputFileTypes.PNG);
51  }
52  }
53 
54  ms.Dispose();
55  }
56 
57  /// <summary>
58  /// Render the page to a PNG stream.
59  /// </summary>
60  /// <param name="page">The <see cref="Page"/> to render.</param>
61  /// <param name="stream">The stream to which the PNG data will be written.</param>
62  /// <param name="scale">The scale to be used when rasterising the page. This will determine the width and height of the image file.</param>
63  public static void SaveAsPNG(this Page page, Stream stream, double scale = 1)
64  {
65  Document doc = new Document();
66  doc.Pages.Add(page);
67 
68  MemoryStream ms = new MemoryStream();
69  doc.SaveAsPDF(ms);
70 
71  using (MuPDFContext context = new MuPDFContext())
72  {
73  using (MuPDFDocument muDoc = new MuPDFDocument(context, ref ms, InputFileTypes.PDF))
74  {
75  muDoc.WriteImage(0, scale, MuPDFCore.PixelFormats.RGBA, stream, RasterOutputFileTypes.PNG);
76  }
77  }
78 
79  ms.Dispose();
80  }
81 
82  /// <summary>
83  /// Rasterise a region of a <see cref="Graphics"/> object.
84  /// </summary>
85  /// <param name="graphics">The <see cref="Graphics"/> object that will be rasterised.</param>
86  /// <param name="region">The region of the <paramref name="graphics"/> that will be rasterised.</param>
87  /// <param name="scale">The scale at which the image will be rendered.</param>
88  /// <param name="interpolate">Whether the resulting image should be interpolated or not when it is drawn on another <see cref="Graphics"/> surface.</param>
89  /// <returns>A <see cref="RasterImage"/> containing the rasterised graphics.</returns>
90  public static RasterImage Rasterise(this Graphics graphics, Rectangle region, double scale, bool interpolate)
91  {
92  Page pag = new Page(1, 1);
93  pag.Graphics.DrawGraphics(0, 0, graphics);
94  pag.Crop(region.Location, region.Size);
95 
96  Document doc = new Document();
97  doc.Pages.Add(pag);
98 
99  MemoryStream ms = new MemoryStream();
100  doc.SaveAsPDF(ms);
101 
102  IntPtr imageDataAddress;
103 
104  int width;
105  int height;
106 
107  using (MuPDFContext context = new MuPDFContext())
108  {
109  using (MuPDFDocument muDoc = new MuPDFDocument(context, ref ms, InputFileTypes.PDF))
110  {
111  int imageSize = muDoc.GetRenderedSize(0, scale, MuPDFCore.PixelFormats.RGBA);
112 
113  RoundedRectangle roundedBounds = muDoc.Pages[0].Bounds.Round(scale);
114 
115  width = roundedBounds.Width;
116  height = roundedBounds.Height;
117 
118 
119  imageDataAddress = Marshal.AllocHGlobal(imageSize);
120  GC.AddMemoryPressure(imageSize);
121 
122  muDoc.Render(0, scale, MuPDFCore.PixelFormats.RGBA, imageDataAddress);
123  }
124  }
125 
126  ms.Dispose();
127 
128  DisposableIntPtr disposableAddress = new DisposableIntPtr(imageDataAddress);
129 
130  return new RasterImage(ref disposableAddress, width, height, true, interpolate);
131  }
132  }
133 }
VectSharp.Rectangle
Represents a rectangle.
Definition: Point.cs:173
VectSharp.Graphics.DrawGraphics
void DrawGraphics(Point origin, Graphics graphics)
Draws a Graphics object on the current Graphics object.
Definition: Graphics.cs:802
VectSharp.RasterImage
Represents a raster image, created from raw pixel data. Consider using the derived classes included i...
Definition: RasterImage.cs:99
VectSharp.DisposableIntPtr
An IDisposable wrapper around an IntPtr that frees the allocated memory when it is disposed.
Definition: RasterImage.cs:54
VectSharp
Definition: Brush.cs:26
VectSharp.Document
Represents a collection of pages.
Definition: Document.cs:28
VectSharp.Page
Represents a Graphics object with a width and height.
Definition: Document.cs:48
VectSharp.Raster.Raster.SaveAsPNG
static void SaveAsPNG(this Page page, Stream stream, double scale=1)
Render the page to a PNG stream.
Definition: Raster.cs:63
VectSharp.Graphics
Represents an abstract drawing surface.
Definition: Graphics.cs:262
VectSharp.Raster.Raster
Contains methods to render a page to a PNG image.
Definition: Raster.cs:30
VectSharp.Rectangle.Size
Size Size
The size of the rectangle.
Definition: Point.cs:187
VectSharp.Raster
Definition: Raster.cs:25
VectSharp.PDF
Definition: PDFContext.cs:29
VectSharp.Page.Graphics
Graphics Graphics
Graphics surface of the page.
Definition: Document.cs:62
VectSharp.Raster.Raster.Rasterise
static RasterImage Rasterise(this Graphics graphics, Rectangle region, double scale, bool interpolate)
Rasterise a region of a Graphics object.
Definition: Raster.cs:90
VectSharp.Raster.Raster.SaveAsPNG
static void SaveAsPNG(this Page page, string fileName, double scale=1)
Render the page to a PNG file.
Definition: Raster.cs:38
VectSharp.Document.Pages
List< Page > Pages
The pages in the document.
Definition: Document.cs:32
VectSharp.Rectangle.Location
Point Location
The top-left corner of the rectangle.
Definition: Point.cs:182
VectSharp.Page.Crop
void Crop(Point topLeft, Size size)
Translate and resize the Page so that it displays the rectangle defined by topLeft and size .
Definition: Document.cs:88