/// <summary>
/// 制作符合格式的照片,并自动缩放。姓名在上工号在下,都居中。
/// </summary>
/// <param name="sourceFile">源图片文件</param>
/// <param name="destFile">输出目标文件</param>
/// <param name="destHeight">输出文件高</param>
/// <param name="destWidth">输出文件宽</param>
/// <param name="staffid">员工工号</param>
/// <param name="staffName">员工姓名</param>
public void GetBitMap(string sourceFile,string destFile, int destHeight, int destWidth, string staffid, string staffName)
{
System.Drawing.Image imgSource = System.Drawing.Image.FromFile(sourceFile);
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
int sW = 0, sH = 0;
// 按比例缩放
int sWidth = imgSource.Width;
int sHeight = imgSource.Height;

if (sHeight > destHeight || sWidth > destWidth)
{
if ((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
sW = sWidth;
sH = sHeight;
}
Bitmap outBmp = new Bitmap(destWidth, destHeight+50);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.WhiteSmoke);

// 设置画布的描绘质量
g.CompositingQuality =CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
SolidBrush drawBrush = new SolidBrush(Color.Black);
Font drawFont = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Pixel);
SizeF size = g.MeasureString(staffName, drawFont);
float StringWidth = size.Width;
int xPos =(outBmp.Width-(int)StringWidth)/2;
int yPos = outBmp.Height-45;
g.DrawString(staffName, drawFont, drawBrush, xPos, yPos);
yPos += 20;
SizeF size2 = g.MeasureString(staffid, drawFont);
StringWidth = size2.Width;
xPos = (outBmp.Width - (int)StringWidth) / 2;
g.DrawString(staffid, drawFont, drawBrush, xPos, yPos);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
outBmp.Save(destFile, ImageFormat.Jpeg);
imgSource.Dispose();
outBmp.Dispose();
}


/// <summary>
/// 绘制大图
/// </summary>
/// <param name="outfile">输出的大文件路径</param>
/// <param name="sourceFolder">包含所有源图片的源目录</param>
private void DrawBigPhoto(string outfile,string sourceFolder)
{
int startpoint = 50;
int x = 50, y = 50;
int jg = 60;
int idx = 0;
if (System.IO.File.Exists(outfile))
System.IO.File.Delete(outfile);
string[] files = System.IO.Directory.GetFiles(sourceFolder, "*.jpg");
Image img = Image.FromFile(files[idx]);
idx++;
XY thexy = getXandY(files.Length);
Bitmap outmap = new Bitmap((img.Width+jg)*thexy.x+startpoint, (img.Height+jg)*thexy.y);
Graphics g = Graphics.FromImage(outmap);
g.Clear(Color.Red);
for (int i = 0; i <= files.Length / thexy.x; i++)
{
x = 50;
for (int j = 0; j < thexy.x; j++)
{
if (idx < files.Length)
{
g.DrawImage(img, x, y);
img = Image.FromFile(files[idx]);
idx++;
x = (img.Width + jg) * (j + 1) + startpoint;
}
else
break;
}
y = (img.Height + jg) * (i + 1) + startpoint;
}

outmap.Save(outfile, ImageFormat.Jpeg);
g.Dispose();
outmap.Dispose();
}