Code Simplified – Viral Sarvaiya

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

Archive for November, 2010

Arithmetic Captcha in asp.net

Posted by Viral Sarvaiya on November 22, 2010


Hi,
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)
 {
 returnNumer();
 }

 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);

 bitmap.Dispose();
 Grfx.Dispose();
 }

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


<body>
 <form id="form1" runat="server">
 <div>
 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>
 </div>
 </form>
</body>

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";
 }
 else
 {
 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…

Thanks.

Advertisements

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”, “system.windows.interactivity” and “system.windows.browser”

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"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
 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">
 </StackPanel>
 <Image x:Name="DropImage" Margin="0,100,0,0" Width="400" Cursor="Hand">
 <i:Interaction.Behaviors>
 <ei:MouseDragElementBehavior ConstrainToParentBounds="True"></ei:MouseDragElementBehavior>
 </i:Interaction.Behaviors>
 </Image>
 </Grid>
</UserControl>

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()
 {
 InitializeComponent();
 }

 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())
 {
 img.SetSource(s);
 }

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

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

 DropCollection.Children.Add(newImage);
 }
 }
 }
}

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: