Saturday, June 26, 2010

Resizing images without loss of quality in .Net

Recently I had faced a problem regarding photos that I had captured using my digicam.
When I capture photo using my cannon digicam, the size of each photo is 2MB or more.
So it is very difficult to upload those photos in web because of their size.
And compress those photos one by one using Microsoft Office Picture Manager is difficult too.
So I wrote some code to resize those photos and to minimize the size without affecting the photo quality.
The code is as follow.........

public void ResizeFromStream(string ImageSrcPath, string ImageSavePath, int MaxSideSize)
{
try
{
int intNewWidth;
int intNewHeight;
Image imgInput = Image.FromFile(ImageSrcPath);

//Determine image format
ImageFormat fmtImageFormat = imgInput.RawFormat;

//get image original width and height
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;

//determine if landscape or portrait
int intMaxSide;

if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}


if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
//create new bitmap
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);

//save bitmap to disk
bmpResized.Save(ImageSavePath, fmtImageFormat);

//release used resources
imgInput.Dispose();
bmpResized.Dispose();
}
catch (Exception ex)
{ }
}

Tuesday, May 25, 2010

Error in Web Service: The test form is only available for requests from the local machine

I have faced a problem when try to access a Web service from a remote computer I cannot see the Invoke button. And received the following error message:

The test form is only available for requests from the local machine

The reason is .NET-connected Web services support HTTP GET, HTTP POST and SOAP protocols. By default, in .NET Framework 1.0, all three protocols are enabled. By default, in .NET Framework 1.1, HTTP GET and HTTP POST are both disabled for security reasons.

Applications that use HTTP GET or HTTP POST to invoke a Web service fail when the Web service is upgraded to .NET Framework 1.1.

The .NET Framework 1.1 defines a new protocol that is named HttpPostLocalhost. By default, this new protocol is enabled. This protocol permits invoking Web services that use HTTP POST requests from applications on the same computer.

To solve this problem we have to enable HTTP GET and HTTP POST by editing the Web.config file where the Web service resides.

<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>

Thursday, April 22, 2010

How to keep session alive in your site

Some days back I faced a problem of session lose. Today I am writing an article how to keep session alive in asp.net without increasing session time out for the site.
The steps are as follows
First create an aspx Page e.g SessionAlive.aspx.
Then write one line in code behind.

protected void Page_Load(object sender, System.EventArgs e)
{
Response.AddHeader("Refresh", Convert.ToString(( Session.Timeout * 60 ) -120 ));
}

Now in your Master Page add this line
<iframe id=”MaintainSession” src="SessionAlive.aspx" width=0 height=0 runat="server"></ iframe >

Now you can maintain Session as long as the website is open.

Wednesday, June 3, 2009

Place Cursor after some character in a Textbox

In one of my project requirement was to Place Cursor after some character in a Textbox.
because their was a textbox (lets say txtPhoneNo).The initial value of txtPhoneNo is (868).Now i will have to place the cursor after (868) whenever user will click on that textbox.
This is very interesting thing
I do this by writting a javascript function......

<script type="text/javascript">
function SetCursorToTextEnd(textControlID)
{
var text = document.getElementById(textControlID);
if (text != null && text.value.length > 0)
{
if (text.createTextRange)
{
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', 5);
FieldRange.collapse();
FieldRange.select();
}
}
}
</script>
Write this line in Page_Load of aspx.cs file
txtPhoneNo.Attributes.Add("onfocus", "javascript:SetCursorToTextEnd(this.id)");

Friday, March 6, 2009

How to select a row in a grid by clicking any where of that row

Here one more thing that i have faced in my way....that is How to select a row in a grid by clicking any where of that row....
Below is the sample grid view for that...



Then add this simple code on the RowDataBound event of that Grid

protected void grdPatient_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].FindControl("lnkName");

string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");

e.Row.Attributes["onclick"] = _jsSingle;

}

}

Friday, January 16, 2009

HTML Injection

Now I would like to share another interesting thing in web application. That is HTML Injection
HTML Injection refers to injecting HTML code into a web server’s response to alter
the content to the end user. This is also known as Cross Site Scripting.
Cross Site Scripting (XSS) attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user.
An attacker can use XSS to send malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by your browser and used with that site.

Let’s see how the problem can be solved.

validateRequest is a nice feature that tells ASP.NET whether to examine all data from the browser for potentially malicious input — particularly anything that looks like HTML or scripting that form the basis for many types of attacks, such as cross-site scripting. By introducing validateRequest and setting it to true by default, Microsoft has very effectively put a halt to some of the most common Web site attacks.

The first problem with validateRequest comes when you run an ASP.NET application on a server in many circumstances — painfully many — you’ll get an exception of “A potentially dangerous Request. Form value was detected from the client” when it detects uuencoded input. This breaks the application. In this case it is justified. If you don’t catch these kinds of attacks, your servers could be under a hacker’s control without your knowledge.

You can disable this for a single page by setting it to false in the page directive

<%@ Page ... validateRequest="false" %>

With this setting, ASP.NET won’t examine the data coming from the browser.

Secondly Never trust user input and always filter metacharacters. This will eliminate the majority of XSS attacks. Converting < and > to < and > is also suggested when it comes to script output.

Thursday, January 15, 2009

(CheckBox in HeaderTemplate to) Select All CheckBoxes inside a GridView using JavaScript

This question has been around for pretty long time.
Here is the Scenario; there is one TemplateColumn in GridView which is used to Select Row within GridView. So, there will be Checkbox inside ItemTemplate of that TemplateColumn. User would be able to select more than one Checkbox in that Column. And to Select All the Checkboxes of that column, there will be a "Select All" Checkbox in HeaderTemplate of that GridView. When user check "Select All" Checkbox in Header, all the checkboxes should be selected and vice versa And when an User will select all the checkbox individually the Select All checkbox should be selected and vice versa.

First of all I am showing the grid structure for this.










and Now the Required javascript for these.This one is for check/uncheck all Checkbox when Select All is clicked

function SelectAll(obj)
{
var item= <%=GridView1.Rows.Count%>;
if(obj.checked==true)
{
for(var i=2;i<=item+1;i++) { if(i<=9) document.getElementById("GridView1$ctl0"+i+"$chkChild").checked=true; else document.getElementById("GridView1$ctl"+i+"$chkChild").checked=true; } } else { for(var i=2;i<=item+1;i++) { if(i<=9) document.getElementById("GridView1$ctl0"+i+"$chkChild").checked=false; else document.getElementById("GridView1$ctl"+i+"$chkChild").checked=false; } } }

This one is for check/uncheck SelectAll Checkbox

function SelectParent(obj)
{ var item= <%=GridView1.Rows.Count%>;
var check=0;
for(var i=2;i<=item+1;i++)
{
if(i<=9)
{
if(document.getElementById("GridView1$ctl0"+i+"$chkChild").checked==true)
check++;
}
else
{
if(document.getElementById("GridView1$ctl"+i+"$chkChild").checked==true)
check++;
}
}
if(item==check)
document.getElementById("GridView1$ctl01$chkParent").checked=true;
else
document.getElementById("GridView1$ctl01$chkParent").checked=false;
}