我正在使用这个教程here生成一个ppt文件
步骤4描述了如何换出图像占位符。我的图片有不同的维度,这使得一些图片看起来有点太滑稽了。
有没有办法调整占位符的大小,使其可以保留尺寸?
编辑:好的,一个更好的解释:用户可以上传他们自己的照片。图像存储在服务器上。我正在生成一个每张幻灯片一个用户的ppt文件。而且每张幻灯片都会有一张图片。我当然可以得到每个图像的尺寸,但是如何用占位符以外的其他维度的图像替换占位符呢?
发布于 2010-10-28 23:48:06
好吧,我不能根据那个教程告诉你,但是我可以告诉你它是在Open XML中完成的(也就是说不是SDK)。
您的图片将有一个带有一组值的xfrm元素,如下所示:
<p:spPr>
<a:xfrm>
<a:off x="7048050" y="6248401"/>
<a:ext cx="972000" cy="288000"/>
</a:xfrm>
</p:spPr>要更改的值是a:ext的cx和cy。从System.Drawing.Image对象中获取新图片的尺寸(h和w),并取每个值并乘以12700。因此,如果图片的宽度为400像素,则a:ext的cx值将为(400x 12700 = 5080000)。
发布于 2021-07-27 19:28:49
我是这样做的:
using DocumentFormat.OpenXml.Packaging;让我们假设您有SlidePart
在我的例子中,我想检查图片的alt标题,如果它与我的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();现在我们检查所有的图像:
//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 per inch是914400。在大多数情况下,您只需将其除以96即可,但对于某些监视器来说,这是不同的。因此,最好将其划分为x和y的DPI。
https://stackoverflow.com/questions/4040643
复制相似问题