Safety/Bitmap/XpsBitmapHelper.cs

118 lines
4.7 KiB
C#
Raw Normal View History

2026-02-03 14:33:42 +01:00
using System.IO;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using System.Windows.Media.Imaging;
using System;
using System.Threading;
using System.Drawing;
using System.Threading.Tasks;
namespace Bitmapper
{
static public class XpsBitmapHelper
{
static public bool SaveXpsPageToPng(string xpsFileName)
{
bool rv = false;
var tcs = new TaskCompletionSource<bool>();
Thread thread = new Thread(() =>
{
try
{
tcs.SetResult(SaveXpsPageToBitmap(xpsFileName));
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
tcs.Task.Wait();
if (tcs.Task.IsCompleted)
rv = tcs.Task.Result;
return rv;
}
static bool SaveXpsPageToBitmap(string para1)
{
bool rv = false;
string xpsFileName = para1 as string;
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
var t = xpsDoc.FixedDocumentSequenceReader.FixedDocuments[0].FixedPages[0];
BitmapEncoder encoder = null;
string ext = ".png";
var th = xpsDoc.Thumbnail;
if (false)
{ //th != null ) {
//ext = System.IO.Path.GetExtension(th.Uri.OriginalString);// ".jpeg" string
var x = th.GetStream();
var bf = BitmapFrame.Create(x);
encoder = new PngBitmapEncoder();
encoder.Frames.Add(bf);
using (FileStream pageOutStream = new FileStream(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(xpsFileName), System.IO.Path.GetFileNameWithoutExtension(xpsFileName)) + ext, FileMode.Create, FileAccess.Write))
{
encoder.Save(pageOutStream);
pageOutStream.Close();
}
}
else
{
string outfilename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(xpsFileName), System.IO.Path.GetFileNameWithoutExtension(xpsFileName)) + ext;
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
if (docSeq.DocumentPaginator.PageCount > 0)
{
RenderTargetBitmap renderTarget;
using (DocumentPage docPage = docSeq.DocumentPaginator.GetPage(0))
{
renderTarget =
new RenderTargetBitmap((int)docPage.Size.Width,
(int)docPage.Size.Height,
96, // WPF (Avalon) units are 96dpi based
96,
System.Windows.Media.PixelFormats.Default);
renderTarget.Render(docPage.Visual);
}
System.Drawing.Image bitmap1 = null;
encoder = new PngBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
var bm = BitmapFrame.Create(renderTarget);
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
var h = bm.Height;
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
bitmap1 = System.Drawing.Bitmap.FromStream(ms);
}
int newHeight = 128;
int newWidth = (int)(bitmap1.Width * 128.0 / bitmap1.Height);
Bitmap bm2 = new Bitmap(bitmap1, newWidth, newHeight);
bm2.Save(outfilename, System.Drawing.Imaging.ImageFormat.Png);
rv = true;
}
}
return rv;
}
public static bool BitmapToThumbnail(string bitmapFileName, out string outfilename)
{
outfilename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(bitmapFileName), System.IO.Path.GetFileNameWithoutExtension(bitmapFileName) + "_th.png");
bool ok = false;
Image i = Image.FromFile(bitmapFileName);
int newHeight = 128;
int newWidth = (int)(i.Width * 128.0 / i.Height);
Bitmap bm2 = new Bitmap(i, newWidth, newHeight);
bm2.Save(outfilename, System.Drawing.Imaging.ImageFormat.Png);
ok = true;
return ok;
}
}
}