Aspose.Slides.NET 19.2 解析ppt内容保存svg 保存ppt内部图片
转图片可以直接使用,需要注意传递宽高,也可以转svg再转图片(可能会在不同电脑上效果不同,建议是使用图片直接转)
slide.GetThumbnail(new System.Drawing.Size((int)windowWidth, (int)windowHeight)).Save($"slide_test.png", ImageFormat.Png);
如果需要根据ppt宽高比生成图片可以使用SlideSize获取宽高然后计算宽高比
Presentation presentation = new Presentation(filePath);
var size = presentation.SlideSize;
完整逻辑如下
// Load PowerPoint presentation
Presentation presentation = new Presentation("D:\\work\\code\\demo\\WPFPPT\\test.ppt");
var slide = presentation.Slides[3];
// Save slide as SVG
var stream = new FileStream(@"D:\图像{0}.svg", FileMode.Create);
slide.WriteAsSvg(stream);
foreach (var item in presentation.Slides)
{
// 方法1 直接转成图片
//获取屏幕宽高生成ppt截图
double windowWidth = 0;
double windowHeight = 0;
Application.Current.Dispatcher.Invoke(() =>
{
windowWidth = Application.Current.MainWindow.Width;
windowHeight = Application.Current.MainWindow.Height;
});
//传递宽高后可以生成清晰的图片
//slide.GetThumbnail(new System.Drawing.Size((int)windowWidth, (int)windowHeight)).Save($"slide_test.png", ImageFormat.Png);
// 方法2 转成svg后保存图片
var shapes = item.Shapes;
foreach (var shape in shapes)
{
if(shape is IPictureFrame image)
{
var img = image.PictureFormat.Picture.Image;
var imageType = img.ContentType.Remove(0, img.ContentType.IndexOf("/") + 1);
ImageFormat format ;
switch (imageType)
{
case "jpeg":
format = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "emf":
format = System.Drawing.Imaging.ImageFormat.Emf;
break;
case "bmp":
format = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "png":
format = System.Drawing.Imaging.ImageFormat.Png;
break;
case "wmf":
format = System.Drawing.Imaging.ImageFormat.Wmf;
break;
case "gif":
format = System.Drawing.Imaging.ImageFormat.Gif;
break;
}
img.SystemImage.Save(@$"D:\图像xx.{imageType}");
//img.ContentType
//img.BinaryData
//image.PictureFormat.Picture.Image.BinaryData.Save(stream, Aspose.Slides.Export.SaveFormat.Png);
//shape.Save(@"D:\图像{0}.png", Aspose.Slides.Export.SaveFormat.Png);
}
//presentation.Images.
//image.Picture.Save(stream, Aspose.Slides.Export.SaveFormat.Png);
//shape.Save(@"D:\图像{0}.png", Aspose.Slides.Export.SaveFormat.Png);
}
}
注入授权
//对应的授权码
var v19_12 = "xxxxxx";
new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String(v19_12)));
svg转图片
需要安装nuget包【svg】
public class SVGHelper
{
/// <summary>
/// 保持svg为图片
/// </summary>
/// <param name="path"></param>
/// <param name="imgPath"></param>
/// <returns></returns>
public static void SaveSVGImg(string path,string imgPath)
{
var svgContent = File.ReadAllText(path);
// 使用SvgDocument解析SVG内容
SvgDocument svgDocument = SvgDocument.FromSvg<SvgDocument>(svgContent);
// 将SvgDocument转换为Drawing对象
System.Drawing.Bitmap bitmap = svgDocument.Draw();
bitmap.Save(imgPath);
}
/// <summary>
/// SVG转Bitmat然后再转为ImageSource
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static ImageSource ConvertSvgToDrawingImage(string path)
{
var svgContent = File.ReadAllText(path);
// 使用SvgDocument解析SVG内容
SvgDocument svgDocument = SvgDocument.FromSvg<SvgDocument>(svgContent);
// 将SvgDocument转换为Drawing对象
System.Drawing.Bitmap bitmap = svgDocument.Draw();
var imageSource = ToImageSource(bitmap);
return imageSource;
}
/// <summary>
/// Bitmap转ImageSource
/// </summary>
/// <param name="hObject"></param>
/// <returns></returns>
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource( Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
// 记得要进行内存释放。否则会有内存不足的报错。
if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
}
return wpfBitmap;
}
}
留待后查,同时方便他人
联系我:renhanlinbsl@163.com
联系我:renhanlinbsl@163.com