使用 open xml 在 powerpoint 中设置图像大小

发布于 2024-09-30 01:43:53 字数 387 浏览 0 评论 0原文

我正在使用本教程生成一个 ppt 文件 此处

步骤 4 描述了如何换出图像占位符。 我的图像具有不同的尺寸,这使得某些图像看起来有点太有趣了。

有什么方法可以调整占位符的大小以保持尺寸吗?

编辑:好的,更好的解释:用户可以上传自己的图像。图像存储在服务器上。我正在生成一个 ppt 文件,每张幻灯片有一个用户。每张幻灯片都会有一个图像(如果有的话)。我当然可以获取每个图像的尺寸,但是如何用占位符以外的其他尺寸的图像替换占位符?

Im am generating a ppt-file using this tutorial here

Step 4 describes how to swap out the image placeholder.
My images has different dimensions, which makes some images look a little bit too funny.

Is there any way to resize the placeholder so it can keep the dimensions?

Edit: Ok, a better explanation: users can upload images of them selves. The images are stored on the server. I am generating a ppt-file with one user per slide. And for every slide there will be an image, if any. I can of course get the dimensions of every image, but how do I replace the placeholder with an image of another dimension than the placeholder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-10-07 01:43:53

好吧,我无法根据该教程告诉您,但我可以告诉您它是在 Open XML 中完成的(即不是 SDK)。

您的图片将包含一个包含一组值的 xfrm 元素,如下所示:

    <p:spPr>
      <a:xfrm>
        <a:off x="7048050" y="6248401"/>
        <a:ext cx="972000" cy="288000"/>
      </a:xfrm>
    </p:spPr>

您要更改的值是 cxcy代码>a:ext。从 System.Drawing.Image 对象中获取新图片的尺寸(高和宽),然后将每个值乘以 12700。因此,如果图片的宽度为 400 像素,则 < a:ext 的 code>cx 值为 (400 x 12700 = 5080000)。

Well, I can't tell you based on that tutorial, but I can tell you where it is done in Open XML (i.e. not the SDK).

Your picture will have an xfrm element with a set of values, like this:

    <p:spPr>
      <a:xfrm>
        <a:off x="7048050" y="6248401"/>
        <a:ext cx="972000" cy="288000"/>
      </a:xfrm>
    </p:spPr>

The values you want to change are the cx and cy of a:ext. Take your new picture's dimensions (h and w) from like a System.Drawing.Image object and take each of the values and multiply by 12700. So if the width of the picture is 400 pixels, the cx value of a:ext will be (400 x 12700 = 5080000).

仅此而已 2024-10-07 01:43:53

我就是这样做的:

using DocumentFormat.OpenXml.Packaging;

假设您有 SlidePart

在我的例子中,我想检查图片的替代标题,如果它与我的键匹配,则替换它们。

//find all image alt title (description) in the slide
List<DocumentFormat.OpenXml.Presentation.Picture> slidePictures = slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>()
.Where(a => a.NonVisualPictureProperties.NonVisualDrawingProperties.Description.HasValue).Distinct().ToList();

现在我们检查所有图像:

//check all images in the slide and replace them if it matches our parameter
foreach (DocumentFormat.OpenXml.Presentation.Picture imagePlaceHolder in slidePictures)

现在在循环中我们查找 Transform2D 并用我们的值修改它:

Transform2D transform = imagePlaceHolder.Descendants<Transform2D>().First();
Tuple<Int64Value, Int64Value> aspectRatio = CorrectAspectRatio(param.Image.FullName, transform.Extents.Cx, transform.Extents.Cy);
                        transform.Extents.Cx = aspectRatio.Item1;
                        transform.Extents.Cy = aspectRatio.Item2;

这个函数如下所示:

public static Tuple<Int64Value, Int64Value> CorrectAspectRatio(string fileName, Int64Value cx, Int64Value cy)
{
   BitmapImage img = new();
   using (FileStream fs = new(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
   {
       img.BeginInit();
       img.StreamSource = fs;
       img.EndInit();
    }
   int widthPx = img.PixelWidth;
   int heightPx = img.PixelHeight;

   const int EMUsPerInch = 914400;

   Int64Value x = (Int64Value)(widthPx * EMUsPerInch / img.DpiX);
   Int64Value y = (Int64Value)(heightPx * EMUsPerInch / img.DpiY);

   if (x > cx)
   {
       decimal ratio = cx * 1.0m / x;
       x = cx;
       y = (Int64Value)(cy * ratio);
    }

    if (y > cy)
    {
        decimal ratio = cy * 1.0m / y;
        y = cy;
        x = (Int64Value)(cx * ratio);
    }

    return new Tuple<Int64Value, Int64Value>(x, y);
}

需要注意的重要一点是 EMU perinch 是 914400。在大多数情况下,您只需将其除以 96,但对于某些显示器而言,情况有所不同。因此,最好将 DPI 分为 xy

This is how I did it:

using DocumentFormat.OpenXml.Packaging;

lets assume you have your SlidePart

In my case I wanted to check for the alt title of the pictures and replace them if it matches to my key.

//find all image alt title (description) in the slide
List<DocumentFormat.OpenXml.Presentation.Picture> slidePictures = slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>()
.Where(a => a.NonVisualPictureProperties.NonVisualDrawingProperties.Description.HasValue).Distinct().ToList();

now we check all the images:

//check all images in the slide and replace them if it matches our parameter
foreach (DocumentFormat.OpenXml.Presentation.Picture imagePlaceHolder in slidePictures)

now in the loop we look for Transform2D and modify it with our value:

Transform2D transform = imagePlaceHolder.Descendants<Transform2D>().First();
Tuple<Int64Value, Int64Value> aspectRatio = CorrectAspectRatio(param.Image.FullName, transform.Extents.Cx, transform.Extents.Cy);
                        transform.Extents.Cx = aspectRatio.Item1;
                        transform.Extents.Cy = aspectRatio.Item2;

And this function looks like this:

public static Tuple<Int64Value, Int64Value> CorrectAspectRatio(string fileName, Int64Value cx, Int64Value cy)
{
   BitmapImage img = new();
   using (FileStream fs = new(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
   {
       img.BeginInit();
       img.StreamSource = fs;
       img.EndInit();
    }
   int widthPx = img.PixelWidth;
   int heightPx = img.PixelHeight;

   const int EMUsPerInch = 914400;

   Int64Value x = (Int64Value)(widthPx * EMUsPerInch / img.DpiX);
   Int64Value y = (Int64Value)(heightPx * EMUsPerInch / img.DpiY);

   if (x > cx)
   {
       decimal ratio = cx * 1.0m / x;
       x = cx;
       y = (Int64Value)(cy * ratio);
    }

    if (y > cy)
    {
        decimal ratio = cy * 1.0m / y;
        y = cy;
        x = (Int64Value)(cx * ratio);
    }

    return new Tuple<Int64Value, Int64Value>(x, y);
}

Important thing to note is that EMU per inch is 914400. In most cases you just need to divide it by 96, but for some monitors it is different. Therefore it is best to divide it the DPI for x and y.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文