Hello there,
I want to upload multiple images using Ajax fileupload in dynamic folder, resize the images while uploading and once entered, enter the path into the Datadase.
I am using following code but its working.
protected Bitmap GetScaledPicture(Bitmap source, int maxWidth, int maxHeight)
{
int width, height;
float aspectRatio = (float)source.Width / (float)source.Height;
if ((maxHeight > 0) && (maxWidth > 0))
{
if ((source.Width < maxWidth) && (source.Height < maxHeight))
{
//Return unchanged image
return source;
}
else if (aspectRatio > 1)
{
// Calculated width and height,
// and recalcuate if the height exceeds maxHeight
width = maxWidth;
height = (int)(width / aspectRatio);
if (height > maxHeight)
{
height = maxHeight;
width = (int)(height * aspectRatio);
}
}
else
{
// Calculated width and height,
// and recalcuate if the width exceeds maxWidth
height = maxHeight;
width = (int)(height * aspectRatio);
if (width > maxWidth)
{
width = maxWidth;
height = (int)(width / aspectRatio);
}
}
}
else if ((maxHeight == 0) && (source.Width > maxWidth))
{
// If MaxHeight is not provided (unlimited), and
// the source width exceeds maxWidth,
// then recalculate height
width = maxWidth;
height = (int)(width / aspectRatio);
}
else if ((maxWidth == 0) && (source.Height > maxHeight))
{
// If MaxWidth is not provided (unlimited), and the
// source height exceeds maxHeight, then
// recalculate width
height = maxHeight;
width = (int)(height * aspectRatio);
}
else
{
//Return unchanged image
return source;
}
Bitmap newImage = GetResizedImage(source, width, height);
return newImage;
}
protected Bitmap GetResizedImage(Bitmap source, int width, int height)
{
//This function creates the thumbnail image.
//The logic is to create a blank image and to
// draw the source image onto it
Bitmap thumb = new Bitmap(width, height);
Graphics gr = Graphics.FromImage(thumb);
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.DrawImage(source, 0, 0, width, height);
return thumb;
}
protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
fetch = Request.QueryString["RowIndex"];
title1 = lbltitle.Text.Replace("'", "''");
directory1 = lbltitle.Text.Replace(" ", "");
directory2 = directory1.Replace("'", "");
filepath = Server.MapPath("~/images/NewsEvents/" + directory2 + fetch + "\\");
string filepath2 = Server.MapPath("~/images/NewsEvents/" + directory2 + fetch + "/");
CountFile();
abpimga = "~/images/NewsEvents/" + directory2 + fetch + "/" + directory2 + fetch + Label2.Text + ".jpg";
string shopLogoName = filepath + "\\" +
System.IO.Path.GetFileName(directory2 + fetch + Label2.Text + ".jpg");
string shopname2 = directory2 + fetch + Label2.Text + ".jpg";
con.Open();
string statuson = "1";
MySqlCommand cmd = new MySqlCommand("insert into eventnews_images(event_id,event_title,image_url,status) values('" + fetch + "','" + title1 + "','" + abpimga + "','" + statuson + "')", con);
cmd.ExecuteNonQuery();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('data submitted sucessfully ')", true);
con.Close();
string fileName = Path.GetFileName(e.FileName);
AjaxFileUpload11.SaveAs(Server.MapPath(filepath + shopname2));
Response.Redirect("new_and_eventdetails.aspx?RowIndex=" + fetch);
Bitmap uploadedImage = new Bitmap(e.GetStreamContents());
int maxWidth = 750;
int maxHeight = 0;
Bitmap objImage = GetScaledPicture(uploadedImage, maxWidth, maxHeight);
objImage.Save(shopLogoName, ImageFormat.Jpeg);
}
private void CountFile()
{
string dir = Server.MapPath("~/images/NewsEvents/" + directory2 + fetch + "/");
string[] files;
files = Directory.GetFiles(dir);
nof = files.Length;
//string nextFileName = (numFiles + 1).ToString() + ".jpg";
//TextBox1.Text = nextFileName;
Label2.Text = (nof + 1).ToString() + "";
// String s = (nof + 1).ToString() + "";
}
Let me know if anyonce can help