如何基于图片创建具有形状的PictureBox [英] How to create PictureBoxes with shapes based on a Picture
本文介绍了如何基于图片创建具有形状的PictureBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题: 我在白色背景上有物体的图片. 我需要具有这些对象确切形状的PictureBox,但是我不知道这些对象的外观如何.
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.
推荐答案
我的解决方案是一个新类:
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.
这篇关于如何基于图片创建具有形状的PictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文