Answered by:
Insert image as powerpoint Backgrounds

Question
-
hello,
I developed a com-addins for powerpoint 2007 using c#.net 2008 and vsto. I want to insert an image as backgrounds of powerpoint slide by using code.
Thanks
DileepMonday, June 15, 2009 5:29 AM
Answers
-
How about the following code?
object indexOfSelectedSlide = 1;
PowerPoint.SlideRange slideRange = presentation.Slides.Range(indexOfSelectedSlide);
slideRange.FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
slideRange.Background.Fill.UserPicture(@"C:\BackgroundImage.jpg");- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:52 AM
Friday, August 7, 2009 12:42 PM -
Hi Dileep!
To insert an image as the background of all slides in a presentation:
PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
float slideHeight = presentation.PageSetup.SlideHeight;
float slideWidth = presentation.PageSetup.SlideWidth;presentation.SlideMaster.Shapes.AddPicture(
@"C:\BackgroundImage.jpg",
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, slideWidth, slideHeight);- Proposed as answer by Jose Anton Bautista Monday, June 15, 2009 6:18 AM
- Edited by Jose Anton Bautista Monday, June 15, 2009 9:12 AM
- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:51 AM
Monday, June 15, 2009 6:17 AM -
Hi Dileep!
If you would right-click any slide on the left pane (list of slides under the Normal view), you would see a popup menu.
This is the "Thumbnails" command bar.
This command bar has an item called "Reset Slide", which you need in order to update the current slide with your background image.
To execute the command that this item represents, you simply call:
Globals.ThisAddIn.Application.CommandBars["Thumbnails"].Controls["&Reset Slide"].Execute();- Marked as answer by Dileep sankar Tuesday, June 16, 2009 5:28 AM
- Unmarked as answer by Dileep sankar Thursday, July 30, 2009 10:01 AM
- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:52 AM
Tuesday, June 16, 2009 4:32 AM -
I see. The problem is in your first code.
When you insert the image, it is being inserted as an actual Shape object, not a background.
Your second code works but the image inserted earlier is covering the new background hence you can't see the background.
Try the following for setting the background for all slides:
presentation.SlideMaster.Background.Fill.UserPicture(@"C:\BackgroundImage.jpg");- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:52 AM
Monday, August 10, 2009 11:23 AM
All replies
-
Hi Dileep!
To insert an image as the background of all slides in a presentation:
PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
float slideHeight = presentation.PageSetup.SlideHeight;
float slideWidth = presentation.PageSetup.SlideWidth;presentation.SlideMaster.Shapes.AddPicture(
@"C:\BackgroundImage.jpg",
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, slideWidth, slideHeight);- Proposed as answer by Jose Anton Bautista Monday, June 15, 2009 6:18 AM
- Edited by Jose Anton Bautista Monday, June 15, 2009 9:12 AM
- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:51 AM
Monday, June 15, 2009 6:17 AM -
Hai Jose,
The above code inserted image as background. But here two problems,
1)image fit into Presentation. That means , left, right, top, bottom size same as the presentaion size. How can i?
2)after inserting image as background, It will dispaly only when minimize or maximize the powerpoint.
Thanks
DileepMonday, June 15, 2009 9:00 AM -
Hi again!
I edited my code above to make the image size similar to the bounds of the slide.
For your 2nd problem, I'm not sure I understand what you're trying to say.
If the image is supposed to appear when the Powerpoint window is maximized or minimized,
you can try the following:
PowerPoint.Application application = Globals.ThisAddIn.Application;
PowerPoint.Presentation presentation = application.ActivePresentation;
PowerPoint.Shape backgroundImage = presentation.SlideMaster.Shapes.AddPicture(...if (application.WindowState == PowerPoint.PpWindowState.ppWindowMaximized ||
application.WindowState == PowerPoint.PpWindowState.ppWindowMinimized)
{
backgroundImage.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
}else
{
backgroundImage.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
}- Proposed as answer by Jose Anton Bautista Monday, June 15, 2009 9:35 AM
Monday, June 15, 2009 9:34 AM -
Hai jose,
My second problem still continues . The background image display only when any refresh(add new slide/minimize/maximize)
And
my first problem , How can i take slides left,right,top and bottom.
Thanks
DilleepMonday, June 15, 2009 9:48 AM -
As I've mentioned earlier, I edited my first code to answer your first problem (the one regarding the slide's left, top, width, height).
The left and top parameters correspond to the position of the image on the slide; in this case, they are both zero (positioned at the upper-left corner).
To address your second problem, you might want to hook up my other code to the Application.WindowActivate and Application.PresentationNewSlide events
to determine the visibility of the background image.Monday, June 15, 2009 10:51 AM -
Hai Jose,
Ok My first problem is solved.
But the second problem still continue. Here i checked the visibility of background image .it is true.
The interest thing is , The background show the left pane (slide master) and not dispaly in Central pane.If i minimize the powerpoint (any Refresh method), the the central pane will show the corresponding backgrounds.
Thanks
DileepMonday, June 15, 2009 11:32 AM -
Oh ok, now I get what you mean. : )
I'm not sure if there's a more elegant way to do this, but you can try the following after adding the image to the background:
Globals.ThisAddIn.Application.CommandBars["Thumbnails"].Controls["&Reset Slide"].Execute();- Proposed as answer by Jose Anton Bautista Monday, June 15, 2009 3:10 PM
- Marked as answer by Dileep sankar Tuesday, June 16, 2009 4:37 AM
- Unmarked as answer by Dileep sankar Thursday, July 30, 2009 8:32 AM
Monday, June 15, 2009 3:10 PM -
Hai Jose,
Thank you,
But i have some doubt about the above code.
What is ["Thumbnails"] ?
What is ["&Reset Slide"].
How it can be fetched?
Thanks
DileepTuesday, June 16, 2009 4:17 AM -
Hi Dileep!
If you would right-click any slide on the left pane (list of slides under the Normal view), you would see a popup menu.
This is the "Thumbnails" command bar.
This command bar has an item called "Reset Slide", which you need in order to update the current slide with your background image.
To execute the command that this item represents, you simply call:
Globals.ThisAddIn.Application.CommandBars["Thumbnails"].Controls["&Reset Slide"].Execute();- Marked as answer by Dileep sankar Tuesday, June 16, 2009 5:28 AM
- Unmarked as answer by Dileep sankar Thursday, July 30, 2009 10:01 AM
- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:52 AM
Tuesday, June 16, 2009 4:32 AM -
hai Jose,
Can i find my background image or other background image by using code.
Thanks
DileepTuesday, June 16, 2009 8:43 AM -
hai Jose,
Please give me the reply.
Thanks
DileepWednesday, June 17, 2009 3:46 AM -
Hi!
I think you opened a new thread for this issue. I'll just reply in that thread.Wednesday, June 17, 2009 3:56 AM -
Hai,
The above code set the background in all slide. But i want to apply the backgrounds selected slide only.
Thanks
DileepWednesday, July 29, 2009 8:57 AM -
Hai Jose,
If
Globals.ThisAddIn.Application.CommandBars["Thumbnails"].Controls["&Reset Slide"].Execute(); is used in powerpoint 2003.
It will get exception. My application is both for powerpoint 2003 and powerpoint 2007.
Thanks
DileepFriday, August 7, 2009 4:20 AM -
Hai Jose,
If
Globals.ThisAddIn.Application.CommandBars["Thumbnails"].Controls["&Reset Slide"].Execute(); is used in powerpoint 2003.
It will get exception. My application is both for powerpoint 2003 and powerpoint 2007.
Thanks
Dileep
Try the following code instead:
Globals.ThisAddIn.Application.CommandBars.ExecuteMso("SlideReset");
Hai,
The above code set the background in all slide. But i want to apply the backgrounds selected slide only.
Thanks
Dileep
Then use the Slides collection instead of the SlideMaster:
int indexOfSelectedSlide = 1;
presentation.Slides[indexOfSelectedSlide].Shapes.AddPicture(...- Proposed as answer by Jose Anton Bautista Friday, August 7, 2009 6:15 AM
Friday, August 7, 2009 6:15 AM -
hai Jose,
The code given below is not add the backgroundImage of slide. If we use the below code, it fit as slide . But it can be resized.The important thing is , we cant drag the slide background.
int indexOfSelectedSlide = 1;
presentation.Slides[indexOfSelectedSlide].Shapes.AddPicture(...
Thanks
DileepFriday, August 7, 2009 11:35 AM -
How about the following code?
object indexOfSelectedSlide = 1;
PowerPoint.SlideRange slideRange = presentation.Slides.Range(indexOfSelectedSlide);
slideRange.FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
slideRange.Background.Fill.UserPicture(@"C:\BackgroundImage.jpg");- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:52 AM
Friday, August 7, 2009 12:42 PM -
hai Jose ,
It is working fine the selected slide only. But i have one more problems. Now i have two options,
1) apply backgrounds all slides.
2) apply backgrouns selected slide only.
For this , i used the code alredy sent you. My problem is I apply backgrounds to all slide with one image and then selected only one slide and apply backgrounds to selected slide. In this case the previous background image is not changed. If the slide has no background image, then apply background image to selected slide , now it is working. What is the problem now?
Thanks
Dileep
Monday, August 10, 2009 6:40 AM -
I haven't experienced the problem that you're getting.
It works fine on my machine.
May I see your code?
Does this problem happen on both Powerpoint 2003 and 2007?Monday, August 10, 2009 9:12 AM -
hai jose,
apply all backgrounds , i wrote code below,
PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
float slideHeight = presentation.PageSetup.SlideHeight;
float slideWidth = presentation.PageSetup.SlideWidth; presentation.SlideMaster.Shapes.AddPicture(
@"C:\BackgroundImage.jpg",
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, slideWidth, slideHeight);
Globals.ThisAddIn.Application.CommandBars["Thumbnails"].Controls["&Reset Slide"].Execute();
And
Apply image to as selected Background,i wrote,
bject indexOfSelectedSlide = 1;
PowerPoint.SlideRange slideRange = presentation.Slides.Range(indexOfSelectedSlide);
slideRange.FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
slideRange.Background.Fill.UserPicture(@"C:\BackgroundImage.jpg");
The above two code written in Two button events.
Thanks
DileepMonday, August 10, 2009 10:55 AM -
I see. The problem is in your first code.
When you insert the image, it is being inserted as an actual Shape object, not a background.
Your second code works but the image inserted earlier is covering the new background hence you can't see the background.
Try the following for setting the background for all slides:
presentation.SlideMaster.Background.Fill.UserPicture(@"C:\BackgroundImage.jpg");- Marked as answer by Cindy Meister MVP Tuesday, August 11, 2009 7:52 AM
Monday, August 10, 2009 11:23 AM -
hai Jose,
I have one more problems.
i have three slide. Firstly add background to all slide. It is working . And then i selected the only one slide. And Apply background image to slected slide. Now it is working. Again i add the Background image to all slide , The previous seleced slide
Background image is not change.?
Thanks
DileepMonday, August 10, 2009 12:34 PM -
That's because you set the FollowMasterBackground property of the PowerPoint.SlideRange object to false.
Hence, setting again the background of the Slide Master would not affect those selected slides.
You should set that property back to true.
presentation.SlideMaster.Background.Fill.UserPicture(@"C:\BackgroundImage.jpg");
for (int i = 1; i <= presentation.Slides.Count; i++)
{
presentation.Slides.Range(i).FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoTrue;
}- Marked as answer by Dileep sankar Tuesday, August 11, 2009 6:30 AM
- Unmarked as answer by Dileep sankar Wednesday, August 19, 2009 9:55 AM
Tuesday, August 11, 2009 3:04 AM -
Hai Jose,
Tahnk You Very Much. Now it is working . But I have one or more requirments.
I want to apply an image to all Templates and selected slide.
Thanks
Dileep
- Marked as answer by Dileep sankar Tuesday, August 11, 2009 6:30 AM
- Unmarked as answer by Dileep sankar Tuesday, August 11, 2009 6:30 AM
Tuesday, August 11, 2009 4:30 AM -
Do you need further help on that one?
The process is the same when working with templates.
I am not sure what your requirements are, but I suggest you mark this thread as 'Answered'
and open a new thread if the functionality you want is different from what was discussed in this thread.
It would be easier to follow up our discussion.Tuesday, August 11, 2009 5:19 AM -
Hai Jose,
I have one or more requirements,
I have 4 slides, i selected two slide only, I want to apply the backgrounds only selected two slide. How can i do ?
thanks
DileepWednesday, August 19, 2009 4:05 AM