I am uploading images in c# windows application. I am uploading the images folder wise. In one folder there are 200 to 3000 images. My one images size is about 3.10 MB to 6.2 MB. Its dimensions are 3008 x 1960, 1960 , 4000 x 2600. 6000 x 4000 . while uploading i am reducing the size of image to 400 x 266 or 266 x 400 depending on the height and width of image. but still my image size is about 227 KB. Now i want to reduce the image resolution to decrease the image size. How i will do it. Please suggest me. My code is given below:
private void btnUpload_Click(object sender, EventArgs e)
{
getDestinationPath();
string ik = DestinationPath;
string ik2 = DestinationPath2;
string lastFolderName = Path.GetFileName(ik);
LoginDTO objInsert = new LoginDTO();
objInsert.CId = Convert.ToInt32(cmbUCat.SelectedValue.ToString());
if (cmbUScat1.SelectedIndex != -1)
{
objInsert.SId = Convert.ToInt32(cmbUScat1.SelectedValue.ToString());
}
else
{
objInsert.SId = 0;
}
if (cmbUScat2.SelectedIndex != -1)
{
objInsert.S2Id = Convert.ToInt32(cmbUScat2.SelectedValue.ToString());
}
else
{
objInsert.S2Id =0;
}
if (cmbUScat3.SelectedIndex != -1)
{
objInsert.S3Id = Convert.ToInt32(cmbUScat3.SelectedValue.ToString());
}
else
{
objInsert.S3Id = 0;
}
if ((folderBrowserDialog1.SelectedPath).ToString() != "" && (textSource.Text !=""))
{
objInsert.FolderPath = DestinationPathWeb;
objInsert.FolderName = lastFolderName;
objInsert.Keywords = txtKeywords.Text;
string dt = dtpPhotoDate.Value.ToShortDateString();
objInsert.PhotoDate = Convert.ToDateTime(dt);
objInsert.Notes = txtNotes.Text;
LoginBAL objInsertPhoto = new LoginBAL();
string ch = objInsertPhoto.InsertPhotoDetails(objInsert);
int count = Int32.Parse(ch);
if (count > 0)
{
if (!Directory.Exists(DestinationPath.Trim()))
{
Directory.CreateDirectory(DestinationPath.Trim());
}
if (!Directory.Exists(DestinationPath2.Trim()))
{
Directory.CreateDirectory(DestinationPath2.Trim());
}
progressBar1.Maximum = Directory.GetFiles(textSource.Text.Trim()).Length;
progressBar1.Minimum = 0;
progressBar1.Visible = true;
foreach (string file in Directory.GetFiles(textSource.Text.Trim()))
{
string dest = Path.Combine(DestinationPath.Trim(), Path.GetFileName(file));
File.Copy(file, dest, true);
string s = Path.GetFileName(file);
if (s != "Thumbs.db")
{
img = ResizeImage(dest, 400, 400, false);
string dest2 = Path.Combine(DestinationPath2.Trim(), Path.GetFileName(file));
img.Save(DestinationPath2 + "/" + s);
// File.Copy(DestinationPath2 + "/" + img, DestinationPath2, true);
SqlCommand cmd = new SqlCommand("insert into FolderImages(FolderName,image) values('" + lastFolderName + "','" + s + "')", con);
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
progressBar1.Value = progressBar1.Value + 1;
}
progressBar1.Visible = false;
progressBar1.Value = 0;
txtKeywords.Text = "";
txtNotes.Text = "";
dtpPhotoDate.Text = "";
textSource.Text = "";
BindUploadFolder();
MessageBox.Show("Folder's Images Uploaded successfully.");
}
else
{
progressBar1.Visible = false;
progressBar1.Value = 0;
MessageBox.Show("This Folder "+lastFolderName+" already exists.");
}
}
else
{
MessageBox.Show("select Folder of Images.");
}
}
public static Image ResizeImage(string file, int width, int height, bool onlyResizeIfWider)
{
using (Image image = Image.FromFile(file))
{
// Prevent using images internal thumbnail
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (onlyResizeIfWider == true)
{
if (image.Width <= width)
{
width = image.Width;
}
}
int newHeight = image.Height * width / image.Width;
if (newHeight > height)
{
// Resize with height instead
width = image.Width * height / image.Height;
newHeight = height;
}
Image NewImage = image.GetThumbnailImage(width,newHeight,null,IntPtr.Zero);
return NewImage;
}
}