Introduction
There are so many articles available which show you how to insert a picture to a database and read it back, how to reduce the quality and size, and how to change the dimensions. I haven’t come across any article which does all the above in one project. This article will show you all these tricks, like, change the quality of photo so the size can be smaller without compromising the quality [for web publishing or email purposes], change the dimension, how to save pictures to a database, and how to read images from a database and then display in an image control. This is ideal for bulk processing.
Background
The basic idea is taken from an article in GeekPedia by Andrei Pociu; some help was taken from Microsoft and the DZone Snippets website.
Using the code
The code below will load a the list from your folder which contains pictures:
if (fbdOpen.ShowDialog() == DialogResult.OK)
{
lstPhotos.Items.Clear();
foreach (string Filename in Directory.GetFiles(fbdOpen.SelectedPath))
{
FileInfo fiPicture = new FileInfo(Filename);
if (fiPicture.Extension.ToLower() == ".jpeg" ||
fiPicture.Extension.ToLower() == ".jpg")
{
lstPhotos.Items.Add(Filename);
}
}
}
prgReduce.Maximum = lstPhotos.Items.Count;
This will process your request to resize, adjust quality, and save to a database, as well as save to another folder with the desired size and quality.
prgReduce.Value = 0;
if (fbdSave.ShowDialog() == DialogResult.OK)
{
ImageCodecInfo iciJpegCodec = null;
EncoderParameter epQuality =
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
(int)numQual.Value);
ImageCodecInfo[] iciCodecs = ImageCodecInfo.GetImageEncoders();
EncoderParameters epParameters = new EncoderParameters(1);
epParameters.Param[0] = epQuality;
for (int i = 0; i < iciCodecs.Length; i++)
{
if (iciCodecs[i].MimeType == "image/jpeg")
{
iciJpegCodec = iciCodecs[i];
break;
}
}
foreach (string strFile in lstPhotos.Items)
{
SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest" +
" (BLOBData, BLOBName, InsertDate) " +
"VALUES (@BLOBData, @BLOBName, @DateTime)", cn);
prgReduce.PerformStep();
Image newImage = Image.FromFile(strFile);
FileInfo fiPicture = new FileInfo(strFile);
int PhotoWidth = 0;
if (radioButton1.Checked)
{
PhotoWidth = 1024;
}
else if(radioButton2.Checked)
{
PhotoWidth = 640;
}
else if (radioButton3.Checked)
{
PhotoWidth = 420;
}
System.Drawing.Image ImageReSized =
ResizeImage(strFile, fbdSave.SelectedPath.ToString() +
"\\" + fiPicture.Name, PhotoWidth, 768, false);
//newImage.GetThumbnailImage(640, 480, null, IntPtr.Zero);
//Convert Image to byt to save on database
System.Drawing.ImageConverter ic =
new System.Drawing.ImageConverter();
byte[] bytBLOBData = new byte[1];
bytBLOBData = (byte[])ic.ConvertTo(ImageReSized, bytBLOBData.GetType());
//Update the database
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary,
bytBLOBData.Length, ParameterDirection.Input, false,
0, 0, null, DataRowVersion.Current, bytBLOBData);
SqlParameter prm1 = new SqlParameter("@BLOBName", SqlDbType.VarChar, 100,
ParameterDirection.Input, false, 0, 0, null,
DataRowVersion.Current, fiPicture.FullName);
SqlParameter prm2 = new SqlParameter("@DateTime", SqlDbType.DateTime, 21,
ParameterDirection.Input, false, 0, 0, null,
DataRowVersion.Current, DateTime.Now);
//Add parameter
cmd.Parameters.Add(prm);
cmd.Parameters.Add(prm1);
cmd.Parameters.Add(prm2);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
// Save resized picture here
ImageReSized.Save(fbdSave.SelectedPath.ToString() + "\\" +
fiPicture.Name, iciJpegCodec, epParameters);
}
// Open the folder containing the new items
System.Diagnostics.Process.Start(fbdSave.SelectedPath.ToString());
}
The code above calls this function to change the dimensions:
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(originalFile);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (onlyResizeIfWider)
{
if (FullsizeImage.Width <= newWidth)
{
newWidth = FullsizeImage.Width;
}
}
int NewHeight = FullsizeImage.Height * newWidth / FullsizeImage.Width;
if (NewHeight > maxHeight)
{
newWidth = FullsizeImage.Width * maxHeight / FullsizeImage.Height;
NewHeight = maxHeight;
}
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(newWidth,
NewHeight, null, IntPtr.Zero);
FullsizeImage.Dispose();
return NewImage;
Points of interest
Now, you have all the code in one project which allows you to manipulate your images or photos. Now, my wife does not have to use PaintBrush to reduce picture quality to sent a photo by email.