Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drawing to a Picture

Posted on 2003-12-08
10
Medium Priority
?
600 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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