Pete, thanks for your advice here. I am making progress and have
implemented most of your suggestions.
If (and only if) you have time, could you respond to my ongoing
ignorance?...
-- it is more idiomatic to use the Size struct, instead of
keeping
individual width/height variables. Doing so makes some of the
later
Graphics calls more convenient too.
Can you explain why it's better to use the Size struct than w & h
variables, and how to do so.
-- It is also idiomatic to use the "using" statement when dealing
with
IDisposable objects. Doing so is also a good way to avoid mistakes
such
as disposing objects in the wrong order (such as disposing a Bitmap
instance before you've disposed the Graphics instance that draws into
that
Bitmap instance, as you've done here).
I use the "using" statements at the top of my program.cs. Are you
saying that I should be using them in my ImageResizing method as well?
Did I get the order right this time? Are there any other objects I
should be disposing of?
My entire program.cs appears below.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace JICS_Photos
{
class Program
{
static void Main(string [ ] args)
{
String strIDPhotoPath = @"\\gotest\InetPub\photos\upload
\";
String strJICSPhotoPath = @"\\gordon\admsoft\jenzabar
\ICSFileServer\IDPhotos\";
synergyDataSetTableAdapters.selectAccountBarcodeJICSIDTableAdapter
taPeople = new
JICS_Photos.synergyDataSetTableAdapters.selectAccountBarcodeJICSIDTableAdapter
();
synergyDataSet.selectAccountBarcodeJICSIDDataTable
dtPeople = new synergyDataSet.selectAccountBarcodeJICSIDDataTable();
taPeople.Fill(dtPeople);
foreach (DataRow drPerson in dtPeople.Rows)
{
//Console.WriteLine(dr["account_id"].ToString() + ".jpg
\t" + dr["barcode"].ToString() + "jpg");
if (File.Exists(strIDPhotoPath + drPerson
["account_id"].ToString() + ".jpg"))
{
String strFullSourcePath = strIDPhotoPath +
drPerson["account_id"].ToString() + ".jpg";
String strFullDestPath = strJICSPhotoPath +
drPerson["barcode"].ToString() + ".jpg";
ImageResize(strFullSourcePath, strFullDestPath,
130, 130);
Console.WriteLine("transforming " +
strFullSourcePath + " to " + strFullDestPath);
}
}
}
public static void ImageResize(string strSourceImagePath,
string strDestImagePath, int iMaxWidth, int iMaxHeight)
{
if (File.Exists(strSourceImagePath))
{
Image imgPhoto = System.Drawing.Image.FromFile
(strSourceImagePath);
Int32 iWidth = imgPhoto.Width;
Int32 iHeight = imgPhoto.Height;
Int32 iNewWidth = imgPhoto.Width;
Int32 iNewHeight = imgPhoto.Height;
float scaleWidth = (float)iMaxWidth / iWidth,
scaleHeight = (float)iMaxHeight / iHeight;
float scaleOutput = Math.Min(scaleWidth, scaleHeight);
iNewWidth = (int) Math.Round(scaleOutput * iWidth, 0);
iNewHeight = (int) Math.Round(scaleOutput * iHeight,
0);
// Create a new blank canvas. The resized image will
be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(iNewWidth, iNewHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(96, 96);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
// draw our image on the new canvas
grPhoto.DrawImage(imgPhoto, 0, 0, iNewWidth,
iNewHeight);
// create a codec so that we can set the compression
and quality levels.
ImageCodecInfo encJPEG = FindEncoder
(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder qualityEncoder =
System.Drawing.Imaging.Encoder.Quality;
System.Drawing.Imaging.Encoder compressionEncoder =
System.Drawing.Imaging.Encoder.Compression;
EncoderParameters myEncoderParameters = new
EncoderParameters(1);
EncoderParameter myQualityEncoderParameter = new
EncoderParameter(qualityEncoder, 100L);
myEncoderParameters.Param[0] =
myQualityEncoderParameter;
// Save out to a file. We dispose of all objects to
make sure the files don't stay locked.
bmPhoto.Save(strDestImagePath, encJPEG,
myEncoderParameters);
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
}
}
public static ImageCodecInfo FindEncoder(ImageFormat format)
//source:
http://msdn.microsoft.com/en-us/library/ms142148(VS.80).aspx
{
if (format == null)
{
throw new ArgumentNullException("format");
}
foreach (ImageCodecInfo codec in
ImageCodecInfo.GetImageEncoders())
{
if (codec.FormatID.Equals(format.Guid))
{
return codec;
}
}
return null;
}
}
}