How to Detect the user Close the page

Posted by Venkat | Labels:

You can use the onunload or the onbeforeunload events to execute codes when the browser is about to closed.

You may also use Page Methods, see below

http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide.all

The code below will simulates the closed button of the browser when the user invoked the button and display an alert message..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function doSomething()
{
alert('Widnow is about to close');
}
</script>

</head>
<body onunload="doSomething();">
<form id="form1" runat="server">

</form>
</body&gt;

Small trick to hide HTML element

Posted by Venkat | Labels:

In this post I’m going to only show a simple trick how to hide an image by using XPath expression and by not adding “server side” code into codebehind or inside a server side script block. I got this simple idea from a post I recently answered on the ASP.Net forum. Maybe some other may find this useful.


<img runat="server" visible='<%# (XPath("@value") == "35" ) %>' src='<%# "images/" + XPath("@img") + ".gif" %>'>


The XPath expression for the @value attribute will return true if the value is 35, if not it will return false. The visible attribute that is added to the img element, can only hide an element if it’s turned into a html server control. That is done by adding the runat attribute.


Ref: http://fredrik.nsquared2.com/ViewPost.aspx?PostId=324

Watermark of images

Posted by Venkat | Labels: ,

We are going to discuss about watermarking the image , the importance of doing the watermark is protected and not to be redistributed , Suppose if you take some online gallery images , like arts,some site which selling photos are restricted his site image. They might be not to allow users to save image by right click on image , or saving the web page.


'Creating Dynamic Watermark on image
Dim objImage As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("~/images/" & "Management.jpg"))
'From File
Dim height As Integer = objImage.Height
'Actual image width
Dim width As Integer = objImage.Width
'Actual image height
Dim bitmapimage As New System.Drawing.Bitmap(objImage, width, height)
' create bitmap with same size of Actual image
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmapimage)
'Creates a System.Drawing.Color structure from the four ARGB component
'(alpha, red, green, and blue) values. Although this method allows a 32-bit value
' to be passed for each component, the value of each component is limited to 8 bits.
'create Brush
Dim brush As New SolidBrush(Color.FromArgb(113, 255, 255, 255))
'Adding watermark text on image
g.DrawString("Copywright", New Font("Arial", 18, System.Drawing.FontStyle.Bold), brush, 0, 100)
'save image with Watermark image/picture
'bitmapimage.Save("watermark-image.jpg"); //if u want to save image
Response.ContentType = "image/jpeg"
bitmapimage.Save(Server.MapPath("~/images/Copy.jpg"), ImageFormat.Jpeg)
bitmapimage.Dispose()
objImage.Dispose()

Resize an image using asp.net (vb.net) - Method 2

Posted by Venkat | Labels:

This is the second method of dynamically resizing the image ,


Resize an image using asp.net (vb.net) while uploading it into the server
======================Copy following code to Invoke upload======================

Dim fileExt = System.IO.Path.GetExtension(FileUpload.FileName).ToLower()
Dim previewName As String = Me.txtName.Text & "_preview" & fileExt
If fileExt.ToString.ToLower = ".jpeg" Or fileExt.ToString.ToLower = ".jpg" Or
fileExt.ToString.ToLower = ".gif" Then
Dim previewPic As Bitmap = (ResizeImage(FileUpload.PostedFile.InputStream, 500,
695))
previewPic.Save(Server.MapPath("Images/preview/") & previewName,
ImageFormat.Jpeg)
previewPic.Dispose()
End If
==================Copy following Function for resizing image====================
Function ResizeImage(ByVal streamImage As Stream, ByVal maxWidth As Int32, ByVal
maxHeight As Int32) As Bitmap

Dim originalImage As New Bitmap(streamImage)
Dim newWidth As Int32 = originalImage.Width
Dim newHeight As Int32 = originalImage.Height
Dim aspectRatio As Double = Double.Parse(originalImage.Width) /
Double.Parse(originalImage.Height)
If (aspectRatio <= 1 And originalImage.Width > maxWidth) Then
newWidth = maxWidth
newHeight = CInt(Math.Round(newWidth / aspectRatio))
Else
If (aspectRatio > 1 And originalImage.Height > maxHeight) Then
newHeight = maxHeight
newWidth = CInt(Math.Round(newHeight * aspectRatio))
End If
End If
Dim newImage As New Bitmap(originalImage, newWidth, newHeight)
Dim g As Graphics = Graphics.FromImage(newImage)
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height)
originalImage.Dispose()
Return newImage
End Function

Resize Image using asp.net Method - 1

Posted by Venkat | Labels: ,

Here We have to discuss, how to dynamically resize the images using asp.net , generally we used two image one as Thumbnail and another one is big image , so if you upload the big image , we can set the default height and width on code to resize that image , at the same while we upload small image , on resizing the image it will be stretched to the specified height and width and also not seen qualify of image is not good ,

so here , we calculate the width , as per we resize the image

I would prefer this method.

this is the First Method , for this place one fileupload control and button on you design form


Dim thumbWidth As Integer = 132
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
'Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
Dim srcWidth As Integer = image.Width
Dim srcHeight As Integer = image.Height
Dim thumbHeight As Integer = (srcHeight / srcWidth) * thumbWidth
Dim bmp As New Bitmap(thumbWidth, thumbHeight)
'Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
'Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
'Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
'Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High

' Draw the original image into the target Graphics object scaling to the desired width and height
Dim rectDestination As New System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight)
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel)

'Save to destination file
bmp.Save(Server.MapPath("~/images/" & FileUpload1.PostedFile.FileName))

' dispose / release resources
bmp.Dispose()
image.Dispose()

Get the directories

Posted by Venkat | Labels:

Here is the code how to get all directories and save them to a List:


//Path with all directories in

String Path = Server.MapPath("Folder1\\"); //Declare List

List<String> GetThreads = new List<String>();

//Get all direcotories

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Path);

foreach (System.IO.DirectoryInfo file in dir.GetDirectories())

{

//This saves all Direcory names like "Folder1", "Folder2", "Folder3", "Folder4";

GetThreads.Add(file.Name);

}

Validate textbox it should contains atleast 20 characters

Posted by Venkat | Labels:

Here we have to discuss the validation of the field , the textbox should contain atleast 20 Characters


ValidationExpression=^.{20,1999}$ - >

This expression should accept atleast 20 charactes and maximum of 1999 - it does not accept less than 20 characters

If I assumed that you want to enter only alphabets ie a-z into your textbox for which the validation expression is:

ValidationExpression="^\D{20,}$"

For having both alphabets and digits expression:

ValidationExpression="^[a-z0-9]{20,}$"

Your expression below will allow all alphabets, digits as well as special chars as #, $ etc to be entered incase thats what you want:

ValidationExpression="^.{20,1999}$"

Validate URL

Posted by Venkat | Labels: ,

Validate URL 

<asp:RegularExpressionValidator 
            ID="RegularExpressionValidator1"
            runat="server" 
            ValidationExpression="(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"
            ControlToValidate="TextBox1"
            ErrorMessage="Input valid Internet URL!"
            ></asp:RegularExpressionValidator> 
 
Here i update the Regex this will accept the URL like:
http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html 

Validate US Zip Code

Posted by Venkat | Labels: ,

To validate US Zip Code


<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="\d{5}(-\d{4})?"
ControlToValidate="TextBox1"
ErrorMessage="Input valid U.S. Zip Code!"
></asp:RegularExpressionValidator>

Validate US Phone number

Posted by Venkat | Labels: ,

Validate US Phone number


<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
ControlToValidate="TextBox1"
ErrorMessage="Input valid U.S. Phone Number!"
></asp:RegularExpressionValidator>

PayOffers.in