如何使用基于图片的形状创建图片框

本文关键字:创建 何使用 于图片 | 更新日期: 2023-09-27 18:34:27

问题:我在白色背景上有物体的图片。我需要具有这些对象的确切形状的图片框,但我不知道这些对象看起来像先验的。

如何使用基于图片的形状创建图片框

我的解决方案是一个新类:

class ShapedPictureBox : PictureBox
{
    public ShapedPictureBox()
    {
    }
    public Color transparentColor = Color.White;
    public void updateShape()
    {
    if(this.Image = null) return;
    Bitmap bitmap = new Bitmap(this.Image);
    System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
    for(int x = 0; x < this.Image.Width; x++)
        for(int y = 0; y < this.Image.Height; y++)
            if(transparentColor != bitmap.GetPixel(x, y))
                graphicsPath.AddRectangle(new Rectangle(new Point(x, y), new Size(1, 1)));
    this.Region = new Region(graphicsPath);
    }
}

每当使对象失效时,都会重新创建形状。我知道这个解决方案根本没有效果,但它是我发现的唯一解决方案。我希望它对某人有所帮助。

如果您有更好/更有效的想法,请告诉我。