How to create PictureBoxes with shapes based on a Picture

做~自己de王妃 提交于 2019-12-13 11:24:51

问题


Problem: I have pictures of objects on a white background. I need PictureBoxes that do have the exact shape of these objects, but I do not know how these objects look like a priori.


回答1:


My solution to this is a new class:

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);
    }
}

whenever you invalidate the object the shape will be recreated. I am aware that this solution is not effective at all, but it was the only I found.. I hope it helps someone..

If you have better/more efficient ideas, please let me know.



来源:https://stackoverflow.com/questions/27530407/how-to-create-pictureboxes-with-shapes-based-on-a-picture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!