Friday, September 16, 2011

Visual Studio 11


    Today in the BUILD keynote I had the opportunity to show some of the new functionality in Microsoft® Visual Studio® 11 Developer Preview and Microsoft® Team Foundation Server Preview.  MSDN subscribers can download the previews today as well as the new release of .NET Framework 4.5 Developer Preview; general availability is on Friday, September 16.
Some exciting announcements are being made here at BUILD.  Visual Studio 11 provides anintegrated development experience that spans the entire lifecycle of software creation from architecture to code creation, testing and beyond. This release adds support for Windows 8 and HTML 5, enabling you to target platforms across devices, services and the cloud.  Integration with Team Foundation Server enables the entire team to collaborate throughout the development cycle to create quality applications.
.NET 4.5 has focused on top developer requests across all our key technologies, and includes new features for Asynchronous programming in C# and Visual Basic, support for state machines in Windows Workflow, and increased investments in HTML5 and CSS3 in ASP.NET.
We’ve shared a lot at BUILD already, for more on the future of Windows development I suggest you take a look at Steven Sinofsky and S. Somasegar’s blogs. More details on Team Foundation Server including the new service announced at BUILD and how we’re helping teams be more productive can be found on Brian Harry’s blog.

Tuesday, September 6, 2011

Image Thumbnail Creation using C#.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, September 3, 2011

ASP.NET DataTable To Excel


   EXPORT DATA FROM DATATABLE TO EXCEL IN ASP.NET
VB - Code

Public Sub ExportExcel(ByVal Datadt As DataTable)
        Dim dt as new datatable
        dt = Datadt       
        Dim attachment As String = "attachment; filename=" & System.DateTime.Now.[Date].ToShortDateString() & "_SalesReport.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        Dim tab As String = ""
        For Each dc As DataColumn In dt.Columns
            Response.Write(tab + dc.ColumnName)
            tab = vbTab
        Next
        Response.Write(vbLf)
        Dim i As Integer
        For Each dr As DataRow In dt.Rows
            tab = ""
            For i = 0 To dt.Columns.Count - 1
                Response.Write(tab & dr(i).ToString())
                tab = vbTab
            Next
            Response.Write(vbLf)
        Next
        Response.[End]()
    End Sub

C# - Code

    public void ExportExcel(DataTable Datadt)
    {
        DataTable dt = new DataTable();
        dt = Datadt;
        string attachment = "attachment; filename="+System.DateTime.Now.Date.ToShortDateString()+"_SalesReport.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-excel";
        string tab = "";
        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write(tab + dc.ColumnName);
            tab = "\t";
        }
        Response.Write("\n");
        int i;
        foreach (DataRow dr in dt.Rows)
        {
            tab = "";
            for (i = 0; i < dt.Columns.Count; i++)
            {
                Response.Write(tab + dr[i].ToString());
                tab = "\t";
            }
            Response.Write("\n");
        }
        Response.End();
    }

Localizing ASP.NET Application


  Localizing an ASP.NET Application

Globalization is the process of designing and developing applications that function for multiple cultures, and localization is the process of customizing your application for a given culture and locale. The topics in this section describe how to create ASP.NET Web applications that can be adapted to different languages and cultures.

Example for Localization:-

Imports System.Threading.Thread
Imports System.Threading
Public Class Globalization
    Public Sub New()
        ' Creating a Global Culture specific to our application.
        Dim cultureInfo As New System.Globalization.CultureInfo("en-US")
        ' Creating the DateTime Information specific to our application.
        Dim dateTimeInfo As New System.Globalization.DateTimeFormatInfo()
        ' Defining various date and time formats.
        dateTimeInfo.DateSeparator = "/"
        dateTimeInfo.LongDatePattern = "MM/dd/yyyy"
        dateTimeInfo.ShortDatePattern = "MM/dd/yyyy"
        ' Setting application wide date time format.
        cultureInfo.DateTimeFormat = dateTimeInfo
        ' Assigning our custom Culture to the application current thread.
        'Application.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentCulture = cultureInfo
        Thread.CurrentThread.CurrentUICulture = cultureInfo
    End Sub
End Class


Create an instance for the above class in the base form of your project. 
Now the datetime will be treated in the format of  MM/dd/yyyy

Expression Web 4 Add-Ins


Some Expression Web 4 Add-Ins:
Here are some Expression Web add-ins you can use:
Insert Bing Map Add-in by Mike Calvo (read his overview of this add-in)
Expression Web 4 Add-Ins by fellow MVP Ian Haynes
Other Add-Ins