Solved

Drawing to a Picture

Posted on 2003-12-08
10
589 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

777 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