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

Loading an image, modifying that image, losing mods when minimized

P: n/a
I'm afraid I will incur the wraith of Mr. Powell on this one - but I did read
his #1 FAQ and some others and I still can't figure this out.

I created this little c# app. and I have a PictureBox in my Form. I load
this image from the filesystem into the PictureBox and then I draw random
little lines on the image. Then when I minimize and reopen the application
the little lines are gone.

Is there a way to save my lines in memory and then have them redrawn when I
restore the application (after being minimized)?

Here is my code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Threading;

namespace PsychImage
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image =
((System.Drawing.Image)(resources.GetObject("pictu reBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(328, 245);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem3});
this.menuItem1.Text = "File";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Do It!";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Text = "Select File";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 245);
//this.Controls.Add(this.pictureBox1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint );

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Thread thread = new Thread (new ThreadStart(drawStuff));
thread.Start();
}

private void drawStuff()
{
Graphics grf = pictureBox1.CreateGraphics();
try
{
for (int i = 0; i < 20;i++)
{
grf.DrawLine(Pens.Black, i,0,100, 100+i);
}
}
finally
{
grf.Dispose();
}
}

private void menuItem3_Click(object sender, System.EventArgs e)
{

}

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
// Use the graphics object passed in the EventArgs
// DO NOT dispose of this object!
}

}
}

-----------------------------------

How do I keep those lines in memory - instead of having to redraw them in
the paint event (as illustrated in the example that Mr. Powell). The reason
I want to keep those lines in memory is because in my actual program I'm
doing more than just drawing a bunch of straight lines - I'm doing some
fairly complex drawings over an existing image.

Thanks,
Novice
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I found a way to do what I want to do below - but it is HORRIBLY slow!!!

Basically what I do is assign the image to a Bitmap object, then modify the
Bitmap object and each time I add another shape/line/whatever to the image, I
assign it to the PictureBox.Image property.

I would like to ideally be able to do the following:
1. start my program
2. click a button that starts the complex drawing over an existing image
(that I can watch happen in progress)
3. minimize the application while it continues to draw
4. restore my application (from being minimized) an hour later
5. see the changes to the image and potentially watch the changes real time
6. potentially minimize it again while it continues processing - and so
on.....

Here is my new solution - again - in my real application I'm not just
drawing a bunch of lines:
-------------------------------------
private void drawStuff()
{
Bitmap bm = new Bitmap(pictureBox1.Image);
Graphics grf = Graphics.FromImage(bm);
try
{
for (int i = 0; i < 20;i++)
{
grf.DrawLine(Pens.Black, i,0,100, 100+i);
pictureBox1.Image = bm;
}
}
finally
{
grf.Dispose();
}
}
-------------------------------------

Thanks,
Novice

"Novice" wrote:
I'm afraid I will incur the wraith of Mr. Powell on this one - but I did read
his #1 FAQ and some others and I still can't figure this out.

I created this little c# app. and I have a PictureBox in my Form. I load
this image from the filesystem into the PictureBox and then I draw random
little lines on the image. Then when I minimize and reopen the application
the little lines are gone.

Is there a way to save my lines in memory and then have them redrawn when I
restore the application (after being minimized)?

Here is my code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Threading;

namespace PsychImage
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image =
((System.Drawing.Image)(resources.GetObject("pictu reBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(328, 245);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem3});
this.menuItem1.Text = "File";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Do It!";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Text = "Select File";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 245);
//this.Controls.Add(this.pictureBox1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint );

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Thread thread = new Thread (new ThreadStart(drawStuff));
thread.Start();
}

private void drawStuff()
{
Graphics grf = pictureBox1.CreateGraphics();
try
{
for (int i = 0; i < 20;i++)
{
grf.DrawLine(Pens.Black, i,0,100, 100+i);
}
}
finally
{
grf.Dispose();
}
}

private void menuItem3_Click(object sender, System.EventArgs e)
{

}

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
// Use the graphics object passed in the EventArgs
// DO NOT dispose of this object!
}

}
}

-----------------------------------

How do I keep those lines in memory - instead of having to redraw them in
the paint event (as illustrated in the example that Mr. Powell). The reason
I want to keep those lines in memory is because in my actual program I'm
doing more than just drawing a bunch of straight lines - I'm doing some
fairly complex drawings over an existing image.

Thanks,
Novice

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.