Member Avatar for cool_intentions

Hello guys,
I`m having a problem with my assignment. I should make simple Captcha in Windows Form Application. I have one text box, picture box and button. When I click GO button, my text from text box should become Captcha end show itself in picture box. Any idea how can I do that?
Thanks all for helping me.

So...you want to take text from the box and turn it into a graphical representation with noise in it?

Or do you want to have an image appear and have the user enter what they see?

Member Avatar for cool_intentions

Yes, I want to take text from the box and turn it into a graphical representation with noise in it.

Well, I would say there are two steps involved here.

  1. Convert your text into an image (bitmap)
  2. Alter the image you created and display it in the picturebox

You will probably (for clarity in my opinion) need two methods, one for each of those tasks.

For the first step I would check out the Drawing namespace. You need a method that returns a Bitmap object first. You can add the text from your textbox to the bitmap with the Graphics.Drawstring method. There's a (somewhat) decent example of this being done here.

For the second step, you need to modify your new bitmap object. This is the tricky part. I would suggest you start here.

I've never done anything like what you are doing though. Those are good starting points though.

Try searching Google for "Image Processing in C#" also for more ideas with the second step.

Those are just my suggestions, someone might have better ones.

Member Avatar for cool_intentions

Thanks mate, it helped me a lot.

Great! Glad it was helpful!

Member Avatar for cool_intentions
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 Captcha_creator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Provijeravomo unesenu duzinu karaktera.
            if (textBox1.Text.Length > 12)
                MessageBox.Show("Dozvoljeno je unijeti maksimalno 12 karaktera");
            else
            {
                try
                {
                    // Kreiramo bmp file (sliku koja ce se kasnije prikazati u pictureBox1 kontroli).
                    Bitmap bmp = new Bitmap(240, 60);
                    Graphics gImage = Graphics.FromImage(bmp);
                    gImage.FillRectangle(Brushes.Wheat, 0, 0, bmp.Width, bmp.Height);

                    // Odredjujemo font, boju teksta i deklarisemo DrawString metodu.
                    Font CAPTCHAfont = new Font("Chiller", 40);
                    Brush cetka = Brushes.Black;
                    gImage.DrawString(textBox1.Text, CAPTCHAfont, cetka, 0, 0);

                    // Dodajemo linije preko teksta, da je (SPAM)programima teze procitati tekst.
                    Pen p = new Pen(Color.Black, 3);
                    gImage.DrawLine(p, 230, 15, 10, 15);
                    gImage.DrawLine(p, 230, 30, 10, 30);
                    gImage.DrawLine(p, 230, 45, 10, 45);


                    // Dodjeljujemo ime bmp fajlu.
                    string imeFajla = textBox1.Text.ToString();

                    // Sacuvavamo bmp file.
                    bmp.Save(@"C:\" + imeFajla + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

                    //Prikazujemo bmp file u pictureBox kontroli.
                    pictureBox1.Image = new Bitmap(@"C:\" + imeFajla + ".bmp");
                }
                catch
                {
                    MessageBox.Show("Željena CAPTCHA je već kreirana.");
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Ahhh! A zombie!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.