Как в данном коде поменять картинку на свою - C#
Формулировка задачи:
Имеется вот такой код:
Появляется шарик и передвигается потому что там стоит Элипс, нужно сделать чтоб загружалась своя картинка как это осуществить?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace pacman
{
public partial class Form1 : Form
{
Graphics _gr;
BufferedGraphics _bufferedGraphics;
BufferedGraphicsContext _bufferedGraphicsContext;
Rectangle _rect = new Rectangle(180, 180, 10, 10);
Pen _pen = new Pen(Color.Green, 10);
public Form1()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
KeyDown += new KeyEventHandler(Form1_KeyDown);
Paint += new PaintEventHandler(Form1_Paint);
SizeChanged += new EventHandler(Form1_SizeChanged);
_bufferedGraphicsContext = BufferedGraphicsManager.Current;
InitializeGraphics();
}
void Form1_SizeChanged(object sender, EventArgs e)
{
InitializeGraphics();
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
_rect.Location = new Point(_rect.Left - 5, _rect.Top);
if (e.KeyCode == Keys.Right)
_rect.Location = new Point(_rect.Left + 5, _rect.Top);
if (e.KeyCode == Keys.Up)
_rect.Location = new Point(_rect.Left, _rect.Top - 5);
if (e.KeyCode == Keys.Down)
_rect.Location = new Point(_rect.Left, _rect.Top + 5);
Refresh();
}
void Form1_Paint(object sender, PaintEventArgs e)
{
DrawToBuffer();
}
private void DrawToBuffer()
{
_bufferedGraphics.Graphics.Clear(BackColor);
_bufferedGraphics.Graphics.DrawEllipse(_pen, _rect);
_bufferedGraphics.Render();
}
private void InitializeGraphics()
{
_gr = CreateGraphics();
_bufferedGraphics = _bufferedGraphicsContext.Allocate(_gr, ClientRectangle);
}
}
}Решение задачи: «Как в данном коде поменять картинку на свою»
textual
Листинг программы
if (e.KeyCode == Keys.Left)
{
_rect.Location = new Point(_rect.Left - 5, _rect.Top);
_img=_imgLeft;
}...