Monday, January 26, 2009

Resizing image on the fly!


public partial class Thumbnail : System.Web.UI.Page
{
const string Q_IMG = "img";
const string Q_WIDTH = "w";
const string Q_HEIGHT = "h";

const int DEFAULT_WIDTH = 120;
const int DEFAULT_HEIGHT = 118;

int Thumbnail_Width
{
get
{
if (Request.QueryString[Q_WIDTH] != null)
return Convert.ToInt32(Request.QueryString[Q_WIDTH]);
else
return DEFAULT_WIDTH;
}
}

int Thumbnail_Height
{
get
{
if (Request.QueryString[Q_HEIGHT] != null)
return Convert.ToInt32(Request.QueryString[Q_HEIGHT]);
else
return DEFAULT_HEIGHT;
}
}

///
/// just a dummy delegate for the required GetThumbnailImageAbort delegate
///
///
public bool ImgAbort_Callback()
{
return false;
}


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString[Q_IMG] != null)
{
try
{
using (System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(Request.QueryString[Q_IMG])))
{
using (System.Drawing.Image thumb = img.GetThumbnailImage(
this.Thumbnail_Width,
this.Thumbnail_Height,
new System.Drawing.Image.GetThumbnailImageAbort(ImgAbort_Callback),
IntPtr.Zero))
{
Response.ContentType = "image/jpeg";
thumb.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
catch { }
}
}
}
}


Share/Save/Bookmark

Monday, January 12, 2009

Globalization and Localization in ASP.Net 2.0

I would really love to write about this topic that my drool flooded my laptop and broke it. I just implemented this on my new project and its working perfectly compared to the one our company is currently using. So without further ado, i would like to present...............the link, everything you need to know is already in there.

Share/Save/Bookmark

Saturday, January 3, 2009

Simple Delegate and Event Implementation Sample


class definition:

public class DelegateAndEvent
{
//it is a good practice to name your delegates with "EventHandler"
public delegate void ErrorEventHandler(DelegateAndEvent sender, ErrorEventArgs args);

//declare event
public event ErrorEventHandler ErrorRaised;

//a better way of returning values is to create a class which inherits the EventArgs class and add the
//desired property(ies)
public class ErrorEventArgs : EventArgs
{
private string _errmsg;
public string ErrorMessage
{
get { return _errmsg; }
set { _errmsg = value; }
}

public ErrorEventArgs() { }
public ErrorEventArgs(string error)
{
_errmsg = error;
}
}

/// Method to allow raising of error (can either be public or private)
public void RaiseError(string error)
{
if (!string.IsNullOrEmpty(error))
{
//check is event is not null (event has a subscriber)
if (ErrorRaised != null)
{
ErrorRaised(this, new ErrorEventArgs(error));
}
}

}
}


code behind:


public partial class _Default : System.Web.UI.Page
{
DelegateAndEvent dne = new DelegateAndEvent();

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dne.ErrorRaised += new DelegateAndEvent.ErrorEventHandler(dne_ErrorRaised);
}

protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnEvent_Click(object sender, EventArgs e)
{
dne.RaiseError("Error was forced to be raised!");

}

void dne_ErrorRaised(DelegateAndEvent sender, DelegateAndEvent.ErrorEventArgs args)
{
lblError.Text = args.ErrorMessage;
}
}




Share/Save/Bookmark