0

WinForms Project

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?

Emcrank
  • 3
  • 2

1 Answers1

1

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

enter image description here

Add one timer and turn on the tick event Then choose an image with transparency in png, one like this:

enter image description here

Put it in the directory where you start your application from

enter image description here

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 :)

karique
  • 533
  • 6
  • 17
  • Hi there, Thank-you for this although answer although the window is external to my application. Sorry I should have mentioned that in the original question, ive updated it now. – Emcrank Feb 22 '17 at 07:52