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?
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;
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);
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
Related
I use the following code to add a slide to the presentation, and then I want to set its background color to the color of one of the slides that are already in the presentation:
presentation.Slides.AddSlide(presentation.Slides.Count + 1 presentation.Slides[presentation.Slides.Count].CustomLayout);
//set the last slide color to the color of the current slide
presentation.Slides[presentation.Slides.Count].Background.Fill.ForeColor.RGB = presentation.Slides[e].Background.Fill.ForeColor.RGB;
presentation.Save();
However, the color does not get set to red. The right hand side has value 255, which is what I want, but the left hand side has 11675 (before and after the line where I set the color). How to fix this?
This solved the probelm:
int slide_color = presentation.Slides[e].Background.Fill.ForeColor.RGB;
presentation.Slides[presentation.Slides.Count].Design.SlideMaster.Background.Fill.ForeColor.RGB = slide_color;
I'm using the Brushes in my wpf application as shown below, what I want to do is to control the colour intensity. So as shown below the brush is set to Green colour. But what I want to do is to control the intensity of the green colour, so that sometimes it can be lighter or thicker, depending on the value I pass it. so if anyone could please advise.
private readonly Pen P = new Pen(Brushes.Green, 6);
You could create your own brush:
private readonly Pen p = new Pen(new SolidColorBrush(Color.FromARGB(...)), 6);
You can then define the color exactly by passing alpha, red, green and blue components to Color.FromARGB.
An easy solution would be to just manipulate the A-value of a green color like so:
int a = yourAValue;
private readonly Pen p = new Pen(new SolidColorBrush(Color.FromARGB(a,0,255,0)), 6);
Now you can change the colors intensity by changing the a-value;
You could use the HSL colour model instead of RGB. RGB is good for displays as it is based upon mixing the three primary colours of light. However, it doesn't fit well with the human model.
HSL's elements are hue, saturation and lightness. This does fit with the way we describe colours in real life. Adjusting the hue changes the colour - keeping the hue the same and modifiying the other elements gives a colour that varies but that a person would see as being related.
Varying the saturation changes the deepness of the colour. A saturation of zero removes the colour and you end up with a greyscale. A higher saturation makes the colour more vivid. The lightness is what it says. A lightness of zero always gives black. A lightness of the maximum value is white. In between the two extremes are your colours.
Unfortunately HSL is not natively supported by WPF / .NET. However, it's quite easy to create your own method to convert between RGB and HSL. This would probably work well within a method or value converter in your case.
I wrote an article about the conversion on my web site. There's also a downloadable sample and source code for the conversion that you can use for free (you just have to promise not to sue me if something goes wrong!). It's at http://www.blackwasp.co.uk/RGBHSL.aspx
Please see this blog post for an HSL Color implementation for WPF, and a XAML markup extension to lighten/darken colors.
I have 3 rectangles that differ from each other in opacity. The base color is same for all of them. For eg: the base color could be Red and the three rectangles would have the following colors: Red, Red (opacity: 55), Red (opacity: 135).
I tried setting the Background Color of the rectangle but it seems I cannot change the transparency of a rectangle. Is there a workaround for this?
Thank you!
It turns out that this is a limitation of reportviewer. Opacity values can be given to only gauges and graphs. Shapes and fonts do not accept opacity values.
A close workaround to this is to add the opacity value to RGB. For example: if I want to generate a color inRGBA as (255,0,0,160) , I should add 255-160=95 to the values of G and B to get a color close to my needs. Therefore the color turns out to be (255,95,95). It is not the exact color but it is close. I tried this for red, blue and green colors since those are the only colors I need.
I hope this helps someone.
I have a TextBox that displays a color as its background color and the background color code in its text. I have set the text color as Black.
The problem is that if the user sets the color as Black then the color code will be unreadable. How do I set the text color programmatically so that it becomes readable when the user selects any color?
You can use negative color for the text:
Color InvertColor(Color sourceColor) {
return Color.FromArgb(255 - sourceColor.R,
255 - sourceColor.G,
255 - sourceColor.B);
}
Any color is guaranteed to be more or less readable on its negative color, so there you go. This is a quick and dirty way to invert a color, you may also want to check the answers for this question: How do I invert a color?
Another option is to add a white halo to the black text. That's what people do in GIS applications to ensure that map labels are readable on top of any surface. The idea of halo effect is to have a thin white border around black text. This way the text is readable whether it's on white background (the border becomes invisible) or on black background (the border outlines the text).
There are multiple tutorials on the topic, like this article or this SO question (with VB.NET sample).
When you have a Color picked out, just assign it to the ForeColor property of your textbox like this:
txtColor.ForeColor = mycolor;
Not working on Gray color.
This code is more usable:
lblCarColor.BackColor = color;
if ((color.B + color.R + color.G) / 3 <= 128)
{
lblCarColor.ForeColor = Color.White;
}
Is it possible to change the background color (white by default) of a zedgraph pane?
I tried changing the background color of the zedgraph element, but it doesn't give any visible result, background is still white:
ZedGraphControl.BackColor = System.Drawing.Color.Black;
And there doesn't seem to exist a Color or BackColor property on ZedGraphControl.GraphPane.
myChart.GraphPane.Chart.Fill.Color = System.Drawing.Color.Black;
You can use
zg.GraphPane.Chart.Fill.Color = SystemColors.ControlText;
to change the background [only] of the chart.
If you want to change the background color the zedgraph except the chart, use
zg.GraphPane.Fill.Color = SystemColors.ControlText;
If you want to change the background color of everything in the zedgraph, use both:
zg.GraphPane.Chart.Fill.Color = SystemColors.ControlText;
zg.GraphPane.Fill.Color = SystemColors.ControlText;
EDIT: I know that you already solved your problem but I made this so if someone searches it, he can solve his problem quickly :)
myChart.GraphPane.Fill.Color = System.Drawing.Color.Black; will create a gradient fill started with black color. If you don't want to use gradient you should use -
myChart.GraphPane.Chart.Fill.Brush = new System.Drawing.SolidBrush(Color.Black);
Hope this will solve your problem.