the situation:
.net 3.5
c# or vb.net(also tested)
word 2007 add-in
I'm trying to set the Background Color of the text to a custom rgb color.
The code is the following:
Range r = this.Application.ActiveDocument.Range();
r.Text = "blabla";
r.Font.Shading.BackgroundPatternColor =(WdColor) Color.FromArgb(0, 214, 227,188).ToArgb();
At first it seems to work, except that the color is not the right one. It seems that whenever I set a custom color, it changes it to an existing WdColor constant. Having a look at the doc, it says :
Returns or sets the 24-bit color that's applied to the background of the Shading object. Can be any valid WdColor constant or a value returned by Visual Basic's RGB function.
So, my question is: does anybody has an idea of how it's supposed to work?
Thanx
Use ColorTranslator
Range r = this.Application.ActiveDocument.Range();
r.Text = "blabla";
r.Font.Shading.BackgroundPatternColor =(WdColor)ColorTranslator.ToOle(0, 214, 227,188);
Related
I am using the following code snippet to remove the background from an image.
int[] bgColors = {203,255,240}
ImageMagick.MagickImage image = new ImageMagick.MagickImage("input.jpg")
ImageMagick.MagickImage combineMask= new ImageMagick.MagickImage("mask.jpg")
combineMask.Blur(7, 30);
image.Composite(combineMask, ImageMagick.CompositeOperator.CopyAlpha);
image.Settings.BackgroundColor = new ImageMagick.MagickColor(bgColors[0], bgColors[1], bgColors[2]);
image.Alpha(ImageMagick.AlphaOption.Remove);
It takes the MagickImage image and the MagickImage combineMask and outputs a combined image as linked below.
Original + Mask = Final
Previously, I never needed anything other than a plain white background (which this provides). Now, I would like to be able to set the color of the background. I started by passing a non (255,255,255) set of colors to the settings to image.Settings.BackgroundColor. This did not accomplish anything. I've been trying to investigate the MagickImage library (through their github, they don't really have very complete documentation), but it has remained inscrutable.
How could I get image.Alpha(ImageMagick.AlphaOption.Remove); to replace the removed section with the background color (or a passed color), or otherwise change the color of the background?
Solution
I've figured out a Solution that takes me 90% of the way there.
int[] bgColors = {203,255,240}
ImageMagick.MagickImage image = new ImageMagick.MagickImage("input.jpg")
ImageMagick.MagickImage combineMask= new ImageMagick.MagickImage("mask.jpg")
combineMask.ColorFuzz = new ImageMagick.Percentage(5);
combineMask.Transparent(ImageMagick.MagickColor.FromRgb(255, 255, 255));
combineMask.Opaque(ImageMagick.MagickColor.FromRgb((byte)0, (byte)0, (byte)0),
ImageMagick.MagickColor.FromRgb((byte)bgColors[0], (byte)bgColors[1], (byte)bgColors[2]));
image.Composite(combineMask, ImageMagick.CompositeOperator.Over);
Instead of using the Black/White mask and grabbing alpha channels, I am converting the white part of the mask to transparent, then converting the black part of the mask to the colors contained in the bgColors.
Now, because the formerly white area is now transparent, I cannot blur the image in order to create a smooth transition between mask and picture, which I still need to fix.
How can one access the last-used colour in PowerPoint from within a C# VSTO Add-In?
In the example below, I am referring to the orange colour indicated by the bucket fill tool.
For Posterity
Here is the C# version of the accepted answer:
var shape = Globals.ThisAddIn.Application.ActivePresentation.Slides[1]
.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 0, 0, 100, 100);
shape.Select();
Globals.ThisAddIn.Application.CommandBars.ExecuteMso("ShapeFillColorPicker");
var color = shape.Fill.ForeColor.RGB;
shape.Delete();
Other than prowling the XML, which may or may not disgorge anything useful, I don't know of any way to do this via the object model, but you could draw a rectangle, invoke the button's action to color it, grab the color then delete the rectangle. Here's how you could do it in VBA:
Function GetColorBucketColor() As Long
Dim oSh As Shape
Set oSh = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 0, 0, 100, 100)
oSh.Select
Application.CommandBars.ExecuteMso ("ShapeFillColorPicker")
GetColorBucketColor = oSh.Fill.ForeColor.RGB
oSh.Delete
End Function
In real life it's a bit more complicated than that; the color in the ShapeFill button might be a theme color or it might be a standard RGB color; this will return the RGB value. Setting other shapes to this color would produce the same appearance, but the shapes would no longer follow the theme. Sometimes that matters, sometimes not.
Another potential drawback is that for this to work, something must be selected. That means that you won't be able to do it in an invisible window, and that there'll be a bit of a blip when PPT selects the newly drawn shape.
I have an access color (light yellow, or 153,255,255) but when I try to use this RGB combo in my Windows Form by using such syntax
System.Drawing.Color ly = System.Drawing.Color.FromArgb(153, 255, 255);
this.BackColor = ly;
the windows form does not produce the same color as my Access Form color does. I also tried to use this
this.BackColor = Color.FromName("LightYellow");
to no such success. Did I translate the color incorrectly? Or can I not use the same colors?
EDIT -
#Alex K thank you for your response, I used this website to attempt to convert and that is the RGB color that it gave. Take a look here:
http://www.numberconverter.net/get-computer-data/color-code-converter/from-rgb-to-ms-access-color/
Access colors seems to be represented as BGR, not RGB (You can verify that quickly by inputting the values in MSPaint). You must switch the values of the first and last values.
So you will need to use
System.Drawing.Color ly = System.Drawing.Color.FromArgb(255, 255, 153);
to obtain your light yellow color.
If instead you want to use the literal name, you can use FromKnownColor which will give you an enum of all color names. It's much more error-proof.
this.BackColor = Color.FromKnownColor(KnownColor.LightYellow);
This won't restrict you on the color choice either, according to the doc FromName takes as an input
Valid names are the same as the names of the elements of the
KnownColor enumeration.
My sample code to change the cell color is this. range refers to a cell range.
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
If i try to change the cell color back to white. Im using this.
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
But this erases my cell range grid lines(default color of the lines are grey) and everything becomes white. i.e.only 'range' grid lines becomes white and all other cells have the default excel grid color.
Thanks
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);The problem is occuring because the interior colour of the range of cells is not actually white (it just appears white). Each cell by default is actually transparent (-4142).
So do this when you want to set it back to "normal":
range.Interior.Color = -4142
Edit: Best to use Constants and -4142 is equal to xlNone.
Anonymous Type
I don't understand why this doesn't work for you? Can you please refine your question? I tried the following and it worked just fine..
const Int32 transparent_Color = -4142;
//Set Yellow
var Rng = ActiveSheet.Range["D5:E7"];
Rng.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
//Set transparent (gridlines come back, so long as you haven't got other code that is interacting with your cells)
var Rng = ActiveSheet.Range["D5:E7"];
Rng.Interior.Color = transparent_Color;
Trick: Open Excel > Developer Toolbar > Record Macro > select/highlight range > change backcolor to yellow > change backcolor to white. Now set the Style to Normal.
Stop the Macro from recording.
Then press Alt + F11 to go into VBA Editor and look at the code in Module 1.
Simply convert that code to C# - it is almost the exact same Object Model in C# as it is with VB.
I need to color some Excel cells with my C# application.
The next "must have" is: I have to do this with the infragistics reference.
I was able to paint some cells:
worksheet.Rows[row].Cells[col].CellFormat.FillPatternBackgroundColor = Color.DarkSeaGreen;
but there is a grey pattern which makes the reading of the cells really hard. Is there any solution how I can take those grey patterns away?
I couldn't find any backgroundcolor attribute except this FillPatternBackgroundColor...
Greez Arion
Assuming you're using a version prior to 12.1, set the fill pattern to solid:
worksheet.Rows[row].Cells[col].CellFormat.FillPattern = FillPatternStyle.Solid;
If you're on 12.1, those properties of been marked obsolete in favor of the Fill property:
worksheet.Rows[row].Cells[col].CellFormat.Fill = new CellFillPattern(new WorkbookColorInfo(Color.DarkSeaGreen), null, FillPatternStyle.Solid);
Try this:
worksheet.Rows[row].Cells[col].interior.color = rgb(0,255,0)
or this:
worksheet.Rows[row].Cells[col].interior.colorindex = 3
I know everyone who views this will probably know this, but to add to this answer; to set the color if you only have the RGB using the Infragistics Excel you can do this as well:
worksheet.Rows[row].Cells[col].CellFormat.Fill = CellFill.CreateSolidFill(Color.FromArgb(255, 0, 255, 0))
(Alpha, Red, Green, Blue) I believe the alpha(opacity) it always ignored by excel so don't let that throw you off.
I am using 2015.2.