Tuesday, February 10, 2009

Upload and resize image.

Hi again,

And now we are face 2 face again, and now we'll upload a image to the server in desired size and resolution. here is the code for that.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

protected void Button1_Click(object sender, EventArgs e)
{
try
{
Boolean fileOK = false;
String path = Server.MapPath("~/Uploads/");
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}

if (fileOK)
{
UploadImage(FileUpload1);
}
else
{
Page.RegisterStartupScript("", "");
}
}
catch (Exception Ex)
{
throw Ex;
}
}
protected void UploadImage(FileUpload fileUploader)
{
try
{
if (fileUploader.HasFile)
{
int newX = 0, newY = 0;

int bmpH = 300;// //New Image target height
int bmpW = 300;// //New image target width

if (fileUploader.HasFile)
{
Random rnd = new Random();
Int32 i = rnd.Next();

String filePath = "Uploads/" + i + ".jpeg";

Bitmap upBmp = (Bitmap)Bitmap.FromStream(fileUploader.PostedFile.InputStream);

Decimal reDuce;

if (upBmp.Width > upBmp.Height)
{
reDuce = (Decimal)bmpW / (Decimal)upBmp.Width;

bmpH = ((Int32)(upBmp.Height * reDuce));
}
else
{
if (upBmp.Width < upBmp.Height)
{
reDuce = (Decimal)bmpH / (Decimal)upBmp.Height;

bmpW = ((Int32)(upBmp.Width * reDuce));
}
else
{
if (upBmp.Height == upBmp.Width)
{

reDuce = bmpH / upBmp.Height;

bmpW = ((Int32)((upBmp.Width * reDuce)));
}
}
}

Bitmap newBmp = new Bitmap(bmpW, bmpH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

newBmp.SetResolution(200, 200);

Graphics newGraphic = Graphics.FromImage(newBmp);
newGraphic.Clear(Color.Gray);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upBmp, newX, newY, bmpW, bmpH);
newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
catch (Exception Ex)
{
throw Ex;
}
}

Sql function to get first day of the week.

Hello pals,

How r you??

Today we are going to create a user defined function in SQL 2005, to fetch first day of the week.


GO
/****** Object: UserDefinedFunction [dbo].[F_START_OF_WEEK] Script Date: 02/05/2009 18:19:51 by Nitin Dhiman ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[F_START_OF_WEEK]
(
@DATE datetime,
– Sun = 1, Mon = 2, Tue = 3, Wed = 4
– Thu = 5, Fri = 6, Sat = 7
– Default to Sunday
@WEEK_START_DAY int = 1
)
/*
Find the fisrt date on or before @DATE that matches
day of week of @WEEK_START_DAY.
*/
returns datetime
as
begin
declare @START_OF_WEEK_DATE datetime
declare @FIRST_BOW datetime

– Check for valid day of week
if @WEEK_START_DAY between 1 and 7
begin
– Find first day on or after 1753/1/1 (-53690)
– matching day of week of @WEEK_START_DAY
– 1753/1/1 is earliest possible SQL Server date.
select @FIRST_BOW = convert(datetime,-53690+((@WEEK_START_DAY+5)%7))
– Verify beginning of week not before 1753/1/1
if @DATE >= @FIRST_BOW
begin
select @START_OF_WEEK_DATE =
dateadd(dd,(datediff(dd,@FIRST_BOW,@DATE)/7)*7,@FIRST_BOW)
end
end

return @START_OF_WEEK_DATE

end

Sending mail containing attachements using CDONTS in .Net

Hi,

Chalo aaj attachement wali mail send karte hai Using CDONTS and asp.net.

Here are the steps to send mail using CDONTS:

1) Add refrence to the CDONTS.dll.

2) Register the dll using:

Start + Run, regsvr32 c:\cdonts.dll

3) Code to sned email:
s1 = FileUpload1.PostedFile.FileName;
Response.Write(s1+” s1
”);
Response.Write(s2 + “ s2
”);
Response.Write(s3 + “ s3
”);
NewMailClass Obj = new NewMailClass();
Obj.MailFormat = 0;
Obj.BodyFormat = 0;

// we can use physical path of any file on the place of FileUpload1.PostedFile.FileName. And ‘my file’ will be the name of the file sent.

Obj.AttachFile(FileUpload1.PostedFile.FileName, “my file”, 1);

Obj.Send(”nitin.sender@anyfakeid.com”, “nitin.reciever@anyrealid.com”, “mail subject”, “mail message body”, 1);

thanks,

Nitin Dhiman.

To get the complete physical path of the file on firefox.

Hi,
Yesterday i was working with some page to upload files using asp.net uploader, tht was working well on IE but not on firefox.

FileUpload1.PostedFile.FileName file uploader does not provide physical path of the file, if we are using firefox.

It works well on internet explorer but if we are using firefox then it will return just file name of the posted file.

u can use following code for same:

System.IO.Directory.GetParent(FileUpload1.PostedFile.FileName).ToString() + “//” + FileUpload1.FileName;

Thanks,

Nitin Dhiman.