Solved

Drawing to a Picture

Posted on 2003-12-08
10
587 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
  • 5
  • 5
10 Comments
 
LVL 7

Accepted Solution

by:
TransBind earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Expert Comment

by:TransBind
Comment Utility
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
Comment Utility
i guess something like that
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:derekthornton
Comment Utility
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
Comment Utility
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
Comment Utility
no thats wrong i can already see it
0
 
LVL 2

Author Comment

by:derekthornton
Comment Utility
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
Comment Utility
Got it! Thanks for the help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now