我如何通过给定一个 int 值而不是一直更改所有值来更改图像大小?
How can I change the image size with giving one single int value instead changing all the time all the values?
private Image GetIcon(bool nodeIsExpanded)
{
if (openedImage == null)
InitImage();
return nodeIsExpanded ? openedImage : closedImage;
}
在 InitImage 中,我在本例中设置了三角形的大小:
private void InitImage()
{
openedImage = new Bitmap(16, 16);
closedImage = new Bitmap(16, 16);
using (Brush b = new SolidBrush(ArrowColor))
{
using (Graphics g = Graphics.FromImage(openedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(15, 0), new Point(8, 15), });
using (Graphics g = Graphics.FromImage(closedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(15, 8), new Point(0, 15), });
}
}
我想保持三角形的形状,但更小或更大地改变它的大小更容易。所以我想使方法类似于:
private void InitImage(int size)
仅更改大小更容易。
这是我在 TreeView 节点中使用它的位置和方式:
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
Color backColor = (GetTopNodeIndex(e.Node) & 1) == 0 ? BackColor : AlternateBackColor;
using (Brush b = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(b, new Rectangle(0, e.Bounds.Top, ClientSize.Width, e.Bounds.Height));
}
if ((e.State & TreeNodeStates.Selected) != 0)
{
e.Graphics.FillRectangle(Brushes.Green, e.Bounds);
}
// icon
if (e.Node.Nodes.Count > 0)
{
Image icon = GetIcon(e.Node.IsExpanded);
e.Graphics.DrawImage(icon, e.Bounds.Left - icon.Width - 3, e.Bounds.Top);
}
// text (due to OwnerDrawText mode, indenting of e.Bounds will be correct)
TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, ForeColor);
// indicate selection (if not by backColor):
if ((e.State & TreeNodeStates.Selected) != 0)
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
}
试试这个:
private void InitImage(int size)
{
openedImage = new Bitmap(size, size);
closedImage = new Bitmap(size, size);
using (Brush b = new SolidBrush(ArrowColor))
{
using (Graphics g = Graphics.FromImage(openedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(size - 1, 0), new Point(size / 2, size - 1), });
using (Graphics g = Graphics.FromImage(closedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(size - 1, size / 2), new Point(0, size - 1), });
}
}
private Image GetIcon(bool nodeIsExpanded)
{
if (openedImage == null)
InitImage();
return nodeIsExpanded ? openedImage : closedImage;
}
在 InitImage 中,我在本例中设置了三角形的大小:
private void InitImage()
{
openedImage = new Bitmap(16, 16);
closedImage = new Bitmap(16, 16);
using (Brush b = new SolidBrush(ArrowColor))
{
using (Graphics g = Graphics.FromImage(openedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(15, 0), new Point(8, 15), });
using (Graphics g = Graphics.FromImage(closedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(15, 8), new Point(0, 15), });
}
}
我想保持三角形的形状,但更小或更大地改变它的大小更容易。所以我想使方法类似于:
private void InitImage(int size)
仅更改大小更容易。
这是我在 TreeView 节点中使用它的位置和方式:
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
Color backColor = (GetTopNodeIndex(e.Node) & 1) == 0 ? BackColor : AlternateBackColor;
using (Brush b = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(b, new Rectangle(0, e.Bounds.Top, ClientSize.Width, e.Bounds.Height));
}
if ((e.State & TreeNodeStates.Selected) != 0)
{
e.Graphics.FillRectangle(Brushes.Green, e.Bounds);
}
// icon
if (e.Node.Nodes.Count > 0)
{
Image icon = GetIcon(e.Node.IsExpanded);
e.Graphics.DrawImage(icon, e.Bounds.Left - icon.Width - 3, e.Bounds.Top);
}
// text (due to OwnerDrawText mode, indenting of e.Bounds will be correct)
TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, ForeColor);
// indicate selection (if not by backColor):
if ((e.State & TreeNodeStates.Selected) != 0)
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
}
试试这个:
private void InitImage(int size)
{
openedImage = new Bitmap(size, size);
closedImage = new Bitmap(size, size);
using (Brush b = new SolidBrush(ArrowColor))
{
using (Graphics g = Graphics.FromImage(openedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(size - 1, 0), new Point(size / 2, size - 1), });
using (Graphics g = Graphics.FromImage(closedImage))
g.FillPolygon(b, new[] { new Point(0, 0), new Point(size - 1, size / 2), new Point(0, size - 1), });
}
}