如何放大在矩形中绘制的影线样式?
How can I scale up the hatchstyle drawn in a rectangle?
我正在使用.Net 绘图来绘制图表。它本质上是一个堆积条形图。
我遇到的问题是我想减少填充样式中的线条数量,因此可以按比例放大以使其更清晰。我环顾四周,但没有发现任何可以帮助我的东西。
我画了一个矩形,然后使用影线刷填充它,但由于图像尺寸的原因,影线填充变得不太清晰。感谢您的任何建议。
hatchStyles 和笔刷类型存储在数据库中,我使用辅助函数 return 它们。所以我画了矩形,在拿到画笔后我填充了矩形。本质上,如果可以的话,我想扩大影线填充。
g.DrawRectangle(gridpen, startX, startY, BOREHOLE_RECT_WIDTH, layerRectHeight);
brush = GetBoreholeBrush(l.SoilTypeMatrixLevel1Id.PrimaryBrushType,
l.SoilTypeMatrixLevel1Id.PrimaryFillStyle,
l.SoilTypeMatrixLevel1Id.PrimaryColour);
g.FillRectangle(brush, startX, startY, BOREHOLE_RECT_WIDTH, layerRectHeight);
以及getBrush函数;画笔类型、填充样式和颜色存储在数据库中,用于创建 returned 画笔:
//===================================
private Brush GetBoreholeBrush(string BrushType, string HatchStyle, string Colour)
//===================================
{
//Decide on what brush type has been chosen.
Brush brush;
if (BrushType.ToLower() == BrushTypes.HatchBrush.ToString().ToLower())
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
else if (BrushType.ToLower() == BrushTypes.SolidBrush.ToString().ToLower())
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
else if (BrushType.ToLower() == BrushTypes.TextureBrush.ToString().ToLower())
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
else
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
return brush;
}
对return填充样式的函数:
//===================================
private HatchStyle GetHatchStyle(string FillStyle)
//===================================
{
//Loop through each hatch tyle and return the correct one.
foreach (HatchStyle style in Enum.GetValues(typeof(HatchStyle)))
{
if (style.ToString().ToLower() == FillStyle.ToLower())
{
return style;
}
}
return HatchStyle.Vertical;
}
正如您在下图中所见,填充样式不清晰。
最直接但可能不是很有用的答案是:不,你不能缩放 HatchBrush
.
的填充图案
它旨在始终在像素级别看起来清晰,甚至不受缩放 Graphics
对象的影响。
看着你的问题我想知道:你确定你真的在使用HatchBrush
吗?您从函数 GetBoreholeBrush
中获取画笔。如果你真的已经将索引存储到 50 HatchStyle 那么我猜你真的使用了 HatchBrush
.
现在使用 HatchBrush
行不通了,我猜你可以使用 TextureBrush
来代替..
您可以通过放大填充图案将其转换为更大的版本;这不完全是一个简单的转换。通过整数因子绘制较大且没有抗锯齿的直接方法很简单并且可能足够好。
但您可能需要对它们进行微调,因为这样所有像素,即 线像素 和 背景像素 都会得到放大并且所有对角线看起来都是锯齿状的。
因此您需要平衡填充尺寸和笔划宽度,并以更大的尺寸从头开始重新创建您需要的所有图案。
这里有一个例子来说明简单解决方案的问题;第一行是原始填充图案,其他是简单的纹理笔刷结果,按 1x、2x 和 3x 缩放..:[=27=]
首先是一个将 HatchBrush
转换为 TextureBrush
的函数
TextureBrush TBrush(HatchBrush HBrush)
{
using (Bitmap bmp = new Bitmap(8,8))
using (Graphics G = Graphics.FromImage(bmp))
{
G.FillRectangle(HBrush, 0, 0, 8, 8);
TextureBrush tb = new TextureBrush(bmp);
return tb;
}
}
请注意填充图案为 8x8
像素。
现在用于上图的 Paint
代码:
private void panel1_Paint(object sender, PaintEventArgs e)
{
var hs = (HatchStyle[])Enum.GetValues(typeof(HatchStyle));
for (int i = 0; i < hs.Length; i++)
using (HatchBrush hbr = new HatchBrush(hs[i], Color.GreenYellow))
using (HatchBrush hbr2 = new HatchBrush(hs[i], Color.LightCyan))
{
e.Graphics.FillRectangle(hbr, new Rectangle(i * 20, 10,16,60));
using (TextureBrush tbr = TBrush(hbr2))
{
e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 80, 16, 60));
tbr.ScaleTransform(2, 2);
e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 150, 16, 60));
tbr.ResetTransform();
tbr.ScaleTransform(3,3);
e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 220, 16, 60));
}
}
}
请注意,虽然 TextureBrush
有很好的修改纹理的方法,但 HatchBrush
完全没有这样的东西..
我正在使用.Net 绘图来绘制图表。它本质上是一个堆积条形图。 我遇到的问题是我想减少填充样式中的线条数量,因此可以按比例放大以使其更清晰。我环顾四周,但没有发现任何可以帮助我的东西。
我画了一个矩形,然后使用影线刷填充它,但由于图像尺寸的原因,影线填充变得不太清晰。感谢您的任何建议。
hatchStyles 和笔刷类型存储在数据库中,我使用辅助函数 return 它们。所以我画了矩形,在拿到画笔后我填充了矩形。本质上,如果可以的话,我想扩大影线填充。
g.DrawRectangle(gridpen, startX, startY, BOREHOLE_RECT_WIDTH, layerRectHeight);
brush = GetBoreholeBrush(l.SoilTypeMatrixLevel1Id.PrimaryBrushType,
l.SoilTypeMatrixLevel1Id.PrimaryFillStyle,
l.SoilTypeMatrixLevel1Id.PrimaryColour);
g.FillRectangle(brush, startX, startY, BOREHOLE_RECT_WIDTH, layerRectHeight);
以及getBrush函数;画笔类型、填充样式和颜色存储在数据库中,用于创建 returned 画笔:
//===================================
private Brush GetBoreholeBrush(string BrushType, string HatchStyle, string Colour)
//===================================
{
//Decide on what brush type has been chosen.
Brush brush;
if (BrushType.ToLower() == BrushTypes.HatchBrush.ToString().ToLower())
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
else if (BrushType.ToLower() == BrushTypes.SolidBrush.ToString().ToLower())
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
else if (BrushType.ToLower() == BrushTypes.TextureBrush.ToString().ToLower())
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
else
{
brush = new HatchBrush(GetHatchStyle(HatchStyle),
Color.Black, ColorTranslator.FromHtml(Colour));
}
return brush;
}
对return填充样式的函数:
//===================================
private HatchStyle GetHatchStyle(string FillStyle)
//===================================
{
//Loop through each hatch tyle and return the correct one.
foreach (HatchStyle style in Enum.GetValues(typeof(HatchStyle)))
{
if (style.ToString().ToLower() == FillStyle.ToLower())
{
return style;
}
}
return HatchStyle.Vertical;
}
正如您在下图中所见,填充样式不清晰。
最直接但可能不是很有用的答案是:不,你不能缩放 HatchBrush
.
它旨在始终在像素级别看起来清晰,甚至不受缩放 Graphics
对象的影响。
看着你的问题我想知道:你确定你真的在使用HatchBrush
吗?您从函数 GetBoreholeBrush
中获取画笔。如果你真的已经将索引存储到 50 HatchStyle 那么我猜你真的使用了 HatchBrush
.
现在使用 HatchBrush
行不通了,我猜你可以使用 TextureBrush
来代替..
您可以通过放大填充图案将其转换为更大的版本;这不完全是一个简单的转换。通过整数因子绘制较大且没有抗锯齿的直接方法很简单并且可能足够好。
但您可能需要对它们进行微调,因为这样所有像素,即 线像素 和 背景像素 都会得到放大并且所有对角线看起来都是锯齿状的。
因此您需要平衡填充尺寸和笔划宽度,并以更大的尺寸从头开始重新创建您需要的所有图案。
这里有一个例子来说明简单解决方案的问题;第一行是原始填充图案,其他是简单的纹理笔刷结果,按 1x、2x 和 3x 缩放..:[=27=]
首先是一个将 HatchBrush
转换为 TextureBrush
TextureBrush TBrush(HatchBrush HBrush)
{
using (Bitmap bmp = new Bitmap(8,8))
using (Graphics G = Graphics.FromImage(bmp))
{
G.FillRectangle(HBrush, 0, 0, 8, 8);
TextureBrush tb = new TextureBrush(bmp);
return tb;
}
}
请注意填充图案为 8x8
像素。
现在用于上图的 Paint
代码:
private void panel1_Paint(object sender, PaintEventArgs e)
{
var hs = (HatchStyle[])Enum.GetValues(typeof(HatchStyle));
for (int i = 0; i < hs.Length; i++)
using (HatchBrush hbr = new HatchBrush(hs[i], Color.GreenYellow))
using (HatchBrush hbr2 = new HatchBrush(hs[i], Color.LightCyan))
{
e.Graphics.FillRectangle(hbr, new Rectangle(i * 20, 10,16,60));
using (TextureBrush tbr = TBrush(hbr2))
{
e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 80, 16, 60));
tbr.ScaleTransform(2, 2);
e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 150, 16, 60));
tbr.ResetTransform();
tbr.ScaleTransform(3,3);
e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 220, 16, 60));
}
}
}
请注意,虽然 TextureBrush
有很好的修改纹理的方法,但 HatchBrush
完全没有这样的东西..