-->

Monday, March 9, 2015

Code of Image Captcha in windows form c#

Image captcha is used for human verification. Actually some bots machine is used for creating back links to the blog/website. If you want to stop that machine, create human verification captcha. There are two types, first one is string based and another one is numbered based captcha. In this example, i will implement number based captcha.
Learn, how to design numbered based captcha. Actually, we will write some randomly generated number on image. After that we compare that number with the text box.

Video Contain Full code implementation details:

Copy this code and paste in the code file:

  int number = 0;
        private void createcaptcha()
        {
            Random r1 = new Random();
            number = r1.Next(10, 1000);
            Image img = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            var font = new Font("TimesNewRoman", 25, FontStyle.Bold, GraphicsUnit.Pixel);
            var graphics = Graphics.FromImage(img);
            graphics.DrawString(number.ToString(), font, Brushes.Red, new Point(0, 0));
            this.pictureBox1.Image = img;
     
        }

   private void refresh_Click(object sender, EventArgs e)
        {
            createcaptcha();

        }

private void verify_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == number.ToString())
            {
                MessageBox.Show("match");
            }
            else
            {
                MessageBox.Show("not match");
            }
        }

Here,

  1. Bitmap class is used for picking width and height parameter from the picture box.
  2. Using Font class, set the font style, font family and graphics unit that is pixel.
  3. Using Graphics class draw string on the picture, i have used randomly generated number.
  4. Now, assign the image to the picture box.

Code Generate the following output

Code of Image Captcha in windows form c#

Saturday, March 7, 2015

How to change Background image as a slider in windows forms application c#

Change background look attractive and different. If you want to change background with image also you want to put image slider in background, read this article carefully. Before put some images in background must to add some images in resource folder. Learn How to add images in resources folder: follow the mentioned path which is given below

Expand properties -->Resources-->Add Resource-->Add existing file

Now, set the Background image of the form through code file, add line of code before InitializeComponent() method.

   public Form1()
        {
            this.BackgroundImage = Properties.Resources._007;

            InitializeComponent();
 
        }

Now, Take a timer a control with Tick event also set the timer interval. After every interval image should be change. Now, copy the following code and paste in the code file:

Class constructor code:


 public Form1()
        {
            this.BackgroundImage = Properties.Resources.banner;
            InitializeComponent();
            Timer tm = new Timer();
            tm.Interval = 1000;
           tm.Tick += new EventHandler(changeimage);
            tm.Start();
        }

Definition of the function


  private void changeimage(object sender, EventArgs e)
        {
            List<Bitmap> b1 = new List<Bitmap>();
            b1.Add(Properties.Resources._007);
            b1.Add(Properties.Resources.banner);
            b1.Add(Properties.Resources.aspnet_validator);
            var timeget = DateTime.Now.Second % b1.Count;
            this.BackgroundImage = b1[timeget];
        }
Here,

  1. Timer control with 1 sec interval.
  2. After 1 sec timer control raise tick event handler code.
  3. List with image type.
  4. Add some images in the list.
  5. Find the remainder by the list of item on every second.
If you want to see the full code with output -- see this video

Wednesday, March 4, 2015

edmx with linq query to save and retrieve image to database in windows form c#

In my previous article we have already seen this article(Insert and retrieved) using ado.net in asp.net, On that time we  used nvarchar(size) datatype to store the image in database. Today, i am talking about image data type. Actually image data type store image in binary format. Also i will use edmx file or you can say entity framework to store and retrieve image from database table. First to create a database table in sql server management studio. Now, your table script look like:


USE [ht]
GO

/****** Object:  Table [dbo].[tblPicture]    Script Date: 4/3/2015 6:28:16 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblPicture](
[id] [int] IDENTITY(1,1) NOT NULL,
[pic] [image] NULL,
 CONSTRAINT [PK_tblPicture] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

Now, script generate the following design:


database table in management studio with image data type

Now, come to visual studio and create a new windows form project. Add two button control in the form, one for add image into database and another for retrieving image from table. Now add edmx file in the solution. After that you can write code in button click event handler block.

Code for insert image into database table:


  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opendlg = new OpenFileDialog();
            if(opendlg.ShowDialog()==DialogResult.OK)
            {
                Image img = Image.FromFile(opendlg.FileName);
                MemoryStream ms = new MemoryStream();
                img.Save(ms, img.RawFormat);
                htEntities1 ht = new htEntities1();
                ht.tblPictures.Add(new tblPicture() { pic = ms.ToArray() });
                ht.SaveChanges();
                MessageBox.Show("Load Sucess");

            }
        }
Here,

  1. First to open file dialog box and then save image into MemoryStream class.
  2. Now, you can save the MemoryStream object into database table with the help of context class(htEntities1) and model class(tblPicture).

Code for retrieving image from database table:

 private void button2_Click(object sender, EventArgs e)
        {
            htEntities1 ht = new htEntities1();
            var item = ht.tblPictures.Where(a => a.id == 1004).SingleOrDefault();
            byte[] arr = item.pic;
            MemoryStream ms = new MemoryStream(arr);
            pictureBox1.Image = Image.FromStream(ms);
         

        }
Here,

  1. Retrieve record by using linq query with lambda expression.
  2. Retrieved record insert into byte type array.
  3. Now, array pass into MemoryStream class constructor as a parameter.
  4. Now, Bind the PictureBox with Stream class.

Tuesday, March 3, 2015

How to bind label and text box control using edmx file in windows form c#

In previous article we have already learn about bind label or text box using ado.net. Today, i will discuss about edmx file. If you want to bind label or text box in windows form, edmx file is the best solution for this type of problem. There are following steps to bind the label or text box:

I am giving you a whole code video for user friendly: 

  1. First to add some label and text boxes in the form Like:
How to bind label and text box control using edmx file in windows form c#

  1.  Now, add edmx file in the solution
  2. Now, copy this code and paste in the button click event handler block.

 private void button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(idtxt.Text);
            Student_DBEntities dc = new Student_DBEntities();
            var getrecord = dc.Student_record.Where(a => a.Id == id).SingleOrDefault();

            Studentidtxt.Text = getrecord.Student_Id;
            studentnametxt.Text = getrecord.Student_Name;
            fathernametxt.Text = getrecord.Father_Name;
            MotherNametxt.Text = getrecord.Mother_Name;
            Addresstxt.Text = getrecord.Address;

        }
Here,
  1. Student_DBEntities is a data context name, Create a object of that class, Access public property(Student_record -Screen Shot mentioned in the link) by using instance name.
  2. Access single record with the help of where clause with lambda expression also use SingleOrDefault( ) method.
  3. By using getrecord variable you can access table fields or you can say model class fields. 
  4. Get each fields which is required by you, returning result assign in Text boxes.

Monday, March 2, 2015

How to bind ComboBox using edmx in windows form c#

Introduction

In my previous article i have already explained about bind combo box with entity framework using code first approach. also bind combo box with ado.net. Now, today i am discussing about edmx file. Before bind the combo box with edmx file , first to add edmx file in the solution or you can say project. Now, add a combo box in the form.

Copy the following code and paste in class constructor after  InitializeComponent(); method.

 Student_DBEntities dc = new Student_DBEntities();
            var item = dc.Student_record;
            comboBox1.DataSource = item.ToList();
            comboBox1.DisplayMember = "Student_Name";

Now, Code generate the following output:

How to bind ComboBox using edmx in windows form c#

The following video cover all such types of things which is mentioned in the article, i am giving you this video for better understanding:

Here,
  1. Student_DBEntities is a context class through which we can access or manipulate database record. (Check the context file in your solution).
Context class in edmx folder
  1. Student_record is a public property through which we can communicate database table.
How to bind ComboBox using edmx in windows form c#

Sunday, March 1, 2015

Difference between malloc( ) and calloc() in C language

In c language article we will see the difference between malloc() and calloc(). Both are the functions in c language. See the table which is mentioned below:


Malloc()
Calloc()
The Syntax of malloc() is :
Ptr = (data_type *) malloc(size);

The required number of bytes to be allocated is specified as argument i.e. size un bytes.
The Syntax of calloc() is :
Ptr = (data_type*)calloc(n,size);
Takes two arguments: n is number of blocks to be allocated, size is number of bytes to be allocated for each block.
Allocates a block of memory of size bytes.
Allocates multiple blocks of memory, each block with the same size.
Allocated space will not be initialized
Each byte of allocated space is initialized to zero.
Since no initialization takes place, time efficiency is higher than calloc()
Calloc() is slightly more computationally expensive because of zero filling but, occasionally, more convenient than malloc().
This function can allocate the required size of memory even if the memory is not available contiguously but available at different locations
This function can allocate the required number of blocks contiguously. If required memory cannot be allocated contiguously, it returns NULL.

Advantages and Disadvantages of pointers in C language

By this time, you might have understood the concepts of  C pointers and if any problem is given, you should be in a position to solve. After understanding the full concepts of pointers, we should be in a position to answer the question " What are the advantages and disadvantages of pointers?"

Advantages

  1. More than one value can be returned using pointer concept (pass by reference).
  2. Very compact code can be written using pointers.
  3. Data accessing is much faster when compared to arrays.
  4. Using pointers, we can access byte or word locations and the CPU register directly. The pointers in C are mainly useful in processing of non-primitive data structures such as arrays, linked list etc.


Disadvantages

  1. Un-initialized pointers or pointers containing invalid addresses can cause system crash.
  2. They are likely to be used incorrectly causing bugs that are very difficult to identify and rectify.
  3. They are confusing and difficult to understand in the beginning and if they are misused the result is unpredictable.
© Copyright 2013 Computer Programming | All Right Reserved