By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,826 Members | 835 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,826 IT Pros & Developers. It's quick & easy.

How to draw images to a memory bitmap and then draw that bitmap ontop of a picture bo

P: 16
Hi all

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
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace LevelBuilderTool
  11. {
  12.     public partial class LevelBuilderTool : Form
  13.     {
  14.         int selectedImageIndex = -1;
  15.         Point imagePosition = new Point(0, 0);
  16.  
  17.         //memory bitmap
  18.         private Bitmap bit_DrawSurface;
  19.  
  20.         public LevelBuilderTool()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void btn_move_Click(object sender, EventArgs e)
  26.         {
  27.             //Stores index of what image to draw
  28.             selectedImageIndex = this.lst_Objects.SelectedIndex;
  29.         }
  30.  
  31.         private void pcb_ToolWindow_MouseDoubleClick(object sender,  MouseEventArgs e)
  32.         {  
  33.             //draws on the memory bitmap
  34.             Graphics g = Graphics.FromImage(bit_DrawSurface);
  35.             g.DrawImage(this.imgLs_Sprites.Images[selectedImageIndex], imagePosition);
  36.             this.panel1.Invalidate();
  37.             g.Dispose();
  38.         }
  39.  
  40.         private void LevelBuilderTool_Load(object sender, EventArgs e)
  41.         {
  42.             //creates a bitmap to draw to the size of the picture box
  43.             bit_DrawSurface = new Bitmap(this.panel1.ClientRectangle.Width,
  44.                 this.panel1.ClientRectangle.Height,
  45.                 System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  46.  
  47.             InitializeBitmap();
  48.         }
  49.  
  50.         private void InitializeBitmap()
  51.         {
  52.             Graphics g = Graphics.FromImage(bit_DrawSurface);
  53.             g.Clear(SystemColors.Control);
  54.             g.Dispose();
  55.  
  56.         }
  57.  
  58.         private void panel1_Paint(object sender, PaintEventArgs e)
  59.         {
  60.             //draw bitmap
  61.             e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
  62.                 bit_DrawSurface.Width,
  63.                 bit_DrawSurface.Height);
  64.             e.Graphics.Dispose();
  65.         }
  66.  
  67.         private void pcb_ToolWindow_MouseMove_1(object sender, MouseEventArgs e)
  68.         {
  69.             //stores the mouse position
  70.             this.lbl_xPos.Text = "X: " + e.X.ToString();
  71.             this.lbl_yPos.Text = "Y: " + e.Y.ToString();
  72.             imagePosition.X = (int)e.X;
  73.             imagePosition.Y = (int)e.Y;
  74.         }
  75.  
  76.         private void pcb_ToolWindow_MouseLeave_1(object sender, EventArgs e)
  77.         {
  78.             //updates labels
  79.             this.lbl_xPos.Text = "X:";
  80.             this.lbl_yPos.Text = "Y:";
  81.         }
  82.     }
  83. }
Dec 22 '09 #1
Share this Question
Share on Google+
1 Reply


tlhintoq
Expert 2.5K+
P: 3,525
Bytes has a policy regarding assisting students with their homework.

The short version is that the volunteers here can't help you with schoolwork.
A) We don't know what material you have and have not learned in class.
B) We don't know the guidelines you must follow.
C) In the long run giving you the answers actually short changes your education.
Dec 23 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.