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

Thursday, November 29, 2012

How to make user friendly date in PHP like time ago.

In new web 2.0 displays time and date not in old style style like 07 Jan 2000 12.30 pm. The new generation need relative time like 2 hours ago, 6 months ago, 4 years ago, very old etc. 

So if you save the timestamp of a particular content creation in database we can simply show this ‘ago time calculation’ using the following algorithm. I have create simple function for this :-


function TimeAgo($timestamp)
{
 $difference = time() - $timestamp;
 
 $periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
 $lengths = array("60","60","24","7","4.35","12","10");
 for($j = 0; $difference >= $lengths[$j]; $j++)
 {
  $difference /= $lengths[$j];
 }
 
 $difference = round($difference);
 if($difference != 1) $periods[$j].= "s";
 
 $text = "$difference $periods[$j] ago";
 return $text;
}
 
To call this function you have to convert your datetime value in timestamp of PHP. foreg :-

TimeAgo(strtotime($datetimevalue))

Friday, November 23, 2012

FileUpload control is not working within update panel in asp.net

Hi All,

Today i face a problem with uploading a file using file upload control of asp.net. Because it was in the update panel. It's giving blank in fupImage.FileName. But after searched on web i found a solution on Here. 

It says that paste this code in your page load part :-

Page.Form.Attributes.Add("enctype", "multipart/form-data");
But this was not worked for me :( Finally after so many searches i found a simple solution for this. That i want to share with you :-
 
Solution :

You just need to make the your button to upload the file as the trigger of the Upload panel Something like this : -



 

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

Wednesday, October 17, 2012

What is difference between the GET and POST methods?

  1. In GET Method, All the name value pairs are submitted as a query string in URL. But in the POST method name value pairs are submitted in the Message Body of the request. 
  2. GET Method is not secured as it is visible in plain text format in the Location bar of the web browser. But POST Method is secured because Name-Value pairs cannot be seen in location bar of the web browser. 
  3. Also in GET method characters were restricted only to 256 characters. But in the case of POST method characters were not restricted. 
  4. About form default, Get is the defualt method for any form, if you need to use the post method, you have to change the value of the attribute "method" to be Post.  
  5. About the data type that can be send, with Get method you can only use text as it sent as a string appended with the URL, but with post is can text or binary. 
  6. If get method is used and if the page is refreshed it would not prompt before the request is submitted again. And if post method is used and if the page is refreshed it would prompt before the request is resubmitted. 
  7. GET method can be bookmarked. But POST method can not be bookmarked.
  8. In GET method data is always submitted in the form of text. In POST method data is submitted in the form as specified in enctype attribute of form tag and thus files can be used in FileUpload input box.
  9. GET method should not be used when sending passwords or other sensitive information. POST method used when sending passwords or other sensitive information.
  10. GET method can be cached. POST method can not cached.

Friday, October 12, 2012

What is the difference between String and string

string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32.

As far as guidelines, I think it's generally recommended to use string any time you're referring to an object. e.g.


string place = "world";

Likewise, I think it's generally recommended to use String if you need to refer
specifically the class. e.g.


string greet = String.Format("Hello {0}!", place);

How To View Remote Desktop in Full Screen Mode in Windows 7

Hi,

-Start "Remote Desktop Connection".
-Click on "Options".
-Click on the "Display" tab.
-On "Display configuation" settings, you can change the "Remote Desktop Connection" display by moving the slider from "Small to Large".
-By moving the "Slider" all the way to large, the display settings will automatically set to "Full Screen".

Thanks