Pages

Thursday, July 23, 2009

Page Life cycle and its events

There is a complete cycle of an ASP.net page, that is called the Page Life cycle. here are the various stages and the corresponding event for all that stages,


Page Initialization -- Page_Init

View State Loading -- LoadViewState

Postback data processing -- LoadPostData

Page Loading -- Page_Load

PostBack Change Notification -- RaisePostDataChangedEvent

PostBack Event Handling -- RaisePostBackEvent

Page Pre Rendering Phase -- Page_PreRender

View State Saving -- SaveViewState

Page Rendering -- Page_Render

Page Unloading -- Page_UnLoad


Happy Coding!!!!!!!!!!!!!!

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra

Tuesday, July 7, 2009

Retrieving Image from SQL Server

hi,

Please check out this code for retrieving the image from the sql server, first get the image field data in a data reader and from there we can convert the data into a BYTE array, using that array the file can be created..

have a look

byte[] file = SqlConvert.ToByteArray(dr["FileImage"]);

string fileName = SetupFileName(SqlConvert.ToString(dr["FileName"]));

string filePath = System.Environment.CurrentDirectory + “\\myFiles\\” + fileName;

FileStream fs
= new FileStream(filePath, FileMode.Create, FileAccess.Write);

BinaryWriter bw
= new BinaryWriter(fs);

bw.
Write(file);

bw.
Flush();

bw.
Close();

fs.
Close();


Happy Coding!!!! :)

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra
INDIA

Saving the image in SQL Server

hi,

The following is the sample code to show that how we can save the image in the SQL Server. we have to take the column type as IMAGE, and using the BYTE array we can save the uploaded image.

please check the following code for this.


SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
con.
Open();
byte[] ImgBytes = new byte[flurl.PostedFile.InputStream.Length];
flurl.
PostedFile.InputStream.Read(ImgBytes, 0, ImgBytes.Length);
string qry = "Insert into Table1(ImageData) values(@ImageData)";
SqlCommand cmd
= new SqlCommand(qry, con);
cmd.
Parameters.AddWithValue("@ImageData", ImgBytes);
cmd.
ExecuteNonQuery();
con.
Close();

Happy Coding!!!!!!

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra
INDIA

Monday, July 6, 2009

Creating Thumbnail Images in C#

Hi All,

The image processing is a very typical task, but still it is simple to do. There are application where we need to create the thumbnail of the uploaded images, at that time we need to resize the image. here is the sample code through which we can resize the image easily..
We just need to use the 2-3 main name spaces that are

1. System.Drawing
2. System.Drawing.Imaging
3. System.Drawing.Drawing2D

with the use of these name spaces we can do our task. i have made a function by which we can create the thumbnail of the imgae, we just need to pass some parameter like image data in Byte, path to save the image, required width and the height of the image.

please see the sample function below..

using System;
using System.IO;
using System.Web;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.Security;
using System.Configuration;

///
/// Summary description for ImgUtility
///

public class ImgUtility
{
static string strFileName;
public ImgUtility()
{
//
// TODO: Add constructor logic here
//

}
const int sizeThumb = 69;

public static string uploadImage(string sPath, Stream data)
{
string dirName = string.Empty;
string newPath = string.Empty;
int length = Convert.ToInt32(data.Length);
byte[] origImageData = new byte[length];
data.
Read(origImageData, 0, length);
strFileName
= DateTime.Now.ToString("ddMMyyyy_HHmmss");
dirName
= Path.GetDirectoryName(sPath);
if (sPath.Contains("Avtaar") != true)
{
if (sPath.Contains("features") == true)
{
newPath
= Path.Combine(dirName, @"features/" + strFileName + ".jpg");
return FixedSize(origImageData, newPath, 200, 108);
}
else
{
newPath
= Path.Combine(dirName, @"Thumbnail/" + strFileName + ".jpg");
FixedSize
(origImageData, newPath, 120, 85);
newPath
= Path.Combine(dirName, @"Photo/" + strFileName + ".jpg");
FixedSize
(origImageData, newPath, 500, 375);

//return @"\MyDiscoverySchool\Thumbnail\"+strFileName + ".jpg";
return @"Thumbnail/" + strFileName + ".jpg";
//return strFileName + ".jpg";
}
}
else
{
newPath
= Path.Combine(dirName, strFileName + ".jpg");
return FixedSize(origImageData, newPath, 76, 94);
}
}

public static string FixedSize(byte[] fullsize, string sPath, int Width, int Height)
{
Image imgPhoto
;
imgPhoto
= Image.FromStream(new MemoryStream(fullsize));
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;



nPercentW
= ((float)Width / (float)sourceWidth);
nPercentH
= ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent
= nPercentH;
destX
= System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent
= nPercentW;
destY
= System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto
= new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.
SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto
= Graphics.FromImage(bmPhoto);
grPhoto.
Clear(Color.White);
grPhoto.
InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.
DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

grPhoto.
Dispose();
bmPhoto.
Save(sPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//return bmPhoto;
return strFileName;
}
}

Happy Coding!!!!!!!!!! :)

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra
INDIA

Kontera