Leonardo
Leonardo

Reputation: 11401

C# and PowerPoint 2010 integration

I'm making "something" (I don't know how to define it yet), and what does is create a pptx presentation and some slides... but I'm facing some problems: I can't change the background color/image of my slides and textboxes... and I can't figure it out... Can anyone help me?

Upvotes: 0

Views: 2137

Answers (3)

Owen
Owen

Reputation: 4407

I was having the same issue until I discovered that you need to set slide.FollowMasterBackground to false:

Slide slide = presentation.Slides.AddSlide(presentation.Slides.Count + 1, layout);
slide.FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
slide.Background.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkBlue);

Upvotes: 2

Leonardo
Leonardo

Reputation: 11401

I figured it out... in order to change the background of a slide u must first call the method Fill.Background() before changing the color/picture!!! tks everyone for the help

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244981

The thing you are probably missing with regards to setting colors of objects on your slides is that COM Interop thinks about colors slightly differently than what you may be used to in the .NET Framework.

In the .NET Framework, we represent colors using the aptly-named Color structure, which encapsulates values for the alpha channel of the color, and its red, green, and blue components. However, COM Interop represents colors as an Integer value, in the format BGR. That means that the component values for red, green, and blue are actually stored as blue, green, and red.

However, the .NET Framework provides an easy, built-in way to convert between these two color formats: the ColorTranslator.ToOle and ColorTranslator.FromOle methods. So, you should be able to change the background color of your PowerPoint slide using the following code:

//Create a color
Color myBackgroundColor = Color.LimeGreen;

//Translate to an OLE color
int oleColor = ColorTranslator.ToOle(myBackgroundColor);

//Set the background color of the slide
mySlide.Background.Fill.ForeColor.RGB = oleColor;

Conversely, to retrieve the current background color as a .NET color, you'll have to do the opposite:

//Get the current background color of the slide
int oleColor = mySlide.Background.Fill.ForeColor.RGB;

//Translate to a .NET Color
color myBackgroundColor = ColorTranslator.FromOle(oleColor);

And, of course, if you want to set the foreground (fill) color of a shape, you can simply set its ForeColor property, like so:

//Create a color
Color myForegroundColor = Color.Aqua;

//Translate to an OLE color
int oleColor = ColorTranslator.ToOle(myForegroundColor );

//Set the foreground color of a shape
myShape.Fill.ForeColor.RGB = oleColor;

Upvotes: 2

Related Questions