Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-11-2009
Member
 
Join Date: Nov 2009
Posts: 33
How to draw picture via coding

I want to draw a picture. This is the first time I set foot in this area (more used to SQL and co).

So I see 2, 3 stuff on the net ... But ... (yes there is always a but ...)

I use the following code:
Code:
public Form1()
        {
            InitializeComponent();
 
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            //bmp2 = new Bitmap(pictureBox2.Width, pictureBox2.Height);
            des = Graphics.FromImage(bmp);
            des2 = pictureBox2.CreateGraphics();
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //routine design
            des.FillRectangle(new SolidBrush(Color.DarkCyan), new Rectangle(new Point(10, 10), new Size(50, 50)));
            pictureBox1.Image = bmp;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            //routine design
            des2.FillRectangle(new SolidBrush(Color.DarkCyan), new Rectangle(new Point(10, 10), new Size(50, 50)));
            //pictureBox2.Image = bmp2;
        }
In the case of the picture box 1, I can not define the background as my PictureBox method replaces.

In the case of the picture box 2, I can define the background and graphic elements will come to position above. What I want!

But the problem is at a displacement of the window offscreen, or redirect, the Element graphic picture box 1 continues, then that element of the picture box 2 disappears ..

I grant you that it's probably a matter maternal 2nd year, but how to draw on my background without graphic element that disappears when handling the window?
Reply With Quote
  #2  
Old 13-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How to draw picture via coding

Answer 1: By making the draw in the OnPaint() component

Answer 2: This kind of question is "often" put on this forum, I invite you to research that will explain all good practices to achieve what you want you have done. It seems to me a tutorial on the UserControl explicit all the information you need for your creation!
Reply With Quote
  #3  
Old 13-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: How to draw picture via coding

Or we get the Paint event since you are in form and not in the component. Above all use the Graphics provided by the argument of the function.

Code:
protected override void OnPaint(PaintEventArgs e) 
{ 
    // Recover graphics
    Graphics gfx = e.Graphics;
}
Code:
// Normally generate by designer
pictureBox1.Paint +=  new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
 
// Recuperation of the event
protected override void PictureBox1_Paint(Object Sender,PaintEventArgs e) 
{ 
    // Recover graphics
    Graphics gfx = e.Graphics;
}
Also avoid declaring an object like field graphics class.

In GDI+, your best friends are Using and Dispose which can release the resources that are not needed.

Not those needing to gfx.dispose (), it will automatically as it is declared in function.
Reply With Quote
  #4  
Old 13-11-2009
Member
 
Join Date: Nov 2009
Posts: 33
Re: How to draw picture via coding

I'll see it.

What I wish the finally that just add a point on an existing image .. one that will apply broadly after a search we show the location of the item on a map (plan). It look like X, and it shows on our map (picture) or is placed item X.

There's easier, faster, why? or no ...
Reply With Quote
  #5  
Old 13-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How to draw picture via coding

If it is to put one point (or ten, but not type 10000)

Code:
Bitmap img = new Bitmap("c:\...\img.bmp");
img.SetPixel ( 100,50,Color.Red);
This relatively slow downs approach to the Lock/ Unlock but as the number of points is limited, it will be done by one hundredth of a second
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to draw picture via coding"
Thread Thread Starter Forum Replies Last Post
Picture size for Wallpaper picture in Sony Ericsson Xperia Play Bryant Portable Devices 4 07-10-2011 12:24 AM
Making MS Office Picture Manager My Default Picture Viewer / Editor Klums MS Office Support 2 09-01-2011 05:07 PM
Help me to use the transform() in C++ coding Tailor Software Development 5 19-02-2010 04:17 PM
SANYO Introduces True HD DLP projector with ?Picture-by-Picture? Capability Abbas Portable Devices 2 10-03-2009 11:25 PM
How to make Windows Picture & Fax Viewer as default program for opening picture Stel Windows x64 Edition 1 11-04-2008 05:24 PM


All times are GMT +5.5. The time now is 12:49 AM.