![](/img/trans.png)
[英]Set a transparent image as a window background while still showing the background color
[英]Draw to an image then render that image to a window with a transparent background?
我試圖繪制圖像,然后使用以下方法將其繪制到應用程序外部的窗口:
using(var g = Graphics.FromHandle(handle))
g.DrawImage(myImage);
以減少繪圖時的閃爍行為。
我知道我可以使用BufferedGraphics
類,但是它具有黑色背景,因此我嘗試先在其上繪制透明圖像,然后再在其上繪制,但是渲染時它仍然具有黑色背景。
有什么方法可以使用BufferedGraphics
或一次在內存中繪制一次,以便可以一次將其繪制為整個圖像(以減少閃爍)以顯示具有透明背景的屏幕嗎?
我假設您正在使用計時器並在每次滴答事件觸發時繪制圖像,這就是閃爍的原因
因此,首先創建一個winform,我將backColor設置為紅色,以便更好地查看背景的透明度。
添加一個計時器並打開刻度事件,然后選擇一個具有png透明度的圖像,如下所示:
將其放在您從中啟動應用程序的目錄中
最后這是代碼,它功能齊全,我已經測試過了:v
using System;
using System.Collections.Generic;
using System.ComponentModel;
System.Data;
System.Drawing;
System.IO;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace so
{
public partial class Form1 : Form
{
Image image;
public Form1()
{
image = Image.FromFile("imagen.png");
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Graphics gr = this.CreateGraphics();
BufferedGraphicsContext bgc = BufferedGraphicsManager.Current;
BufferedGraphics bg = bgc.Allocate(gr,this.ClientRectangle);
//before drawing the image clean the background with the current form's Backcolor
bg.Graphics.Clear(this.BackColor);
//use any overload of drawImage, the best for your project
bg.Graphics.DrawImage(image,0,0);
bg.Render(gr);
}
}
}
也許我不正確理解您的答案,但這可以避免閃爍,並以透明背景顯示一張圖像而沒有任何問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.