Direct x drawing speed compared to gdi or gdi+

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
When I draw using gdi or gdi+ whenever the mouse moves I always get flicker on the sides of the image. Would using Direct x or openGL fix that?

And, how does it work? Does it draw a whole screen at once?
 
If you're talking about 2D drawing, DirectX really comes in to its own with the hardware acceleration and that fact that flicker-free double buffering can be done with page flipping at virtually no performance cost when in fullscreen mode.
 
Your post was a little confusing. The fact you are tallking about, is it that with GDI+ I can use double buffer with no flickering on the sides of the drawing in fullscreen mode?

And what league is OpenGL in?

For example, with the snes emulator Snes9xw you can play the game in window mode with spectacular (in my opinion) performance.
 
You can use double buffering techniques in GDI+ to remove the flickering.

In comparison to GDI+ and DirectDraw; DirectDraw is faster, way faster.

OpenGL in general is faster then DirectX, but only when using a language that has direct access to it (ie; C/C++). DirectX is more used for games though because it has more support and is pretty much guaranteed to run on any system with Windows installed on it. OpenGL is typically used for games that need to be multi-platform.
 
"You can use double buffering techniques in GDI+ to remove the flickering."

I am. What I mean is flicker on the sides of the image. I think the reason is because I am redrawing it every pixel from the mouse move event...
C#:
//aForm constructor:
public aForm()
				{
					this.SetStyle(ControlStyles.DoubleBuffer, true);
					this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
					this.SetStyle(ControlStyles.UserPaint, true);

					this.BackColor= a.Colors.aControl;
				}
C#:
public class Form1 : a.Forms.aForm
	{
		a.Api.BlendFunction BF;
		bool draw=false;
		IntPtr hB;
		Bitmap B;
		Rectangle posRec;
		Rectangle posBothRec;
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			B= (Bitmap)Image.FromFile("D:\\copy.bmp");
			/*B= new Bitmap(200, 150);
			Graphics g= Graphics.FromImage(B);
			SolidBrush SB=new SolidBrush(Color.FromArgb(0,0,0,0));
			g.FillRectangle(SB,0,0,B.Width, B.Height);
			g.FillEllipse(Brushes.Red, 0,0, 200, 150);
			g.Dispose();*/
			this.BF= a.Api.GetBlendFunction(255);

			//B.MakeTransparent(Color.White);
			//B.MakeTransparent(Color.Black);

			InitializeComponent();

			//this.BackColor=Color.Blue;
			this.BackgrdBrush=new HatchBrush(HatchStyle.LargeConfetti, Color.Blue, Color.Black);
			//this.BackgrdBrush=new LinearGradientBrush(this.ClientRectangle, Color.Gray, Color.Beige, 20);
			this.posRec=new Rectangle(0,0,0,0);
			hB= B.GetHbitmap();

		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(788, 406);
			this.Name = "Form1";
			this.Text = "Form1";
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

		}
		#endregion

		[STAThread]
		static void Main()
		{
			Application.Run(new Form1());
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			if(draw)
			{
				Draw(e);
			}
		}

		private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.draw=false;
			//if(e.X.ToString().EndsWith("0"))
			{
				Rectangle curPos= this.posRec;
				this.posRec=new Rectangle(e.X, e.Y, B.Width, B.Height);

				posBothRec=new Rectangle(
					(curPos.X < this.posRec.X ? curPos.X : this.posRec.X), 
					(curPos.Y < this.posRec.Y ? curPos.Y : this.posRec.Y), 
					Math.Abs(curPos.X-this.posRec.X)+B.Width*2+2, 
					Math.Abs(curPos.Y-this.posRec.Y)+B.Height
					);

				this.draw=true;
				this.Invalidate(posBothRec);
			}
		}
static int i=255;
static int j=100;
		void Draw(PaintEventArgs e)
		{
			a.Api.DrawBitBlt(e.Graphics, B, ref this.posRec, ref BF);
			//a.Api.DrawBitBlt(e.Graphics, B, ref this.posRec);
			//e.Graphics.DrawImageUnscaled(B, this.posRec);
			
			ImageAttributes Ia= a.graphics.TrasparentImage(j);
			Rectangle testRect=new Rectangle(this.posRec.X+B.Width+2, this.posRec.Y, B.Width, B.Height);
			e.Graphics.DrawImage(B, testRect, 0, 0, B.Width, B.Height, GraphicsUnit.Pixel, Ia);
			
			if(i < 1)
				i= 255;
			else
				i= i-5;
			BF.SourceConstantAlpha=(byte)i;

			if(j < 1)
				j= 100;
			else
				j= j-2;
			//e.Graphics.FillRectangle(Brushes.Black, this.posRec);
		}



	}
 
Last edited:
Back
Top