【IT168技术文档】一个图片转换类,比如PNGtoJPG,BMPtoPNG按比例缩小图片
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace VjsdnPublishProject
{
public class CImageLibrary
{
public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
//检查图片大小
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 = Image.FromFile(file);
if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;
return ValidateImageResult.OK;
}
//按宽度比例缩小图片
public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)
{
Image imgOutput = imgSource;
Size size = new Size(imgSource.Width, imgSource.Height);
if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换
if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)
{
double rate = MAX_WIDTH / (double)imgSource.Width;
if (imgSource.Height * rate > MAX_WIDTH)
rate = MAX_WIDTH / (double)imgSource.Height;
size.Width = Convert.ToInt32(imgSource.Width * rate);
size.Height = Convert.ToInt32(imgSource.Height * rate);
imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
}
return imgOutput;
}
//按比例缩小图片
public static Image GetOutputSizeImage(Image imgSource, Size outSize)
{
Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);
return imgOutput;
}
/// <summary>
/// 由图片文件转字节
/// </summary>
public static byte[] GetImageBytes(string imageFileName)
{
Image img = Image.FromFile(imageFileName);
return GetImageBytes(img);
}
/// <summary>
/// 图片转字节
/// </summary>
public static byte[] GetImageBytes(Image img)
{
if (img == null) return null;
try
{
System.IO.MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
byte[] bs = ms.ToArray();
ms.Close();
return bs;
}
catch { return null; }
}
/// <summary>
/// 字节转图片
/// </summary>
public static Image FromBytes(byte[] bs)
{
if (bs == null) return null;
try
{
MemoryStream ms = new MemoryStream(bs);
Image returnImage = Image.FromStream(ms);
ms.Close();
return returnImage;
}
catch { return null; }
}
/// <summary>
/// 将其它格式的图片转为JPG文件
/// </summary>
public static Image ToJPG(Image source)
{
//注意,先定义Bitmap类,否则会报A generic error occurred in GDI+
Bitmap bmp = new Bitmap(source);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
return Bitmap.FromStream(ms);
}
/// <summary>
/// 将其它格式的图片转为PNG文件
/// </summary>
public static Image ToPNG(Image source)
{
//注意,先定义Bitmap类,否则会报A generic error occurred in GDI+
Bitmap bmp = new Bitmap(source);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
return FromBytes(ms.ToArray());
}
//保存文件
public static void SaveFile(string fileName, Image source)
{
source.Save(fileName, source.RawFormat);
}
}
}