我需要在WinForms上繪製透明PNG圖像。我有一個基類:在WinForms上繪製透明圖像
public abstract class BaseSkinable : Panel
{
protected BaseSkinable()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
protected abstract void OnDraw(Graphics graphics);
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e) { }
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
OnDraw(e.Graphics);
}
}
在繼承的類:
protected override void OnDraw(Graphics graphics)
{
Image toDraw = SelectImageToDraw();
if (toDraw == null)
{
NoImageDraw(graphics);
return;
}
int width = toDraw.Size.Width;
int height = toDraw.Size.Height;
Rectangle rect = new Rectangle(0, 0, width, height);
graphics.DrawImage(toDraw, rect);
}
我需要重畫的圖像,如果用戶移動鼠標移到控制。但問題是反對繪製覆蓋舊圖像。就像我們畫圖層一樣。 Winforms可能不清晰圖形和我的方法吸引了過去的圖片。如何解決它,可能我做錯了什麼。
所以,你想繪製一個PNG圖像到你的面板控制? – 2014-08-28 20:17:42
我有一個透明的PNG圖像。我需要製作一個「ImageButton」 - 控制鼠標進入,離開和點擊時重繪圖像的方式。所以問題是這個ImageButton需要透明且沒有邊框。 – user2598575 2014-08-30 05:54:06
你想在一個按鈕上繪製一個透明的圖像。按鈕後面是「表格」? 「形式」也是不透明的? – 2014-08-30 08:38:09