Tuesday, February 19, 2013

Image resizing in asp.net

Introduction:-

            This tutorial will show you how to generate image thumbnails dynamically from an original big image.

Background:-

            Many of the web developers will show the big images as small by giving the attribute values for the image tag.  This will slow down the webpage loading.  For better performance the best method is creating the small thumbnail images for showing small size image on the web pages.  But for every upload of the images by the user we cannot create a thumbnail image, it’s a tedious process.  I planed to create a method which will create a thumbnail automatically while the image upload and saves in a separate path.  This is a light weight process and easy to use.  The only thing is you have to add one more filed for saving the thumbnail image path while dealing with databases.

Following are the features of this technique.
  • Good Quality.
  • Desired thumbnail size.

Using the code:-

            Below is the method which will generates the thumbnail and saves to the destination folder.

public void GenerateThumbNail(string sourcefile, string destinationfile,int width)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(sourcefile));
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        int thumbWidth = width;
        int thumbHeight;
        Bitmap bmp;
        if (srcHeight > srcWidth)
        {
            thumbHeight = (srcHeight / srcWidth) * thumbWidth;
            bmp = new Bitmap(thumbWidth, thumbHeight);
        }
        else
        {
            thumbHeight = thumbWidth;
            thumbWidth = (srcWidth / srcHeight) * thumbHeight;
            bmp = new Bitmap(thumbWidth, thumbHeight);
        }

        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
        bmp.Save(Server.MapPath(destinationfile));
        bmp.Dispose();
        image.Dispose();
    }

      In the above method we are passing three parameters first one is the soruce location of the image and the second one is the destination location of the image and the third parameter is the width.  At first we need to include two namespaces

using System.Drawing;
using System.Drawing.Drawing2D;

Then create an instance image from the source file as follows

System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(sourcefile));

After getting the image height and width using the instance of the image we need to set the targetted thumbnail image’s height and width. Create and instance for the bitmap.  By using the below logic we can get the width and height of the targetted thumbnail.  Set the thumbnails’s width and height to the bitmap.

Bitmap bmp;
        if (srcHeight > srcWidth)
        {
            thumbHeight = (srcHeight / srcWidth) * thumbWidth;
            bmp = new Bitmap(thumbWidth, thumbHeight);
        }
        else
        {
            thumbHeight = thumbWidth;
            thumbWidth = (srcWidth / srcHeight) * thumbHeight;
            bmp = new Bitmap(thumbWidth, thumbHeight);
        }

After this for creating the high quality thumbnails create an instance for the Graphics class with the bitmap.  Set the SmoothingMode and CompositingQuality to HighQuality and InterpolationMode to Hign.  Then draw a rectangle with the thumbnail’s height and width using the rectangle class and place the image inside the rectangle using the DrawImage class.  Now the bitmap image i.e., thumbnail image is ready.  Now save the thumbnail the the destination location using bmp.save(/*destination location*/) method.

            I think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes.



Saturday, February 16, 2013

RECOVERING SQLSERVER "SA" LOGIN PASSWORD


The time may occur that you may forget or lost your sql serversa” or “sysadmin” login’s password.  This process will give you an opportunity for retaining forgot or lost password of your sqlserver’ssa” login.

  1. Select “SQL SERVER CONFIGURATION MANAGER” from start menu. 




2. Select SQLSERVER from the left panel and right click on “SQL SERVER (MSSQLSERVER)” from the right panel.  Select stop from the context menu appeared for stopping the sqlserver service.


3. After stopping the service again right click on the SQLSERVER service and select properties.  In properties window select advanced tab.  Now for the startup parameter add “-m;” at the starting position of the string as show in the below diagram.


4. After changing the startup parameter again start the sqlserver service.

5. Now open the command prompt and from here we will create a user with the sysadmin role and by using this users login we are going to change the sa login’s password.

6.Open the command prompt and type “SQLCMD”, So that you will change to the sql prompt.

7.By the following command create new user with name “recovery”
CREATE LOGIN recovery WITH PASSWORD=’dileep1234’
GO
You have finished creating a user now you should assign a role to this user as SYSADMIN by the following command.
Sp_addsrvrolemember ‘recovery’,’sysadmin’
GO



8. Now again stop the SQLSERVER service and go to its properties and remove “-m;” in the startup parameter and againg start the service.

9.Now login to the newly created recovery account and change the password of the “SA” login’s password.

That’s it you have now successfully use this account with your new password.

Monday, February 4, 2013

Page Wise Results from Sql Server

Getting page wise results is one of the most important factor while considering the performance of the application.  Getting a lot amount of data from database and showing the required data to the user will lack the performance of the application.  In such cases we can go for retrieving page wise results i.e., the amount of data which is required for user. the Here is the Query for Getting Page wise results from an sqlserver.

This query uses a Northwind database for giving an example.




Use Northwind

DECLARE @startrow int
DECLARE @endrow int

set @startrow=10
set @endrow=20

SELECT Mstr.* FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY CUS.CustomerID DESC) AS SlNo, CUS.CustomerID,CUS.ContactName,CUS.City FROM Customers CUS
)
Mstr WHERE SlNo BETWEEN @startrow AND @endrow ORDER BY CustomerID DESC


Thanks & Regards
Happy Coding...