图像处理业务逻辑CImageLibrary类(C/S框架网开源)
图像处理业务逻辑CImageLibrary类(C/S框架网开源)
图像处理业务逻辑CImageLibrary类
扫一扫加作者微信
图像处理业务逻辑CImageLibrary类
C# Code:
/// <summary>
/// 图片处理
/// </summary>
public class CImageLibrary
{
/// <summary>
/// 检查图片返回的结果
/// </summary>
public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
/// <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)
{
byte[] 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();
}
}
/// <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)
{
Image imgOutput = imgSource;
Size size = new Size(0, 0); //用于存储按比例计算后的宽和高参数
if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换
//按宽度缩放图片
if (imgSource.Width > MAX_WIDTH) //计算宽度
{
double 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)//计算高度
{
double 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>
/// 图片转换为数组
/// </summary>
/// <param name="imageFileName">图片文件</param>
/// <returns></returns>
public static byte[] GetImageBytes(string imageFileName, ImageFormat format)
{
Image img = Image.FromFile(imageFileName);
byte[] 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 = new MemoryStream();
try
{
Bitmap bmp = new Bitmap(img);
bmp.Save(ms, format);
byte[] bs = ms.ToArray();
ms.Close();
return bs;
}
catch { ms.Close(); return null; }
}
/// <summary>
/// 数组转换为图片
/// </summary>.
/// <param name="bs">数组</param>
/// <returns></returns>
public static Image FromBytes(byte[] bs)
{
if (bs == null) return null;
MemoryStream ms = null;
try
{
ms = new MemoryStream(bs);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
catch { if (ms != null)ms.Close(); return null; }
}
/// <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>
/// 获取1x1像素的空图片
/// </summary>
/// <returns></returns>
public static byte[] GetEmptyImageByte()
{
Bitmap bmp = new Bitmap(1, 1);
return GetImageBytes(bmp, ImageFormat.Jpeg);
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// 图片处理
/// </summary>
public class CImageLibrary
{
/// <summary>
/// 检查图片返回的结果
/// </summary>
public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
/// <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)
{
byte[] 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();
}
}
/// <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)
{
Image imgOutput = imgSource;
Size size = new Size(0, 0); //用于存储按比例计算后的宽和高参数
if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换
//按宽度缩放图片
if (imgSource.Width > MAX_WIDTH) //计算宽度
{
double 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)//计算高度
{
double 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>
/// 图片转换为数组
/// </summary>
/// <param name="imageFileName">图片文件</param>
/// <returns></returns>
public static byte[] GetImageBytes(string imageFileName, ImageFormat format)
{
Image img = Image.FromFile(imageFileName);
byte[] 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 = new MemoryStream();
try
{
Bitmap bmp = new Bitmap(img);
bmp.Save(ms, format);
byte[] bs = ms.ToArray();
ms.Close();
return bs;
}
catch { ms.Close(); return null; }
}
/// <summary>
/// 数组转换为图片
/// </summary>.
/// <param name="bs">数组</param>
/// <returns></returns>
public static Image FromBytes(byte[] bs)
{
if (bs == null) return null;
MemoryStream ms = null;
try
{
ms = new MemoryStream(bs);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
catch { if (ms != null)ms.Close(); return null; }
}
/// <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>
/// 获取1x1像素的空图片
/// </summary>
/// <returns></returns>
public static byte[] GetEmptyImageByte()
{
Bitmap bmp = new Bitmap(1, 1);
return GetImageBytes(bmp, ImageFormat.Jpeg);
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网