none
How can I change the bitmap in a PPT picture? RRS feed

All replies

  • Hi David,

    According to my search, there is no API that directly replaces the bitmap in the picture.

    You could use the Shapes.addpicture method to add the original picture to the shape, and then populate the bitmap with the link you gave.

    Please refer to the following link:

    Replacing (changing) the pictures in PowerPoint presentation

    Shapes.AddPicture Method (PowerPoint)

    Hopefully it helps you.

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, December 3, 2018 10:07 AM
    Moderator
  • Hi;

    If I have to create a new picture, how can I copy all the picture properties across to the new picture?

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, December 3, 2018 2:29 PM
  • Hi David,

    You could refer to the following link:

    Replacing (changing) the pictures in PowerPoint presentation

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, December 4, 2018 6:27 AM
    Moderator
  • Hi Bruce;

    That link suggests what I am presently trying. Here is my code:

    		/// <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);
    		}
    


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Wednesday, December 5, 2018 4:53 PM