Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for November, 2010

Arithmetic Captcha in

Posted by Viral Sarvaiya on November 22, 2010

We know the captcha control, but now a day so many captcha code is available, most of the captcha is only for the text, but here I explain the new captcha that want human in intelligence as like arithmetic expression.
Create the new project,

Add a new file for create the captcha control named “captcha.aspx”.

In the page load of the captcha.aspx page.

protected void Page_Load(object sender, EventArgs e)

 void returnNumer()
 Random num1 = new Random();
 Random num2 = new Random();
 int numQ1, numQ2;
 string QString;
 numQ1 = num1.Next(10, 15);
 numQ2 = num1.Next(17, 31);
 QString = numQ1.ToString() + " + " + numQ2.ToString() + " = ";
 Session["answer"] = numQ1 + numQ2;

 Bitmap bitmap = new Bitmap(85,25);

 Graphics Grfx = Graphics.FromImage(bitmap);
 Font font = new Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel);
 Rectangle Rect = new Rectangle(100, 0, 0, 0);

 Grfx.FillRectangle(Brushes.Brown, Rect);
 Grfx.DrawRectangle(Pens.PeachPuff, Rect);
 // Border
 Grfx.DrawString(QString, font, Brushes.Azure, 0, 0);

 Response.ContentType = "Image/jpeg";
 bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);


Now in the default.aspx page take some control and 1 image control for the display captcha.

 <form id="form1" runat="server">
 Are You Human?  <asp:image ID="Image1" runat="server" ImageUrl="Capcha.aspx" />
 <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
 <br /><br />
 <asp:Button ID="Btnsubmit" runat="server" Text="Submit"  onclick="Btnsubmit_Click" /><br /><br />
 <asp:Label ID="lblValidate" runat="server"></asp:Label>

To check the input is valid or not, check the session value for the arithmetic expression.
So in default.aspx.cs file

protected void Page_Load(object sender, EventArgs e)

 protected void Btnsubmit_Click(object sender, EventArgs e)
 if (Session["answer"] != null)
 if (Session["answer"].ToString() == txtInput.Text)
 lblValidate.Text = "Current Answer";
 lblValidate.Text = "Incurrent Answer, Please input Valid Answer.";

Now run the page

inter the result and click the button

if result is currect then

and if answer id not currect then,

Hope this will help you…



Posted in ASP.NET | Tagged: , , , , | 6 Comments »

File Drag and Drop in Silverlight4

Posted by Viral Sarvaiya on November 2, 2010

Silverlight 4 have drag and drop functionality. here i demostrate that functionality.
first create new project named “DragAndDropImage”.
You have to add references “Microsoft.Expression.Interactions”, “” and “”

Silverlight is too fun, when coding gets this simple. So the image can now be dragged around. Now lets add the drop feature.

For making everything very simple, I will just now add AllowDrop=”True” and then add a drop handler to handle any file that is being dropped.

Now, lets move to the code behind. For demonstration purpose, I will use images from deviantArt gallery.

In DragAndDropImageTest.xaml file

<UserControl x:Class="SilverLightTest.DragAndDropImageTest"
 d:DesignHeight="300" d:DesignWidth="400">

 <Grid x:Name="LayoutRoot" AllowDrop="True" Drop="LayoutRoot_Drop" Background="Black">
 <StackPanel x:Name="DropCollection" Height="100" Margin="10,30,10,10" VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal">
 <Image x:Name="DropImage" Margin="0,100,0,0" Width="400" Cursor="Hand">
 <ei:MouseDragElementBehavior ConstrainToParentBounds="True"></ei:MouseDragElementBehavior>

In DragAndDropImageTest.xaml.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Browser;
using System.IO;

namespace SilverLightTest
 public partial class DragAndDropImageTest : UserControl
 public DragAndDropImageTest()

 private void LayoutRoot_Drop(object sender, DragEventArgs e)
 if (e.Data != null)
 //reading the drop file
 FileInfo file = ((FileInfo[])e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();

 //creating a bitmapImage
 BitmapImage img = new BitmapImage();

 using (Stream s = file.OpenRead())

 //setting the image in xaml
 DropImage.Source = img;

 Image newImage = new Image { Source = img };
 newImage.MouseLeftButtonDown += (a, b) =>
 DropImage.Source = newImage.Source;


Run the application and check with dropping the image file in browser.


Enjoy Coding…..

Posted in ASP.NET, Silverlight | Tagged: , , , , , | 1 Comment »

%d bloggers like this: