Solved

Drawing to a Picture

Posted on 2003-12-08
10
597 Views
Last Modified: 2010-04-16
This may sound kind of stupid. Anyway.

I need to do some test data, and so I wanted to make 1000 images with numbers in them. So I figured It would be easier to write a program to just use a Random function to write a bunch of numbers and save them as an image. . . however I'm somewhat perplexed on how to draw to an actual file. I can draw to the form or a picturebox just fine .....but I think I might have just answered my own question with the picturebox. Well, If anyone has any suggestions please let me know..
0
Comment
Question by:derekthornton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 7

Accepted Solution

by:
TransBind earned 500 total points
ID: 9899113
I didn't compile this so not sure about syntax etc ..., sorry I am at work, but I think thats how u can do it.

        // This example creates a PictureBox control on the form and draws to it.
        // This example assumes that the Form_Load event handling method is
        // connected to the Load event of the form.

       
        private PictureBox pictureBox1 = new PictureBox();
       
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // Dock the PictureBox to the form and set its background to white.
            pictureBox1.Dock = DockStyle.Fill;
            pictureBox1.BackColor = Color.White;
            // Connect the Paint event of the PictureBox to the event handling method.
            pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

            // Add the PictureBox control to the Form.
            this.Controls.Add(pictureBox1);
        }

        private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Random R =new Random();

            // Create a local version of the graphics object for the PictureBox.
            Graphics g = e.Graphics;
            //Calling the Next method without any arguments will generate a random number which is greater than or equal to zero and less than the maximum size of int (which i do not pass).

            for(I = 0; I < 1000; I++)
            {
                   // Draw a number on the PictureBox.
            g.DrawString(R.Next().toString(),
             new Font("Arial",10), System.Drawing.Brushes.Blue, new Point(30,30));
            }
         
           
        }
0
 
LVL 7

Expert Comment

by:TransBind
ID: 9899139
Perhaps this will contribute as well.
class MyRandom
{
      public static void Main()
      {
            Random R =new Random();
            int I;
            int MaxLimit = 5000;
            int MinLimit = 1000;
            for(I = 0; I < 10; I++)
            {
                  Console.WriteLine(R.Next(MinLimit,MaxLimit));
            }
      }
}
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9899171
Yes, it works. Can you save a Graphics Object as a File? Or Do I need to save the PictureBox contents each time the loop iterates ( speed isn't an issue here, this is just to create some test data )
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:TransBind
ID: 9899210
private void MainForm_Paint(object sender,                             System.Windows.Forms.PaintEventArgs e)
{

 Graphics g = e.Graphics;  
  Random R =new Random();
   for(I = 1; I <= 1000; I++)
          {

MyBitmap = new Bitmap("c:\\image" + I + ".bmp");    
 g.DrawString(R.Next().toString(),
             new Font("Arial",10), System.Drawing.Brushes.Blue, new Point(30,30));
         }
}
0
 
LVL 7

Expert Comment

by:TransBind
ID: 9899218
i guess something like that
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9899245
Hrnm.. That doesn't seem to work. It is throwing me an exception at the Application.Run Method in my Main Class..
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9899249
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.PictureBox pictureBox1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.pictureBox1 = new System.Windows.Forms.PictureBox();
                  this.SuspendLayout();
                  //
                  // pictureBox1
                  //
                  this.pictureBox1.Location = new System.Drawing.Point(0, 0);
                  this.pictureBox1.Name = "pictureBox1";
                  this.pictureBox1.TabIndex = 0;
                  this.pictureBox1.TabStop = false;
                  this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 266);
                  this.Controls.Add(this.pictureBox1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);

            }
            #endregion

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

            private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
                  Random R =new Random();

                  // Create a local version of the graphics object for the PictureBox.
                  Graphics g = e.Graphics;
                  //Calling the Next method without any arguments will generate a random number which is greater than or equal to zero and less than the maximum size of int (which i do not pass).

                  for(int I = 0; I < 1000; I++)
                  {
                        Bitmap MyBitmap = new Bitmap(@"C:\TestData\Test" + I + "Bmp");
                        // Draw a number on the PictureBox.
                        g.DrawString(R.Next().ToString(),
                              new Font("Arial",10),
                              System.Drawing.Brushes.Blue, new Point(30,30));

                  }
         
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  // Dock the PictureBox to the form and set its background to white.
                  pictureBox1.Dock = DockStyle.Fill;
                  pictureBox1.BackColor = Color.White;
                  // Connect the Paint event of the PictureBox to the event handling method.
                  pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

                  // Add the PictureBox control to the Form.
                  this.Controls.Add(pictureBox1);
            }
      }
}

This is what I've got so far.
0
 
LVL 7

Expert Comment

by:TransBind
ID: 9899251
no thats wrong i can already see it
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9899269
Alright. What do you see that isn't right? For some reason it's telling me I'm passing an invalid parameter through the Main Function.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9899433
Got it! Thanks for the help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question