【问题标题】:Save PointF[] shape as image将 PointF[] 形状保存为图像
【发布时间】:2017-08-06 22:57:36
【问题描述】:

我有一个用户控件制作一个PointF[] 三角形(用作轨迹栏的滑块)。

double hypotenuse;
double finalPoint;

public PointF Target { get; set; }
public PointF PointB { get; set; }
public PointF PointC { get; set; }

public PointF[] triangle = { new Point(0, 0), new Point(0, 0), new Point(0, 0) };

public TriangleSliderUC()
{
    InitializeComponent();
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.OptimizedDoubleBuffer, true);  

    PropertyConstructor();
    PointConstructor();

    Paint += new PaintEventHandler(TriangleSliderUC_Paint);
}

public void PointConstructor()
{
    Target = new PointF((int)finalPoint, (int)(hypotenuse * 0.5f));
    PointB = new PointF(0, 0);
    PointC = new PointF(0, (int)hypotenuse);

    triangle[0] = Target;
    triangle[1] = PointB;
    triangle[2] = PointC;
}

void TriangleSliderUC_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillPolygon(Brushes.Orange, triangle);
}

为了将它用作轨迹栏的拇指,我想将创建的三角形保存为位图,这样它就不必在每次移动时都重新绘制。

我该怎么做?

另外,有没有更好的方法来构建我的代码?我是 C# 新手,对 OOP 不熟悉。

【问题讨论】:

  • 在做一个需要绘图的自定义控件时,我通常继承自PictureBox。绘制一次图像(或在属性更改时重绘),然后将其设置为 Image 属性。不需要自定义 Paint 事件。
  • @James 感谢您的提示!虽然我仍然不完全理解那是什么或我将如何去做。您能否添加答案说明或评论链接以在途中阅读更多内容?

标签: c# image bitmap polygon


【解决方案1】:

这是一个简单的示例,使用我可以看到的您当前的代码。它可能不能直接替代您已经开发的内容,但应该会给您一些想法。

我已经创建了HypotenuseFinalPoint 的公共属性,它们写入一个私有变量,但也调用了更新三角形图像。 UpdateTriangle 创建一个与控件尺寸相同的空白图像,然后像在 PointConstructorPaint 事件中所做的那样绘制三角形。最后,它设置底层控件(图片框)的Image 属性。

public class TriangleSliderUC : PictureBox
{
    public double Hypotenuse
    {
        get { return hypotenuse; }
        set
        {
            hypotenuse = value;
            UpdateTriangle();
        }
    }
    public double FinalPoint
    {
        get { return finalPoint; }
        set
        {
            finalPoint = value;
            UpdateTriangle();
        }
    }

    private PointF[] triangle = { new Point(0, 0), new Point(0, 0), new Point(0, 0) };
    private double hypotenuse;
    private double finalPoint;

    public TriangleSliderUC()
    {
        UpdateTriangle();
    }

    public void UpdateTriangle()
    {
        triangle[0] = new PointF((int)finalPoint, (int)(hypotenuse * 0.5f));
        triangle[1] = new PointF(0, 0); 
        triangle[2] = new PointF(0, (int)hypotenuse);

        // Create a new image with the current control dimensions
        Bitmap image = new Bitmap(Width, Height);

        // Create a grahics object, draw the triangle
        var g = Graphics.FromImage(image);
        g.FillPolygon(Brushes.Orange, triangle);

        // Update the controls image with the newly created image
        this.Image = image;
    }
}

如果您想缩放以适应可用空间,您可能还想从Resize 事件中添加对UpdateTriangle 函数的调用。

this.Resize += TriangleSliderUC_Resize;

private void TriangleSliderUC_Resize(object sender, EventArgs e)
{
    UpdateTriangle();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2012-01-02
    相关资源
    最近更新 更多