我試圖放置一個沒有明確形狀的圖像(例如一頂帽子)在不同的圖像控件上。 問題在於,由於控件具有明確的形狀,因此它會保留默認的背景色以覆蓋空白處留下的空白。圖像控件與圖像的大小完全相同。 我試過使用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。 –