Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Wednesday, February 27, 2013

How to make thumbnail of an image and fix this in center position of div using asp.net and c#

First you have to create a page of CreateThumbnail.aspx and paste this below code :-
    protected void Page_Load(object sender, EventArgs e)
    {
        string Image = Request.QueryString["Image"];

        if (Image == null)
        {
            this.ErrorResult();
            return;
        }

        string sSize = Request["Size"];
        int Size = 120;
        if (sSize != null)
            Size = Int32.Parse(sSize);

        string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image;
        Bitmap bmp = CreateThumbnail(Path, Size, Size);

        if (bmp == null)
        {
            this.ErrorResult();
            return;
        }

        string OutputFilename = null;
        OutputFilename = Request.QueryString["OutputFilename"];

        if (OutputFilename != null)
        {
            if (this.User.Identity.Name == "")
            {
                // *** Custom error display here
                bmp.Dispose();
                this.ErrorResult();
            }

            try
            {
                bmp.Save(OutputFilename);
            }

            catch (Exception ex)
            {
                bmp.Dispose();
                this.ErrorResult();
                return;
            }
        }

        // Put user code to initialize the page here
        Response.ContentType = "image/jpeg";
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        bmp.Dispose();
    }

    private void ErrorResult()
    {
        Response.Clear();
        Response.StatusCode = 404;
        Response.End();
    }

    public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
    {
        System.Drawing.Bitmap bmpOut = null;
        try
        {
            Bitmap loBMP = new Bitmap(lcFilename);
            ImageFormat loFormat = loBMP.RawFormat;

            decimal lnRatio;
            int lnNewWidth = 0;
            int lnNewHeight = 0;

            //*** If the image is smaller than a thumbnail just return it
            if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                return loBMP;

            if (loBMP.Width > loBMP.Height)
            {
                lnRatio = (decimal)lnWidth / loBMP.Width;
                lnNewWidth = lnWidth;
                decimal lnTemp = loBMP.Height * lnRatio;
                lnNewHeight = (int)lnTemp;
            }
            else
            {
                lnRatio = (decimal)lnHeight / loBMP.Height;
                lnNewHeight = lnHeight;
                decimal lnTemp = loBMP.Width * lnRatio;
                lnNewWidth = (int)lnTemp;
            }

            // System.Drawing.Image imgOut = loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight, null,IntPtr.Zero);

            // *** This code creates cleaner (though bigger) thumbnails and properly
            // *** and handles GIF files better by generating a white background for
            // *** transparent images (as opposed to black)
            bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
            Graphics g = Graphics.FromImage(bmpOut);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
            g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

            loBMP.Dispose();
        }
        catch
        {
            return null;
        }
        return bmpOut;
    }
How to call this page on demo page :-
Add this script to set image in center position of div :-
      
      

Monday, December 10, 2012

How to get query string value using javascript

String.prototype.GetQSValue = function() {
    if (!arguments[0]) return null;
    var strFL = unescape(this)
    var regEx = new RegExp("[?&]" + arguments[0] + "=.*$", "g");
    var strMatch = strFL.match(regEx);
    if (strMatch == null) return null
    var arrParts = strMatch.toString().split("&");
    return (arrParts[0].length == 0) ? arrParts[1].split("=")[1] : arrParts[0].split("=")[1];
}
How to call this function, you can see this in bottom. I am getting here "return" parameter's value.
var strLink = window.location.search;
var returnurl = strLink.GetQSValue('return');

Saturday, October 20, 2012

How to allow only Numeric values in input textbox using jQuery

$(document).ready(function() {
    $("#txtPrice").keydown(function(event) {
        // Allow: backspace, delete, tab, escape, and enter
        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
             // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) || 
             // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }   
        }
    });
});

Friday, September 4, 2009

Is Date function Using Javascript

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
var i;
for (i = 0; i < c =" s.charAt(i);"> "9"))) return false;
}
// All characters are numbers.
return true;
}

function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < c =" s.charAt(i);" 4 ="="" 100 ="="" 400 ="="" i =" 1;" i="="4" i="="6" i="="9" i="="11)" i="="2)" daysinmonth =" DaysArray(12)" pos1="dtStr.indexOf(dtCh)" pos2="dtStr.indexOf(dtCh,pos1+1)" strday="dtStr.substring(0,pos1)" strmonth="dtStr.substring(pos1+1,pos2)" stryear="dtStr.substring(pos2+1)" stryr="strYear">1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) { if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : dd/mm/yyyy")
return false
}
if (strMonth.length<1>12){
alert("Please enter a valid month")
return false
}
if (strDay.length<1>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || yearmaxYear){
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
alert("Please enter a valid date")
return false
}
return true
}
function ValidateDate(Obj)
{
if(Obj.value.replace(/\s/g,"") != "")
{
if (isDate(Obj.value)==false)
{
Obj.focus()
Obj.value="";
return false
}
}
return true
}

Is Numeric function using Javascript

function IsNumeric(txtvalue)
{
if(txtvalue.value != "")
{
if(isNaN(txtvalue.value))
{
alert("Please Enter Numeric Value")
txtvalue.focus();
txtvalue.value = "";
return false;
}
}
}

Is Alfa function using Javascript

function IsAlpha(txtvalue)
{
var numaric = txtvalue.value ;
for(var j=0; j
{
var alphaa = numaric.charAt(j);
var hh = alphaa.charCodeAt(0);
if((hh > 64 && hh<91)> 96 && hh<123) || (hh == 32) || (hh==46))
{
}
else
{
txtvalue.focus();
txtvalue.value = "";
alert('Please Insert [a-z] only number are not allowed!');
return false;
}
}
return true;
}

Print Using Javascript


function CallPrint()
{
var prtContent = document.getElementById('Print');
var WinPrint = window.open('','','top=0,width=800,height=700,left=120,scrollbars=yes,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
prtContent.innerHTML=strOldOne;
}