Wednesday, February 15, 2012

Fastest method for pixel plot in windows form application using c sharp sample example

In my previous articles I have shown you how to draw a pixel in picture Box. In that method we have drawn pixel directly in control of picture Box so it was slow method. Here I am presenting the fastest method to draw a pixel. For this technique we first draw a pixel or bitmap in memory then only we load it in out picture Box.
Now lets start a project
1. Create a windows form application in c sharp and give project name fastPixelPlot or your choice.
2. Set the the form properties WindowState to maximized from properties window
3.Add pictureBox on a form from toolbox
4.Set pictureBox properties Dock to fill from properties window
I assume your form name is Form1 and pictureBox name is pictureBox1


Open the Form1.cs file add a code which is shown as below:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace fastPixelPlot
{
    public partial class Form1 : Form
    {
        private Bitmap m_Canvas;
 
        public Form1()
        {
            InitializeComponent();
 
        }
        // Call this somewhere
        public void DrawSquare()
        {
            m_Canvas = new Bitmap(700, 700); // Doesn't have to be initialized here
 
            for (int x = 100; x < 200; x++)
            {
                for (int y = 100; y < 200; y++)
                {
                    m_Canvas.SetPixel(x, y, Color.Blue);
                }
            }
 
            SetCanvasAsImage();
        }
 
        public void SetCanvasAsImage()
        {
            pictureBox1.Image = m_Canvas;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            DrawSquare();
        }
 
       
    }
}


Form1.Designer.cs



namespace fastPixelPlot
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed;
        /// otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(820, 483);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            //this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(820, 483);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.PictureBox pictureBox1;
    }
}
 


Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace fastPixelPlot
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete