Nitin Dhiman. Powered by Blogger.
Showing posts with label fileupload. Show all posts

Upload and resize image.

No comments
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;
}
}

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

No comments
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. sale ne bada dimag khrab kiya. phle to problem pata hi ni chali, but baad me ho gya.

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.