public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.InitialDX();
}
private Device device;
private Texture texture;
private void InitialDX()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(
0,
DeviceType.Hardware,
this,
CreateFlags.SoftwareVertexProcessing,
presentParams);
device.RenderState.Lighting = false;
texture = Texture.FromBitmap(
device,
(Bitmap)Image.FromFile("w.png"),
Usage.None,
Pool.Managed);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.DrawDX();
}
private void DrawDX()
{
device.Clear(ClearFlags.Target, Color.AliceBlue, 1f, 0);
device.BeginScene();
this.DrawMyGraphics();
device.EndScene();
device.Present();
}
private void DrawMyGraphics()
{
Sprite sprite = new Sprite(device);
sprite.Begin(SpriteFlags.AlphaBlend);
sprite.Draw2D(texture,
Point.Empty,
0f,
new Point(10, 10),
Color.White);
sprite.End();
}
}
效果: