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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martinsmith160
    New Member
    • Dec 2009
    • 16

    #1

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

    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:

    Code:
    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:";
            }
        }
    }
    Last edited by tlhintoq; Dec 23 '09, 03:39 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    Working...