///
/// 开始转换
///
/// xps的路径
/// 图片保存的路径
///
public static void ConvertToJpg(object obj)
{
string[] str = obj as string[];
string xpsPath = str[0];
string saveImgPath = str[1];
BitmapEncoder bitmapEncoder = null;
MemoryStream memoryStream = new MemoryStream();
XpsDocument xpsDocument;
try
{
xpsDocument = new XpsDocument(xpsPath, System.IO.FileAccess.Read);
FixedDocumentSequence documentPageSequence = xpsDocument.GetFixedDocumentSequence();
bitmapEncoder = new JpegBitmapEncoder();
for (int i = 0; i < documentPageSequence.DocumentPaginator.PageCount; i++)
{
DocumentPage documentPage = documentPageSequence.DocumentPaginator.GetPage(i);
//RenderTargetBitmap targetBitmap = new RenderTargetBitmap(2479, 3508, 330, 400, PixelFormats.Default);
//创建目标位图对象
RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)documentPage.Size.Width, (int)documentPage.Size.Height, 96.0, 96.0, PixelFormats.Pbgra32);
targetBitmap.Render(documentPage.Visual);
bitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));
}
xpsDocument.Close();
bitmapEncoder.Save(memoryStream);
Bitmap bt = new System.Drawing.Bitmap(memoryStream);
// bt= KiResizeImage(bt, 2479, 3508);
bt.Save(saveImgPath, ImageFormat.Jpeg);
bt.Dispose();
bitmapEncoder = null;
// return true;
}
catch (Exception ex)
{
//NLogLogger.Instance.Info("ConvertToJpg:" + ex.ToString());
}
finally
{
memoryStream.Dispose();
}
}