Im trying to draw to an image then draw that to a window that is external to my application using:
using(var g = Graphics.FromHandle(handle))
g.DrawImage(myImage);
to reduce flickering behavior when drawing.
I know that I can use the BufferedGraphics
class, but it had a black background so I tried to draw a transparent image to it first and then draw on top of that but it still had a black background when rendering it.
Is there any way to use BufferedGraphics
or a way of drawing once to memory so it can be drawn as a whole image at once (to reduce flickering) to screen with a transparent background?
i assume that you are using a timer and drawing the image each time the tick event fires and that's the reason of the flickering
So, first create a winform, i am putting the backColor to red so you can see the transparency of the background better
Add one timer and turn on the tick event Then choose an image with transparency in png, one like this:
Put it in the directory where you start your application from
Finally this is the code, it's fully functional, i tested already :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);
}
}
}
Maybe i didn't understand correctly your answer, but this will avoid the flickering and show one image with transparent background without any problem :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.