Showing posts with label .Net Tips/Tricks. Show all posts
Showing posts with label .Net Tips/Tricks. Show all posts

Thursday, May 19, 2011

How to copy an image from an URL to own server and resize it


First: Download the image with Url.
Second: Resize the image and save it into disk.

<!   1) For the first requirement, you can use WebClient class. Refer to the following code
  private void Page_Load(object sender, System.EventArgs e)
    {
        WebClient wc = new WebClient();

        byte[] data = wc.DownloadData("http://www.google.cn/intl/en-us/images/logo_cn.gif");

        MemoryStream ms = new MemoryStream(data);

        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

        GetThumbnailImage(img);//Resize Image
    }
   
->   2) For the second requirement refer to the code below:
private void GetThumbnailImage(System.Drawing.Image img)
    {
        float iScale = img.Height > img.Width ? (float)img.Height / 100 : (float)img.Width / 100;

        img = img.GetThumbnailImage((int)(img.Width / iScale), (int)(img.Height / iScale), null, IntPtr.Zero);

        MemoryStream memStream = new MemoryStream();

        img.Save(Server.MapPath("att.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);

        memStream.Flush();
    }

Custom Rating Control, create your own simple rating control


I have implemented rating control in one of my application by using two images (blue star and white star), and written c# code for displaying the rating selected by the user, you can store the selected rating in the database also and display accordingly.

You might be wondering if rating controls are already available then why build from scratch, your thinking is absolutely right; however I wanted a simple light weight control for my application so that I can change anything I want.
First what is does it display 5 rating white stars and when user click on any star let’s say click on 3rd  then you have to replace the image URL of previous 2 images and the clicked one to blue (whatever color you like), so it will become 3 blue star.
Lets have a look at the sample code. You just need to pass the rating value to the setFeebackScore method; it will set the images and display the score description accordingly. You can create this control as a component and use it wherever you want in your application.

  private const string IMAGE_FEEDBACK_BLUE      = "/_layouts/images/neon_FeedbackBlue.JPG";
  private const string IMAGE_FEEDBACK_WHITE   = "/_layouts/images/neon_FeedbackWhite.JPG";

  private void setFeedbackScore(float rating)
        {
            if (rating == float.Parse("1"))
            {
                imgPoor.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgFair.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgVeryGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgExcellent.ImageUrl = IMAGE_FEEDBACK_WHITE;

                lblScoreDescription.Text = "(Poor)";
            }
            else if (rating == float.Parse("2"))
            {
                imgPoor.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgFair.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgVeryGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgExcellent.ImageUrl = IMAGE_FEEDBACK_WHITE;

                lblScoreDescription.Text = "(Fair)";
            }
            else if (rating == float.Parse("3"))
            {
                imgPoor.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgFair.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgGood.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgVeryGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgExcellent.ImageUrl = IMAGE_FEEDBACK_WHITE;

                lblScoreDescription.Text = "(Good)";
            }
            else if (rating == float.Parse("4"))
            {
                imgPoor.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgFair.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgGood.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgVeryGood.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgExcellent.ImageUrl = IMAGE_FEEDBACK_WHITE;

                lblScoreDescription.Text = "(Very Good)";
            }
            else if (rating == float.Parse("5"))
            {
                imgPoor.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgFair.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgGood.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgVeryGood.ImageUrl = IMAGE_FEEDBACK_BLUE;
                imgExcellent.ImageUrl = IMAGE_FEEDBACK_BLUE;

                lblScoreDescription.Text = "(Excellent)";
            }
            else
            {
                imgPoor.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgFair.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgVeryGood.ImageUrl = IMAGE_FEEDBACK_WHITE;
                imgExcellent.ImageUrl = IMAGE_FEEDBACK_WHITE;

                lblScoreDescription.Text = string.Empty;
            }

Tuesday, August 3, 2010

How to keep session alive in asp.net application


Friends, many times we get requirements to increase the session timeout expiry time on the application server, so what we do basically we go in web.config file and set session time out to some time period, by default its 20 mins, but remember sometime you increase time period in web.config file and it doesn't work why?? The reason is you also have to check your application pool recycling time period it should be more than the session expiry period which you are doing in web.config file.

 Yo! But anyways you did not avoid session expiration in your application, session will still expire in your application let's say if user filling a long form in your application or kept a page open and gone out for coffee. So now what will happen? Session will expire right? By the time use will be back and resume with the work.

Some people do session resetting in code behind by checking if session is null then read User ID () from cookies and reset it. Or if it is intranet application and AD authentication then you get the current logged in user in system and set it in session.Now if you don't wanna do all above crap In your application, what all you wanna do is don't let session to get expire unless user close the browser. To implement this behavior in your applications simply follow below steps.
  1. First of all let me give you some brief about working session state in asp.net. By default it is set to 20 min, so if user sends a request to the server (in simple language browse a page) then server stores the time when request came and extend the session time out to 20 min, so if after 10 min user send again a request so it keeps extended to 20 min, if user don't send any request to server till 20 min then session get expires. This is basic working of session state.

  2. From first step you know that, server need to get any request from client to keep session alive, so now we can think of some methodology to keep pinging the server, below is the script which will keep sending request to the server and will keep session alive.

    Create a SessionCheck.aspx page in your application; you can keep this page blank. So that if it gets called from the browser then it should not be over loaded.

    Using Java Scrip:
    Image will be used to keep session alive by changing image src, assigning this property to some 
    <img id="imgSessionCheck" width="1" height="1" />

    <script type="text/javascript" >
    //Variable used to prevent caching on some browsers and knowing the count how many times user sends request to server.
        var counter;
        counter = 0;

        function KeepSession() {
    // Increase counter value
        counter++;

        // Gets reference of image
        var img = document.getElementById("imgSessionCheck");

        // Set new src value, which will send a request to the server
    img.src = "http://servername:port/appName/SessionCheck.aspx?count=" + counter;

        // now schedule this process to happen in some time interval, in this example its 1 min
        setTimeout(KeepSession, 60000);
    }

        // Call this function for a first time
        KeepSession();
        </script>

    Using JQuery:
    <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script language="javascript" type="text/javascript">

        function KeepSession() {
        // A request to server
        $.post("http://servername:port/appName/SessionCheck.aspx");

        //now schedule this process to happen in some time interval, in this example its 1 min
        setInterval(KeepSession, 60000);
    }

        // First time call of function
        KeepSession();
     </script>

    So it's simple just you need to put this script in your page.

    Thanks for ready this post.

    Happy Coding!!!