我试图放置一个没有明确形状的图像(例如一顶帽子)在不同的图像控件上。 问题在于,由于控件具有明确的形状,因此它会保留默认的背景色以覆盖空白处留下的空白。图像控件与图像的大小完全相同。 我试过使用control.BackColor = Color.Transparent;但它似乎并不奏效。 还有其他建议吗?是否可以创建一个非形状的控件?
回答
您可以使用Control.Region
用于此目的
GraphicsPath path = new GraphicsPath();
path.AddEllipse(control.ClientRectangle);
control.Region = new Region(path);
试试这个,你可以使用GraphicsPath
创建任何形状并将其设置为Region
比如我创建椭圆。
编辑
如果你只是想设置背景色= Color.Transparent。出于某种原因,某些控件不允许这样做。在这种情况下,你可以做以下
public class CustomControl1 : Control
{
public CustomControl1()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
}
创建控制的后裔,并设置this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
应该做的伎俩
非常规形状怎么样?像这样:http://img2-2.timeinc.net/people/i/2013/pets/news/130218/cat-monopoly-600.jpg –
如果该图像具有透明背景,则可以将控件的“BackGroundImage”属性和设置Control.BackColor = Color.Transparent; –
为什么Color.Transparent对你不起作用? –
如果您Image Control
(如PictureBox
)不移动(通过按住鼠标下来,在运行时由用户拖动),您可以使用这种技术,使您可以在彼此之上显示图像。这些图像应该有透明的背景:
public class ImageControl : Control {
public ImageControl(){
SetStyle(ControlStyles.Opaque, true);
}
public Image Image {get;set;}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e){
if(Image != null) e.Graphics.DrawImage(Image, Point.Empty);
}
}
您可以使用上面,而不是PictureBox
控制。在运行时拖动此控件会导致闪烁很多。所以如果你想,所以我认为只有1个解决方案使用Region
。在这种方法中,您必须将您的Bitmap
设置为Region
,并为您的Control.Region
属性分配此Region
。 Chris Dunaway
给出的链接对你来说很有帮助。不过,我不得不说,Region
并没有像你期望的那样平滑的边界。这是缺乏这种方法。为方便起见,我会稍加修改后的代码在这里,该代码使用LockBits
它的表现将优于原代码:
public class Util {
//invert will toggle backColor to foreColor (in fact, I mean foreColor here is the Solid Color which makes your image distinct from the background).
public static Region RegionFromBitmap(Bitmap bm, Color backColor, bool invert)
{
Region rgn = new Region();
rgn.MakeEmpty();//This is very important
int argbBack = backColor.ToArgb();
BitmapData data = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int[] bits = new int[bm.Width * bm.Height];
Marshal.Copy(data.Scan0, bits, 0, bits.Length);
//
Rectangle line = Rectangle.Empty;
line.Height = 1;
bool inImage = false;
for (int i = 0; i < bm.Height; i++)
{
for (int j = 0; j < bm.Width; j++)
{
int c = bits[j + i * bm.Width];
if (!inImage)
{
if (invert ? c == argbBack : c != argbBack)
{
inImage = true;
line.X = j;
line.Y = i;
}
}
else if(invert ? c != argbBack : c == argbBack)
{
inImage = false;
line.Width = j - line.X;
rgn.Union(line);
}
}
}
bm.UnlockBits(data);
return rgn;
}
}
//Use the code
//if your Bitmap is a PNG with transparent background, you can get the Region from it like this:
Region rgn = Util.RegionFromBitmap(yourPng, Color.FromArgb(0), false);
//if your Bitmap has a figure with solid color of Black, you can get the Region like this:
Region rgn = Util.RegionFromBitmap(yourPng, Color.Black, true);
- 1. 是否可以在CSS3中创建这个形状?
- 2. 是否可以为非dotnet DLL创建一个Nuget包?
- 3. 是否可以使用图形API创建一个Facebook页面?
- 4. 创建一个cpShape时它是否也为该形状创建一个物体?
- 5. 是否可以在HTML中创建多边形形状的元素?
- 6. 是否可以在JavaFX中创建一个控制器数组?
- 7. 是否可以改变“UISlider”的形状?
- 8. 创建一个形状文件
- 9. 创建一个形状文件:ggplot2
- 10. d3可以在一个形状内建立形状吗?
- 11. 你可以在css中创建非对称形状
- 12. 是否可以创建一个Web服务来创建一个XML文件?
- 13. 是否可以创建一个可用于多个表控制器的tableViewCell?
- 14. 是否可以创建一个无法复制的文件?
- 15. 是否可以从我的dbml文件创建一个脚本?
- 16. 如何找出一个形状是否可以通过
- 17. 是否有可能创建一个并非绝对的Uri?
- 18. iOS:是否可以创建定制的xib控件模板?
- 19. 是否可以在MediaLibrary中创建一个新文件夹?
- 20. 是否可以从base64字符串创建一个pdf文件?
- 21. 使用streamwriter,是否可以从word.doc创建一个html文件?
- 22. 是否可以使用数据点的形状在matplotlib侧边栏中创建?
- 23. 是否可以创建一个用于StateListDrawable的自定义状态?
- 24. 是否可以将控件锚定到另一个控件?
- 25. 是否可以将控件放入另一个控件中?
- 26. 是否可以在Robot Framework中创建一个非常简单的for循环?
- 27. 是否可以圆形?六边形,八边形等形状?
- 28. 是否有可能在android代码中创建形状
- 29. 如何创建从一个形状到另一个形状的变形效果?
- 30. 是否可以在Java Monkey Engine中创建一个平坦的地形?
是图像本身透明?例如,PNG图像支持透明颜色。这对你的工作很重要... –
编辑你的标题。请阅读:http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles – RahulD
我相信这是WinForms,所以你可能想在你的标签中指定这个(相反也许是背景或图片框)。我原本打算在注意到'BackColor'之前回答WPF。 –