using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Ella.ToolKit
{
/// <summary>
/// 图像处理
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-29
/// </summary>
public sealed class ImageHelper : Singleton<ImageHelper>
{
Bitmap bitmap;
/// <summary>
/// 将BMP格式的图片转换成JPG
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-29
/// </summary>
/// <param name="bmpPath">Bmp图片的路径(包含文件名)</param>
/// <param name="savePath">保存JPG的路径(包含文件名)</param>
public bool BmpToJpg(string bmpPath, string savePath)
{
bool bIsSuccess;
try
{
using (bitmap = new Bitmap(bmpPath))
{
bitmap.Save(savePath, ImageFormat.Jpeg);
}
bIsSuccess = true;
}
catch (Exception ex)
{
bIsSuccess = false;
LogHelper.Instance.Add(ex);
}
return bIsSuccess;
}
/// <summary>
/// 将JPG格式的图片转换成BMP
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-29
/// </summary>
/// <param name="jpgPath">JPG路径(包含文件名)</param>
/// <param name="bmpPath">保存Bmp图片的路径(包含文件名)</param>
public bool JpgToBmp(string jpgPath, string savePath)
{
bool bIsSuccess;
try
{
using (bitmap = new Bitmap(jpgPath))
{
bitmap.Save(savePath, ImageFormat.Bmp);
}
bIsSuccess = true;
}
catch (Exception ex)
{
bIsSuccess = false;
LogHelper.Instance.Add(ex);
}
return bIsSuccess;
}
/// <summary>
/// 将JPG格式的图片转换成ICO
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-29
/// </summary>
/// <param name="jpgPath">JPG图片路径(包含文件名)</param>
/// <param name="savePath">保存ico格式图片路径</param>
public bool JpgToIco(string jpgPath, string savePath)
{
bool bIsSuccess;
try
{
using (bitmap = new Bitmap(jpgPath))
{
bitmap.Save(savePath, ImageFormat.Icon);
FileInfo f = new FileInfo(savePath);
}
bIsSuccess = true;
}
catch (Exception ex)
{
bIsSuccess = false;
LogHelper.Instance.Add(ex);
}
return bIsSuccess;
}
/// <summary>
/// 图片旋转
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-29
/// </summary>
/// <param name="imagePath">图片所在的路径(包含文件名)</param>
/// <param name="pictureBox">显示旋转后的图片控件</param>
/// <param name="rotateFilp">指定旋转角度和方向</param>
public bool RoteateFlipImage(string imagePath, PictureBox pictureBox, RotateFlipType rotateFilp)
{
bool bIsSuccess;
try
{
using (bitmap = (Bitmap)Bitmap.FromFile(imagePath))
{
bitmap.RotateFlip(rotateFilp);
pictureBox.Image = bitmap;
}
bIsSuccess = true;
}
catch (Exception ex)
{
bIsSuccess = false;
LogHelper.Instance.Add(ex);
}
return bIsSuccess;
}
/// <summary>
/// 在图片上边添加文字
/// <para/>Author : AnDequan
/// <para/>Date : 2011-1-5
/// </summary>
/// <param name="imagePath">图片路径(包含文件名)</param>
/// <param name="letter">要写在图片上的文字</param>
/// <param name="form"></param>
/// <returns>true - 成功,false - 失败</returns>
public bool AddLetterToImage(string imagePath, string letter, Form form)
{
bool bIsSuccess;
try
{
Image myImage = System.Drawing.Image.FromFile(imagePath);// 图片对象
Bitmap map = new Bitmap(myImage);
myImage.Dispose();
Graphics graphics = Graphics.FromImage(map);
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
SolidBrush brush = new SolidBrush(Color.Red);
PointF P = new PointF(100, 100);
Font font = new Font(form.Font.Name, 40);// 字体
//[ 调用DrawString()方法向指定图片中写入文字 ]
graphics.DrawString(letter, font, brush, P);
//[ 调用Save方法保存新图片 ]
map.Save(imagePath.Substring(0, imagePath.LastIndexOf("//") + 1) + "new" + imagePath.Substring(imagePath.LastIndexOf("//") + 1, imagePath.LastIndexOf(".") - imagePath.LastIndexOf("//") - 1) + imagePath.Substring(imagePath.LastIndexOf("."), imagePath.Length - imagePath.LastIndexOf(".")), ImageFormat.Jpeg);
bIsSuccess = true;
font.Dispose();
graphics.Dispose();
}
catch (Exception ex)
{
bIsSuccess = false;
LogHelper.Instance.Add(ex);
}
return bIsSuccess;
}
/// <summary>
/// 把文字保存成图片
/// <para/>Author : AnDequan
/// <para/>Date : 2011-1-5
/// </summary>
/// <param name="form">窗体(this)</param>
/// <param name="width">图片的宽度</param>
/// <param name="height">图片的高度</param>
/// <param name="letter">文字</param>
/// <param name="savePath">保存路径(包含文件名)</param>
/// <param name="format">保存图片的格式</param>
/// <returns>true - 成功,false - 失败</returns>
public bool SaveLetterToImage(Form form, int width, int height, string letter, string savePath, ImageFormat format)
{
bool bIsSuccess = false;
using (Graphics myGraphics = form.CreateGraphics())
{
using (bitmap = new Bitmap(width, height))
{
myGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
myGraphics.DrawString(letter, new Font("宋体", 9), new SolidBrush(Color.Black), new Point(0, 2));
bitmap.Save(savePath, format);
bIsSuccess = true;
}
}
return bIsSuccess;
}
}
}