#region
|
|
using System;
|
using System.Drawing;
|
using System.Drawing.Imaging;
|
using System.IO;
|
|
#endregion
|
|
namespace CSFrameworkV5.Common
|
{
|
/// <summary>
|
/// 图片处理
|
/// </summary>
|
public class CImageLibrary
|
{
|
/// <summary>
|
/// 检查图片返回的结果
|
/// </summary>
|
public enum ValidateImageResult
|
{
|
OK,
|
InvalidFileSize,
|
InvalidImageSize
|
}
|
|
/// <summary>
|
/// 数组转换为图片
|
/// </summary>
|
/// .
|
/// <param name="bs">数组</param>
|
/// <returns></returns>
|
public static Image FromBytes(byte[] bs)
|
{
|
if (bs == null) return null;
|
|
using (var ms = new MemoryStream(bs))
|
{
|
return Image.FromStream(ms);
|
}
|
}
|
|
/// <summary>
|
/// 获取1x1像素的空图片
|
/// </summary>
|
/// <returns></returns>
|
public static byte[] GetEmptyImageByte()
|
{
|
Bitmap bmp = null;
|
|
try
|
{
|
using (bmp = new Bitmap(1, 1))
|
{
|
return GetImageBytes(bmp, ImageFormat.Jpeg);
|
}
|
}
|
finally
|
{
|
if (bmp != null) bmp.Dispose();
|
}
|
}
|
|
/// <summary>
|
/// 图片转换为数组
|
/// </summary>
|
/// <param name="imageFileName">图片文件</param>
|
/// <returns></returns>
|
public static byte[] GetImageBytes(string imageFileName,
|
ImageFormat format)
|
{
|
var img = Image.FromFile(imageFileName);
|
var bs = GetImageBytes(img, format);
|
img.Dispose();
|
return bs;
|
}
|
|
/// <summary>
|
/// 图片转换为数组
|
/// </summary>
|
/// <param name="img">图片实例</param>
|
/// <returns></returns>
|
public static byte[] GetImageBytes(Image img, ImageFormat format)
|
{
|
if (img == null) return null;
|
|
MemoryStream ms = null;
|
Bitmap bmp = null;
|
try
|
{
|
using (ms = new MemoryStream())
|
{
|
using (bmp = new Bitmap(img))
|
{
|
bmp.Save(ms, format);
|
return ms.ToArray();
|
}
|
}
|
}
|
finally
|
{
|
if (ms != null) ms.Dispose();
|
|
if (bmp != null) bmp.Dispose();
|
}
|
}
|
|
/// <summary>
|
/// 按宽度比例缩小图片
|
/// </summary>
|
/// <param name="imgSource">原始图片</param>
|
/// <param name="MAX_WIDTH">最大宽度</param>
|
/// <returns></returns>
|
public static Image ResizeImage(Image imgSource, int MAX_WIDTH,
|
int MAX_HEIGHT)
|
{
|
var imgOutput = imgSource;
|
|
var size = new Size(0, 0); //用于存储按比例计算后的宽和高参数
|
|
if (imgSource.Width <= 3 ||
|
imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换
|
|
//按宽度缩放图片
|
if (imgSource.Width > MAX_WIDTH) //计算宽度
|
{
|
var rate = MAX_WIDTH / (double)imgSource.Width; //计算宽度比例因子
|
|
size.Width = Convert.ToInt32(imgSource.Width * rate);
|
size.Height = Convert.ToInt32(imgSource.Height * rate);
|
imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height,
|
null, IntPtr.Zero);
|
}
|
|
//按高度缩放图片
|
if (imgOutput.Height > MAX_HEIGHT) //计算高度
|
{
|
var rate = MAX_HEIGHT / (double)imgOutput.Height; //计算宽度比例因子
|
|
size.Width = Convert.ToInt32(imgOutput.Width * rate);
|
size.Height = Convert.ToInt32(imgOutput.Height * rate);
|
imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height,
|
null, IntPtr.Zero);
|
}
|
|
return imgOutput;
|
}
|
|
/// <summary>
|
/// 转换为BMP格式
|
/// </summary>
|
/// <param name="source">原图</param>
|
/// <returns>返回BMP格式的图片</returns>
|
public static Image ToBMP(Image source, string outFileName)
|
{
|
try
|
{
|
source.Save(outFileName, ImageFormat.Bmp);
|
}
|
catch (Exception ex)
|
{
|
throw new Exception("原图文件流可能已关闭!\r\n" + ex.Message);
|
}
|
|
if (source.RawFormat == ImageFormat.Bmp)
|
return source as Bitmap; //BMP格式不需要转换
|
|
return Image.FromFile(outFileName); //加载存储的BMP文件.
|
}
|
|
/// <summary>
|
/// 转换为JPG格式
|
/// </summary>
|
/// <param name="source">原图</param>
|
/// <returns>返回JPG格式的图片</returns>
|
public static Image ToJPG(Image source, string outFileName)
|
{
|
try
|
{
|
source.Save(outFileName, ImageFormat.Jpeg);
|
}
|
catch (Exception ex)
|
{
|
throw new Exception("原图文件流可能已关闭!\r\n" + ex.Message);
|
}
|
|
if (source.RawFormat == ImageFormat.Jpeg)
|
return source as Bitmap; //BMP格式不需要转换
|
|
return Image.FromFile(outFileName); //加载存储的BMP文件.
|
}
|
|
/// <summary>
|
/// 转换为PNG格式
|
/// </summary>
|
/// <param name="source">原图</param>
|
/// <returns>返回PNG格式的图片</returns>
|
public static Image ToPNG(Image source, string outFileName)
|
{
|
try
|
{
|
source.Save(outFileName, ImageFormat.Png);
|
}
|
catch (Exception ex)
|
{
|
throw new Exception("原图文件流可能已关闭!\r\n" + ex.Message);
|
}
|
|
if (source.RawFormat == ImageFormat.Png)
|
return source as Bitmap; //BMP格式不需要转换
|
|
return Image.FromFile(outFileName); //加载存储的BMP文件.
|
}
|
|
/// <summary>
|
/// 检查图片文件大小
|
/// </summary>
|
/// <param name="file">图片文件</param>
|
/// <param name="MAX_FILE_SIZE">最大文件大小</param>
|
/// <param name="MAX_WIDTH">最大宽度</param>
|
/// <param name="MAX_HEIGHT">最大高度</param>
|
/// <returns>返回检查结果</returns>
|
public static ValidateImageResult ValidateImage(string file,
|
int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)
|
{
|
var bs = File.ReadAllBytes(file);
|
double size = bs.Length / 1024;
|
|
//大于50KB
|
if (size > MAX_FILE_SIZE)
|
return ValidateImageResult.InvalidFileSize;
|
|
Image img = null;
|
try
|
{
|
img = Image.FromFile(file);
|
if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT)
|
return ValidateImageResult.InvalidImageSize;
|
else
|
return ValidateImageResult.OK;
|
}
|
finally
|
{
|
img.Dispose();
|
}
|
}
|
}
|
}
|