21 using System.Runtime.InteropServices;
38 public RasterImageFile(
string fileName,
int pageNumber = 0,
double scale = 1,
bool alpha =
true,
bool interpolate =
true)
40 using (MuPDFContext context =
new MuPDFContext())
42 using (MuPDFDocument document =
new MuPDFDocument(context, fileName))
44 RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);
46 this.
Width = roundedBounds.Width;
47 this.
Height = roundedBounds.Height;
52 this.
Id = Guid.NewGuid().ToString();
54 int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);
58 GC.AddMemoryPressure(imageSize);
60 document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB,
this.ImageDataAddress);
80 public RasterImageStream(Stream imageStream, InputFileTypes fileType,
int pageNumber = 0,
double scale = 1,
bool alpha =
true,
bool interpolate =
true)
82 using (MuPDFContext context =
new MuPDFContext())
84 IntPtr originalImageAddress;
85 long originalImageLength;
87 IDisposable toBeDisposed =
null;
88 GCHandle handleToFree;
90 if (imageStream is MemoryStream ms)
92 int origin = (int)ms.Seek(0, SeekOrigin.Begin);
93 originalImageLength = ms.Length;
95 handleToFree = GCHandle.Alloc(ms.GetBuffer(), GCHandleType.Pinned);
96 originalImageAddress = handleToFree.AddrOfPinnedObject();
100 MemoryStream mem =
new MemoryStream((
int)imageStream.Length);
101 imageStream.CopyTo(mem);
105 int origin = (int)mem.Seek(0, SeekOrigin.Begin);
106 originalImageLength = mem.Length;
108 handleToFree = GCHandle.Alloc(mem.GetBuffer(), GCHandleType.Pinned);
109 originalImageAddress = handleToFree.AddrOfPinnedObject();
112 using (MuPDFDocument document =
new MuPDFDocument(context, originalImageAddress, originalImageLength, fileType))
114 RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);
116 this.
Width = roundedBounds.Width;
117 this.
Height = roundedBounds.Height;
122 this.
Id = Guid.NewGuid().ToString();
124 int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);
128 GC.AddMemoryPressure(imageSize);
130 document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB,
this.ImageDataAddress);
134 toBeDisposed?.Dispose();
148 public RasterImageStream(IntPtr imageAddress,
long imageLength, InputFileTypes fileType,
int pageNumber = 0,
double scale = 1,
bool alpha =
true,
bool interpolate =
true)
150 using (MuPDFContext context =
new MuPDFContext())
152 using (MuPDFDocument document =
new MuPDFDocument(context, imageAddress, imageLength, fileType))
154 RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);
156 this.
Width = roundedBounds.Width;
157 this.
Height = roundedBounds.Height;
162 this.
Id = Guid.NewGuid().ToString();
164 int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);
168 GC.AddMemoryPressure(imageSize);
170 document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB,
this.ImageDataAddress);