C# 如何绘制正方形并动态指定颜色属性(PDFsharp)?
我正在使用PDFsharp导出图表图例。我使用for循环和表来显示属性。实际上只有一个属性:细分名称。对象具有要使用的正确RGB颜色。如何在图表图例显示序列时在其旁边绘制正方形?请原谅我的变量名,我只是举个例子C# 如何绘制正方形并动态指定颜色属性(PDFsharp)?,c#,pdfsharp,C#,Pdfsharp,我正在使用PDFsharp导出图表图例。我使用for循环和表来显示属性。实际上只有一个属性:细分名称。对象具有要使用的正确RGB颜色。如何在图表图例显示序列时在其旁边绘制正方形?请原谅我的变量名,我只是举个例子 //Writting Table Header Text textformater.DrawString(" Color", tableheader, XBrushes.Black, snoColumn); textformater.DrawString(" Subdivision Na
//Writting Table Header Text
textformater.DrawString(" Color", tableheader, XBrushes.Black, snoColumn);
textformater.DrawString(" Subdivision Name", tableheader, XBrushes.Black, snoStudentName);
foreach (var item in data)
{
y = y + 30;
XRect snoColumnVal = new XRect(35, y, 70, height);
XRect snoStudentNameVal = new XRect(100, y, 250, height);
textformater.DrawString(item.Color, tableheader, XBrushes.Black, snoColumnVal);
textformater.DrawString(item.Name, tableheader, XBrushes.Black, snoStudentNameVal);
}
以下是输出:
这就是我需要它的样子
若要绘制正方形,请使用
DrawRectangle
而不是DrawString
示例代码可在此处找到:图形示例也包含在PDFsharp源程序包中。只需按如下方式绘制正方形:
// Create solid brush.
SolidBrush blueBrush = new SolidBrush(Color.Blue);
// Create rectangle.
Rectangle rect = new Rectangle(0, 0, 200, 200);
// Fill rectangle to screen.
e.Graphics.FillRectangle(blueBrush, rect);
以下是设置颜色的方式:
// Create a green color using the FromRgb static method.
Color myRgbColor = new Color();
myRgbColor = Color.FromRgb(0, 255, 0);
return myRgbColor;
参考:我在演示中使用了这个结构:
struct RowItem
{
public int R, G, B;
public string Text;
}
以下是我的测试数据:
var data = new[]
{
new RowItem{R = 255, G = 0, B = 0, Text = "Red row"},
new RowItem{R = 0, G = 255, B = 0, Text = "Green row"},
new RowItem{R = 255, G = 255, B = 0, Text = "Yellow row"},
new RowItem{R = 0, G = 0, B = 255, Text = "Blue row"},
new RowItem{R = 255, G = 0, B = 255, Text = "Purple row"},
new RowItem{R = 0, G = 255, B = 255, Text = "Cyan row"},
new RowItem{R = 0, G = 0, B = 0, Text = "Black row"}
};
下面是绘制图形的代码:
foreach (var item in data)
{
y = y + 30;
XRect snoColumnVal = new XRect(35, y, 60, 25);
XRect snoStudentNameVal = new XRect(100, y, 250, 25);
var brush = new XSolidBrush(XColor.FromArgb(255, item.R, item.G, item.B));
gfx.DrawRectangle(XPens.Black, brush, snoColumnVal);
textformater.DrawString(item.Text, font, XBrushes.Black, snoStudentNameVal);
}
R、 G和B是颜色成分(范围从0到255)。我已经记不清本周有多少类似瓷砖的问题了。大学/学院提交的项目散发着恶臭。。。很遗憾,你自己没有很好地完成这项任务,谁知道你可能真的很擅长。我已经试过了,但我不知道如何动态地改变对象的颜色。除非我真的需要帮助,否则我不会悬赏所以我会把颜色代码放在循环中?item.color将替换(0255,0),我假设e.图形将是xGraph.Graphics,如果我有XGraphics xGrap=XGraphics.FromPdfPage(pdfpage);如果是这样的话,那么xGraph就不能在智能感知中拾取图形。这个答案显示了如何使用C#和GDI+在屏幕上绘制矩形。PDFsharp使用的类相似,但略有不同(参见我的答案)代码>而不是
gfx.DrawRectangle(XPens.Black、brush、snoColumnVal)
如果您不想在颜色矩形周围有一个黑色边框。gfx
是XGraphics对象,也是XTextFormatter的构造函数中使用的对象。因此,我需要将rgb(67134215)解析为R=value,G=value,B=value如果颜色值在字符串中,则必须解析它们。