mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
support conversion of pdf format images to png
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
using Core;
|
||||
using Graphics.Colors;
|
||||
using Graphics.Core;
|
||||
using Images.Png;
|
||||
using Tokens;
|
||||
using Util.JetBrains.Annotations;
|
||||
|
||||
@@ -107,6 +108,47 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGetPng(out byte[] bytes)
|
||||
{
|
||||
bytes = null;
|
||||
if (ColorSpace != Graphics.Colors.ColorSpace.DeviceRGB || !TryGetBytes(out var bytesPure))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var builder = PngBuilder.Create(WidthInSamples, HeightInSamples, false);
|
||||
|
||||
var isCorrectlySized = bytesPure.Count == (WidthInSamples * HeightInSamples * (BitsPerComponent / 8) * 3);
|
||||
|
||||
if (!isCorrectlySized)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
for (var y = 0; y < HeightInSamples; y++)
|
||||
{
|
||||
for (var x = 0; x < WidthInSamples; x++)
|
||||
{
|
||||
builder.SetPixel(bytesPure[i++], bytesPure[i++], bytesPure[i++], x, y);
|
||||
}
|
||||
}
|
||||
|
||||
bytes = builder.Save();
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored.
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user