Basic graphics

D

Daniel

Hey guys

I have an app front end in directx, but have decided since my graphics are
so basic and my app always runs windowed that removing direct x and using
standard graphics functionality is better.

In directx i have a texture file full of images and i can take a rectangular
region and display it, then move to the next rectangle and display it etc.

So i would like to do the same using, i presume gdi which should use a lot
less cpu power i presume?Can gdi do this, take a rectangular region of a
file and blit it to the screen? So that i can do everything i did in directx
in gdi?

Any tips or tutorials would be great

Dan
 
M

Mehdi

In directx i have a texture file full of images and i can take a rectangular
region and display it, then move to the next rectangle and display it etc.

So i would like to do the same using, i presume gdi which should use a lot
less cpu power i presume?

Not necessarily. GDI+ does not yet support hardware acceleration so it
might not be as fast as DirectX and might not use less CPU power. For
simple drawing though, it does the job just fine and is it's really easy to
use (i've never used DirectX but i'm working on DirectShow at the moment
and if that's the same mess in DirectX then you'll be more than happy to
switch to GDI+).
Can gdi do this, take a rectangular region of a
file and blit it to the screen? So that i can do everything i did in directx
in gdi?

I'm not too sure of what you want to do here. If you've got an image file
and want to draw part of the image on a Form or a Control, use the Image
class to load to image then use the Graphics.DrawImage() or
Graphics.DrawImageUnscaled() to draw all or part of the image on whatever
drawing surface you want to draw (this can be a Control's surface or a
Bitmap object).
Any tips or tutorials would be great

The reference for all GDI+ stuff is Bob Powell's FAQ:
<http://www.bobpowell.net/faqmain.htm>. He's got a beginner's guide too.
The appropriate forum for GDI+ drawing is
microsoft.public.dotnet.framework.drawing
 
D

Daniel

Hey

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.
 
M

Mehdi

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;
}
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top