I am trying to create a level builder tool for a final year project and im having some problems drawing. I have placed a picture box within a panel so i can scroll around the image which is working fine. My aim is to double click the picture box and the desired image will be drawn at the mouse position. This works fine unless I scroll or minimise the form because the image isnt repainted after movement. I looked up drawing the image to a memory bitmap and then drawing the bitmap over the picture box in its paint event but everytime I do that i get an error message. I also tried drawing it in the paint event of the panel but nothing appears at all but there is no error message. Any help would be hugely appreciated.
here is my code so far:
Expand|Select|Wrap|Line Numbers
- 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 LevelBuilderTool
- {
- public partial class LevelBuilderTool : Form
- {
- int selectedImageIndex = -1;
- Point imagePosition = new Point(0, 0);
- //memory bitmap
- private Bitmap bit_DrawSurface;
- public LevelBuilderTool()
- {
- InitializeComponent();
- }
- private void btn_move_Click(object sender, EventArgs e)
- {
- //Stores index of what image to draw
- selectedImageIndex = this.lst_Objects.SelectedIndex;
- }
- private void pcb_ToolWindow_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- //draws on the memory bitmap
- Graphics g = Graphics.FromImage(bit_DrawSurface);
- g.DrawImage(this.imgLs_Sprites.Images[selectedImageIndex], imagePosition);
- this.panel1.Invalidate();
- g.Dispose();
- }
- private void LevelBuilderTool_Load(object sender, EventArgs e)
- {
- //creates a bitmap to draw to the size of the picture box
- bit_DrawSurface = new Bitmap(this.panel1.ClientRectangle.Width,
- this.panel1.ClientRectangle.Height,
- System.Drawing.Imaging.PixelFormat.Format24bppRgb);
- InitializeBitmap();
- }
- private void InitializeBitmap()
- {
- Graphics g = Graphics.FromImage(bit_DrawSurface);
- g.Clear(SystemColors.Control);
- g.Dispose();
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- //draw bitmap
- e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
- bit_DrawSurface.Width,
- bit_DrawSurface.Height);
- e.Graphics.Dispose();
- }
- private void pcb_ToolWindow_MouseMove_1(object sender, MouseEventArgs e)
- {
- //stores the mouse position
- this.lbl_xPos.Text = "X: " + e.X.ToString();
- this.lbl_yPos.Text = "Y: " + e.Y.ToString();
- imagePosition.X = (int)e.X;
- imagePosition.Y = (int)e.Y;
- }
- private void pcb_ToolWindow_MouseLeave_1(object sender, EventArgs e)
- {
- //updates labels
- this.lbl_xPos.Text = "X:";
- this.lbl_yPos.Text = "Y:";
- }
- }
- }