How to set MS Chart LegendItem image size - c#

I use Image from Resources to LegendItem in WinForms
var ImageName = "ImageName";
myChart.Images.Add(new NamedImage(ImageName, Resources.Image));
LegendItem legendItem = new LegendItem();
legendItem.Name = "legend text";
legendItem.Image = ImageName;
myChart.Legends[Legend.Name].CustomItems.Add(legendItem);
But the size of Image is too small.
How can I change it?

You should use custom LegendCell in this case. This means you define the cells for your LegendItem specifying their properties. Something like this:
LegendItem legendItem = new LegendItem();
LegendCell cell1 = new LegendCell();
cell1.Name = "cell1";
cell1.Text = "legend text";
// here you can specify alignment, color, ..., too
LegendCell cell2 = new LegendCell();
cell2.Name = "cell2";
cell2.CellType = System.Windows.Forms.DataVisualization.Charting.LegendCellType.Image;
cell2.Image = "path of your img";
cell2.Size = new Size(.....);
legendItem.Cells.Add(cell1);
legendItem.Cells.Add(cell2);

Related

C# WPF MenuItem with image as background programmatically

I'm very bad with XAML, but I just need to create an MenuItem, that has an Image instead of Text.
I already got it working in a way, that the image is presented and loaded out of ressources, but on Mouse Over the image is disappearing.
Could you help me to get this done programmatically? In the internet I only find XAML examples but nothing programatically.
Thanks in advance.
var addOnFrameworkMenuItem = new MenuItem();
var image = new Image();
Assembly asm = Assembly.GetExecutingAssembly();
System.IO.Stream imageStream = asm.GetManifestResourceStream("MyProject.Ressources.image.png");
BitmapFrame bmp = BitmapFrame.Create(imageStream);
image.Source = bmp;
ImageBrush ib = new ImageBrush();
ib.ImageSource = bmp;
addOnFrameworkMenuItem.Width = 150;
addOnFrameworkMenuItem.Height = 31;
addOnFrameworkMenuItem.Background = ib;
Thank you Jim,
your hint helped me. For my problem on disappearing image i just needed to put the image into the header:
addOnFrameworkMenuItem.Header = image;
Additionally I found out to set most of the style properties on following way:
// Create a style to apply to the button
Style s = new Style();
s.TargetType = typeof(System.Windows.Controls.MenuItem);
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontSizeProperty, 11.0));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.BackgroundProperty, Brushes.Orange));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.ForegroundProperty, Brushes.Black));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontFamilyProperty, new FontFamily("Arial")));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontWeightProperty,FontWeights.Bold));
Trigger t2 = new Trigger();
t2.Property = System.Windows.Controls.MenuItem.IsMouseOverProperty;
t2.Value = true;
t2.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Violet));
s.Triggers.Add(t2);
addOnFrameworkMenuItem.Style = s;

How do I create a check box in C# using Open XML SDK

I would like to create a certain number of checkbox using Open XML SDK in C#. How would I do that?
Example:
(Checkbox) - Shoes
(Checkbox) - Shirt
The checkbox count also varies. I am reading a template document, then make edits to return. I have something like this so far:
string documentText;
using (StreamReader reader ...)
{
documentText = reader.ReadToEnd();
}
string addClothes = "";
Run newCheckBox = GenerateRun();
foreach(var item in ClothesList)
{
addClothes = item.clothing;
//MY DILEMMA
documentText = documentText.Replace("##clothing##", newCheckBox + addClothes + "NewLine");
}
public Run GenerateRun()
{
Run run1 = new Run() { RsidRunProperties = "004C0D9A", RsidRunAddition = "00850FA5" };
FieldCode fieldCode1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve };
fieldCode1.Text = " FORMCHECKBOX ";
run1.Append(fieldCode1);
return run1;
}
using the OpenXML SDK i think it goes something like this:
(raw copy/paste - the value of -1636166143 might be way off)
using w14 = DocumentFormat.OpenXml.Office2010.Word
SdtRun sdtRun1 = new SdtRun();
SdtProperties sdtProperties1 = new SdtProperties();
SdtId sdtId1 = new SdtId(){ Val = -1636166143 };
W14.SdtContentCheckBox sdtContentCheckBox1 = new W14.SdtContentCheckBox();
W14.Checked checked1 = new W14.Checked(){ Val = W14.OnOffValues.Zero };
W14.CheckedState checkedState1 = new W14.CheckedState(){ Font = "MS Gothic", Val = "2612" };
W14.UncheckedState uncheckedState1 = new W14.UncheckedState(){ Font = "MS Gothic", Val = "2610" };
sdtContentCheckBox1.Append(checked1);
sdtContentCheckBox1.Append(checkedState1);
sdtContentCheckBox1.Append(uncheckedState1);
sdtProperties1.Append(sdtId1);
sdtProperties1.Append(sdtContentCheckBox1);
SdtContentRun sdtContentRun1 = new SdtContentRun();
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
RunFonts runFonts1 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia, Ascii = "MS Gothic", HighAnsi = "MS Gothic", EastAsia = "MS Gothic" };
runProperties1.Append(runFonts1);
Text text1 = new Text();
text1.Text = "☐";
run1.Append(runProperties1);
run1.Append(text1);
sdtContentRun1.Append(run1);
Any text you want after the checkbox, comes after the above code

Underlining and bolding text

how do I get the text to be underlined and bold? My text gets bold but does not get underlined.
Here is some of my code, which is pretty straight forward uses run properties to bold and underline the given text.
Run run_header = para_main.AppendChild(new Run());
RunProperties runProps = new RunProperties();
Bold bold = new Bold();
Underline ul = new Underline();
runProps.Append(bold);
runProps.Append(ul);
run_header.AppendChild(new RunProperties(runProps));
//run_header.AppendChild(new RunProperties(new Bold(), new Underline()));
string username = form.Username;
string proces_header = form.HeaderTitle;
run_header.AppendChild(new Text(proces_header + " | " + username));
run_header.AppendChild(new Break());
Try to set Underline Val property to Single like this:
new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }

ItextSharp (Itext) - set custom font for paragraph

I am trying to set custom font to Paragraph, but I can't make it work.
I tried setting .Font= , but it only works size-wise, but it ignores font. Could you please assist?
Paragraph T = new Paragraph(newTempLine);
iTextSharp.text.Font contentFont = iTextSharp.text.FontFactory.GetFont("Webdings", 12, iTextSharp.text.Font.NORMAL);
T.Font = contentFont;
myDocument.Add(T);
Set it in the constructor:
Font contentFont = FontFactory.GetFont(…);
Paragraph para = new Paragraph(newTempLine, contentFont);
Check if below works:
string name = "Century Gothic Bold";
if (!FontFactory.IsRegistered(name))
{
string systemRoot = Environment.GetEnvironmentVariable("SystemRoot");
string path = Path.Combine(systemRoot, "fonts", #"GOTHICB.TTF");
FontFactory.Register(path);
}
var font = FontFactory.GetFont(name, fontSize, textColor);
var paragraph = new Paragraph(text, font);
Phrase phrase = new Phrase(paragraph);
var myPdfCell = new PdfPCell(phrase);

Two CellValues with Different Styles in a cell in OPEN XML SDK 2.0

Im tasked to use OPEN XML SDK 2.0 and had encountered this problem. Is it possible to have different styles for a single CellValue inside a cell something like the picture below:
A: The plain text
B: Bold and Underlined
NOTE: I need both in a single cell only thanks :)
Yes that is possible. One way is to format the value that will be inserted into the SharedStringTable. This snippet will create your example above:
// Creates an SharedStringItem instance and adds its children.
public SharedStringItem GenerateSharedStringItem()
{
SharedStringItem sharedStringItem1 = new SharedStringItem();
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
Bold bold1 = new Bold();
Underline underline1 = new Underline();
FontSize fontSize1 = new FontSize(){ Val = 11D };
Color color1 = new Color(){ Theme = (UInt32Value)1U };
RunFont runFont1 = new RunFont(){ Val = "Calibri" };
FontFamily fontFamily1 = new FontFamily(){ Val = 2 };
FontScheme fontScheme1 = new FontScheme(){ Val = FontSchemeValues.Minor };
runProperties1.Append(bold1);
runProperties1.Append(underline1);
runProperties1.Append(fontSize1);
runProperties1.Append(color1);
runProperties1.Append(runFont1);
runProperties1.Append(fontFamily1);
runProperties1.Append(fontScheme1);
Text text1 = new Text();
text1.Text = "Project Name:";
run1.Append(runProperties1);
run1.Append(text1);
Run run2 = new Run();
RunProperties runProperties2 = new RunProperties();
FontSize fontSize2 = new FontSize(){ Val = 11D };
Color color2 = new Color(){ Theme = (UInt32Value)1U };
RunFont runFont2 = new RunFont(){ Val = "Calibri" };
FontFamily fontFamily2 = new FontFamily(){ Val = 2 };
FontScheme fontScheme2 = new FontScheme(){ Val = FontSchemeValues.Minor };
runProperties2.Append(fontSize2);
runProperties2.Append(color2);
runProperties2.Append(runFont2);
runProperties2.Append(fontFamily2);
runProperties2.Append(fontScheme2);
Text text2 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
text2.Text = " ALLAN";
run2.Append(runProperties2);
run2.Append(text2);
sharedStringItem1.Append(run1);
sharedStringItem1.Append(run2);
return sharedStringItem1;
}
You can insert that into the SharedStringTable and then set the cell value to be the index in the SharedStringTable where this was inserted.
There might be some other references that I forgot to include that might be defined in the StylesPart. I recommend creating this example in a blank Excel document and then using the Open XML Productivity Tool to look at the XML. The tool will also supply you with the code I provided you above. It should give you a general direction on where to go next.

Categories