Thursday, September 13, 2012

Re size image or Generate Thumbnail in asp.net

Code for  re size image in asp.net, It also maintain the aspect ratio and image quality.

private void GenerateThumbnail(byte[] byteArray, int thumbnailSize)
        {
            // Convert byte array into stream
            Stream fStream = new MemoryStream(byteArray);
            Bitmap photo = new Bitmap(fStream);
            // maintain aspect ratio
            int width, height;
            if (photo.Width > photo.Height)
            {
                width = thumbnailSize;
                height = photo.Height * thumbnailSize / photo.Width;
            }
            else
            {
                width = photo.Width * thumbnailSize / photo.Height;
                height = thumbnailSize;
            }

            Size resizeImageSize = new Size(width, height);

            System.Drawing.Image img = System.Drawing.Image.FromStream(fStream);
            System.Drawing.Image thumbnailImage = img.GetThumbnailImage(resizeImageSize.Width, resizeImageSize.Height, null, IntPtr.Zero);

            // use high quality conversion
            using (Graphics graphic = Graphics.FromImage(thumbnailImage))
            {
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;


                Rectangle rect = new Rectangle(0, 0, resizeImageSize.Width, resizeImageSize.Height);
                graphic.DrawImage(thumbnailImage, rect);


                using (MemoryStream imageStream = new MemoryStream())
                {
                    thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    // convert back into byte array
                    byte[] imageContent = new Byte[imageStream.Length];
                    imageStream.Position = 0;
                    imageStream.Read(imageContent, 0, (int)imageStream.Length);
                   
                    context.Response.ContentType = "image/jpeg";
                    context.Response.OutputStream.Write(imageContent, 0, imageContent.Length);


                }
            }
        }


Thursday, August 9, 2012

Image file validation in File upload control ASP.NET




<!-- file Upload Control -->

 <asp:FileUpload ID="fuMainImage" runat="server" />

<!-- Image file validator -->

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="fuMainImage"   ErrorMessage="Invalid Image File (supported file type are .JPEG, .PNG, .GIF) "  ValidationExpression="^([0-9a-zA-Z :\\-_!@$%^&*()])+(.jpg|.JPG|.jpeg|.JPEG|.png|.PNG|.gif|.GIF)$">
</asp:RegularExpressionValidator>

Tuesday, August 7, 2012

Query to know Foreign key (FK) column in your database MSSQL

SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id

Query to know Primary key column in your database MSSQL


Query to know Primary key column in your database MSSQL...

SELECT i.name AS IndexName,
OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1 order by OBJECT_NAME(ic.OBJECT_ID)

Query to know identity column in your database MSSQL



Query to know identity column in your database MSSQL

select COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 order by TABLE_NAME

Wednesday, April 20, 2011

code for swap Silverlight xap in html


  <script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
        var IsLSC = true;
      // function create Object block inside Div tag
        function CreateSilverlightMenuTree(XapPath) {
            var userDetails = "";
            var objectValue = Silverlight.createObject(
                "/ClientBin/" + XapPath,  // source
               null,  // parent element
                "slPlugin",  // id for generated object element
                {
                width: "100%", height: "100%", background: "transparent", windowless: "true",
                version: "5.0.60401.0"
            }, { onError: onCustomError, onLoad: onCustomLoad },
              "userData=" + userDetails,
            "context"    // context helper for onLoad handler.
            );
            var divObj = document.getElementById("silverlightControlHost");
            divObj.innerHTML = objectValue;
        }
        function onCustomError() {
            window.status += " Error in loading window";
        }
        function onCustomLoad() {
            window.status += " Window loaded";
        }
//function  swap between two xap file dynamically 
        function SwitchXap() {
            var paramObj= "";
            if (IsLSC) {
                paramObj = "LSC.xap";
                IsLSC = false;
              }
            else {
                paramObj = "SilverlightNetwork.xap";
                IsLSC = true;
            }
            CreateSilverlightMenuTree(paramObj);
        }
    </script>

</head>
<body>
    <form id="form1" runat="server" style="height:100%">
    <input id="Button1" type="button" value="Swap" onclick="SwitchXap()" />
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
 <param id="paramXap" name="source" value="ClientBin/SilverlightNetwork.xap"/>
 <param name="onError" value="onSilverlightError" />
 <param name="background" value="white" />
 <param name="minRuntimeVersion" value="5.0.60401.0" />
 <param name="autoUpgrade" value="true" />

Wednesday, March 2, 2011

Call Scriptable Methods from JavaScript with Silverlight

Here is code snippet  to be written on html page










and here is code snippet to be written on xmal Page code behind