Solved

Drawing to a Picture

Posted on 2003-12-08
10
591 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Device Location 4 36
designing in object programming 12 94
Need to start a web service from Visual Studio 2015 Pro 2 35
Winform Module - What is the ASP.Net equiv 2 28
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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