我试图绘制到图像,然后使用以下方法将其绘制到我的应用程序外部的窗口:
using(var g = Graphics.FromHandle(handle))
g.DrawImage(myImage);
减少绘图时的闪烁行为。
我知道我可以使用BufferedGraphics
类,但它有黑色背景,所以我尝试首先绘制透明图像,然后在其上绘制,但渲染时仍然有黑色背景它。
有没有什么方法可以使用BufferedGraphics
或一种绘制内存的方式,以便可以将整个图像一次性绘制(以减少闪烁),以透明背景进行屏幕显示?
答案 0 :(得分:1)
我假设您正在使用计时器并在每次点击事件触发时绘制图像,这就是闪烁的原因
所以,首先创建一个winform,我将backColor设置为红色,这样你可以更好地看到背景的透明度
添加一个计时器并打开tick事件 然后选择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);
}
}
}
也许我没有正确理解你的答案,但这样可以避免闪烁并显示一个带有透明背景的图像而没有任何问题:)