如何更改PPT图片中的位图?
How can I change the bitmap in a PPT picture?
The answer here 不会替换位图,它会添加到位图。有没有办法替换图片中的位图?
使用 PowerPoint COM 互操作(不是 VSTO)。
密码是:
/// <summary>
/// Create or update a picture (bitmap) tag.
/// </summary>
private static PowerPointTagLocation CreateOrUpdatePictureTag(PowerPointTagLocation tagLoc, bool isUpdate, Image picture,
string text, Slide slide)
{
// get a unique name for the picture
string name = FileUtils.MakeValidFileName("wr_bitmap", true) + rand.Next(9999);
// we need a physical file as the bitmap to display.
string filename = name + ".png";
filename = Path.Combine(Path.GetTempPath(), filename);
if (File.Exists(filename))
{
Trap.trap();
File.Delete(filename);
}
// if it's an update for a picture, we just update the AlternativeText.
// And the bitmap if we get it.
if (isUpdate && tagLoc.IsPicture) {
tagLoc.TagShape.AlternativeText = text;
if (picture != null) {
picture.Save(filename, ImageFormat.Png);
tagLoc.TagShape.Fill.UserPicture(filename);
File.Delete(filename);
}
return tagLoc;
}
if (picture == null)
CommonBitmaps.photo_scenery.Save(filename, ImageFormat.Png);
else
picture.Save(filename, ImageFormat.Png);
Shape shape;
using (Bitmap bitmap = new Bitmap(filename)) {
PictHandler.GetPictureSize(tagLoc.Tag, bitmap, 0, out var width, out var height);
// position/extent in points. Place in middle of slide
float x, y;
try
{
PageSetup page = ((Presentation)slide.Parent).PageSetup;
x = (page.SlideWidth - (width / 20f)) / 2f;
y = (page.SlideHeight - (height / 20f)) / 2f;
}
catch (Exception) {
x = y = 0;
}
shape = slide.Shapes.AddPicture(filename, MsoTriState.msoFalse, MsoTriState.msoTrue, x, y, width / 20f, height / 20f);
// set select
shape.AlternativeText = text;
shape.Name = name;
}
// copy properties across
// if (copyPicture)
// pic.AssignProperties(tagLoc.TagRange.Picture);
// delete the old picture as we wrote a new one.
if (isUpdate)
tagLoc.Delete();
// delete the temp file
File.Delete(filename);
return new PowerPointTagLocation(tagLoc.Tag, shape);
}
我把它作为 Microsoft 的支持票,最后得到的答复是你不能更换图片。
The answer here 不会替换位图,它会添加到位图。有没有办法替换图片中的位图?
使用 PowerPoint COM 互操作(不是 VSTO)。
密码是:
/// <summary>
/// Create or update a picture (bitmap) tag.
/// </summary>
private static PowerPointTagLocation CreateOrUpdatePictureTag(PowerPointTagLocation tagLoc, bool isUpdate, Image picture,
string text, Slide slide)
{
// get a unique name for the picture
string name = FileUtils.MakeValidFileName("wr_bitmap", true) + rand.Next(9999);
// we need a physical file as the bitmap to display.
string filename = name + ".png";
filename = Path.Combine(Path.GetTempPath(), filename);
if (File.Exists(filename))
{
Trap.trap();
File.Delete(filename);
}
// if it's an update for a picture, we just update the AlternativeText.
// And the bitmap if we get it.
if (isUpdate && tagLoc.IsPicture) {
tagLoc.TagShape.AlternativeText = text;
if (picture != null) {
picture.Save(filename, ImageFormat.Png);
tagLoc.TagShape.Fill.UserPicture(filename);
File.Delete(filename);
}
return tagLoc;
}
if (picture == null)
CommonBitmaps.photo_scenery.Save(filename, ImageFormat.Png);
else
picture.Save(filename, ImageFormat.Png);
Shape shape;
using (Bitmap bitmap = new Bitmap(filename)) {
PictHandler.GetPictureSize(tagLoc.Tag, bitmap, 0, out var width, out var height);
// position/extent in points. Place in middle of slide
float x, y;
try
{
PageSetup page = ((Presentation)slide.Parent).PageSetup;
x = (page.SlideWidth - (width / 20f)) / 2f;
y = (page.SlideHeight - (height / 20f)) / 2f;
}
catch (Exception) {
x = y = 0;
}
shape = slide.Shapes.AddPicture(filename, MsoTriState.msoFalse, MsoTriState.msoTrue, x, y, width / 20f, height / 20f);
// set select
shape.AlternativeText = text;
shape.Name = name;
}
// copy properties across
// if (copyPicture)
// pic.AssignProperties(tagLoc.TagRange.Picture);
// delete the old picture as we wrote a new one.
if (isUpdate)
tagLoc.Delete();
// delete the temp file
File.Delete(filename);
return new PowerPointTagLocation(tagLoc.Tag, shape);
}
我把它作为 Microsoft 的支持票,最后得到的答复是你不能更换图片。