473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

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
1 5568
tlhintoq
3,525 Expert 2GB
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

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

Similar topics

7
by: Vinay | last post by:
Hi All: I have a small application that stores images either in the database or as files (depending on the user preference). I'm in the process of providing a web interface to this application....
21
by: DraguVaso | last post by:
Hi, I have an inherited DataGrid, that does lots of extra stuff. For exemple drawing a backgroundimage in every cell. The problem is that it's taking too much time (using gdi+), so I want to do...
3
by: marwa mohamed | last post by:
salamo alikom hi all when i run report that includes image control linked to a field in the table that contains the image path(on the server)and the record source of the report contains over 30...
1
by: Minhua Fu | last post by:
Hi, I have a problem when I draw a transparent gif picture. I have an existing transparent gif picture, i want to modify the gif and save it to reponse.outstream. But the picture always keep...
9
by: charliewest | last post by:
Hello - I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is there any way to temporarily save an image to a session object, and after running some other operations, later...
4
by: Nikhil | last post by:
Hi, I am trying to write a program, which will let me arrange 5 5x7 images into an A3 sheet(12x18), 600 dpi. I have a 3Ghz 512MB RAM machine. Basically, I make a 7200 by 10800 pixel (12x18)...
10
by: mrajanikrishna | last post by:
Hello friends, I have developed an application using ASP.NET and SQL Server. Ours is a garment factory. I need to design another module in my application. I want to generate images on fly...
1
by: blueheelers | last post by:
I have been researching for several hours on the best way to display images in continous forms in Access 2003. For example, I want to display employee name, email, phone, and picture for each...
11
by: Usenet User | last post by:
..NET 1.1/2.0 I have a need to display high-resolution scrollable images in a .NET application (Windows Forms). One well known solution is to create a Panel with AutoScroll set to "true" and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.