1
Answer

chapture image coding

Nitin Sharma

Nitin Sharma

10y
778
1
i m using this coding for capture image for registration page it is working in windows form but not working in asp form can anybody tell me how we can convert this coding so that it will be working on asp page because windows form have picture box but asp webform have image control please help me 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            generateImage();
        }
        int value = 0;
        private void generateImage()
        {
            Random random = new Random (); // get a random instance
            value = random.Next(10000, 99999); // get a random value between any range
            var image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); // Get a bitmap
            var font = new Font("TimesNewRoman", 25, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); // Get a font
            var graphics = Graphics.FromImage(image); // Get a graphics with the bitmap image
            graphics.DrawString(value.ToString (), font, Brushes.Red, new PointF(0, 0)); // Add the value in the graphics
            Pen p = new Pen(Brushes.Orange, 2.0f); // get pen width
            graphics.DrawLine(p, new PointF(0,this.pictureBox1 .Height), new Point(this.pictureBox1.Width,0)); // draw a diagonal line
            graphics.DrawLine(p, new PointF(0,0), new Point(this.pictureBox1.Width , this.pictureBox1.Height)); // draw another diagonal line 
            p.Dispose(); // dispose the pen to avoid memory leak
            graphics.SmoothingMode = SmoothingMode.AntiAlias; // Smoothing the pixel
            graphics.TextRenderingHint = TextRenderingHint.AntiAlias; // Smoothing the text rendering because stem width may differ
            this.pictureBox1.Image = image; // load the image in the picturebox    
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.generateImage();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == value.ToString())
            {
                MessageBox.Show("Inserted value is correct");
            }
            else
            {
                MessageBox.Show("the value you have entered is wrong");
                textBox1.Text = String.Empty;
            }
        }
    }

Answers (1)