On Sat, 1 Jul 2006 22:14:29 +0100, Daniel wrote:
Thanks for the advice. My directx scene runs a game loop and that therefore
uses as much cpu power as is available.
When i referred to the rectangular region, i am referring to taking a bmp
say, and on that bitmap of say 512x512 pixels i can have say frame one of an
animation on the first 32x32, then frame 2 on the next 32x32 etc, then when
i want to show frame one i take rectangular region of 32x32 in position 0,0
then for frame 2 take the same 32x32 size but starting at position 32,0,
frame 2.
I will see if i cant get the cpu usage down, maybe i will stick with my
directx setup, its working great just worried about if i have a bit of
overkill.
I honestly don't know whether this would be less CPU intensive with GDI+ or
not but it takes only a few seconds to do that with GDI+: add a new form to
you project, replace the code with the following (untested) code and
display the form.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Whatever
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private const string ImagePath = "image.bmp";
private const int FrameRate = 10; // 10 frames per second
private Image m_image = null;
private int m_nextX = 0;
private int m_nextY = 0;
private System.Windows.Forms.Timer frameTimer;
public Form1()
{
InitializeComponent();
// Set double buffering to avoid flickering
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.ContainerControl |
ControlStyles.ResizeRedraw, true);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.frameTimer = new System.Windows.Forms.Timer(this.components);
//
// frameTimer
//
this.frameTimer.Interval = 500;
this.frameTimer.Tick += new System.EventHandler(this.frameTimer_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.Form1_ Paint);
}
#endregion
private void Form1_Load(object sender, System.EventArgs e)
{
// Load the image file
m_image = Image.FromFile(ImagePath);
// Start the timer
frameTimer.Interval = 1000 / FrameRate;
frameTimer.Start();
}
private void frameTimer_Tick(object sender, System.EventArgs e)
{
// When the timer ticks, ask the form to redraw itself
// (draw the next frame)
this.Invalidate();
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
// Paint the next frame
e.Graphics.DrawImage(m_image,
0, 0,
new Rectangle(m_nextX, m_nextY, 32, 32),
GraphicsUnit.Pixel);
// Compute the coordinates of the origin of the following frame
m_nextX += 32;
if (m_nextX + 32 m_image.Width)
{
// We've finished this line of images, go to the next line
m_nextX = 0;
m_nextY += 32;
if (m_nextY + 32 m_image.Height)
{
// We've reached the end of the images, loop back
// to the begining
m_nextY = 0;
}
}
}
}
}