usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Drawing.Imaging;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceUtilities.Helper
{///
///image帮助类///
public static classImageHelper
{///
///Convert Image to Byte[]///
/// 图形对象
///
public static byte[] ImageToBytes(Image image)
{
ImageFormat format=image.RawFormat;using (MemoryStream ms = newMemoryStream())
{if(format.Equals(ImageFormat.Jpeg))
{
image.Save(ms, ImageFormat.Jpeg);
}else if(format.Equals(ImageFormat.Png))
{
image.Save(ms, ImageFormat.Png);
}else if(format.Equals(ImageFormat.Bmp))
{
image.Save(ms, ImageFormat.Bmp);
}else if(format.Equals(ImageFormat.Gif))
{
image.Save(ms, ImageFormat.Gif);
}else if(format.Equals(ImageFormat.Icon))
{
image.Save(ms, ImageFormat.Icon);
}else{
image.Save(ms, ImageFormat.Jpeg);
}byte[] buffer = new byte[ms.Length];//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer,0, buffer.Length);returnbuffer;
}
}///
///Convert Byte[] to Image///
/// 字节数组
///
public static Image BytesToImage(byte[] buffer)
{
MemoryStream ms= newMemoryStream(buffer);
Image image=System.Drawing.Image.FromStream(ms);returnimage;
}///
///Convert Byte[] to a picture and Store it in file///
///
///
///
public static string CreateImageFromBytes(string fileName, byte[] buffer)
{string file =fileName;
Image image=BytesToImage(buffer);
ImageFormat format=image.RawFormat;if(format.Equals(ImageFormat.Jpeg))
{
file+= ".jpeg";
}else if(format.Equals(ImageFormat.Png))
{
file+= ".png";
}else if(format.Equals(ImageFormat.Bmp))
{
file+= ".bmp";
}else if(format.Equals(ImageFormat.Gif))
{
file+= ".gif";
}else if(format.Equals(ImageFormat.Icon))
{
file+= ".icon";
}else{
file+= ".jpg";
}
System.IO.FileInfo info= newSystem.IO.FileInfo(file);
System.IO.Directory.CreateDirectory(info.Directory.FullName);
File.WriteAllBytes(file, buffer);returnfile;
}///
///压缩图片, 并保存释放图片///
///
///
/// 压缩质量(数字越小压缩率越高) 1-100
///
public static bool ZipPic(Image img, string dFile, intflag)
{
ImageFormat tFormat=img.RawFormat;//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = newEncoderParameters();long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = newEncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] =eParam;try{
ImageCodecInfo[] arrayICI=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo= null;for (int x = 0; x < arrayICI.Length; x++)
{if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo=arrayICI[x];break;
}
}if (jpegICIinfo != null)
{
img.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}else{
img.Save(dFile, tFormat);
}return true;
}catch{return false;
}finally{
img.Dispose();
}
}///
///按比例缩放图片///
/// 源图片,自动释放
///
///
///
public static Image ZoomPicture(Image SourceImage, int TargetWidth, intTargetHeight)
{int IntWidth; //新的图片宽
int IntHeight; //新的图片高
try{
System.Drawing.Imaging.ImageFormat format=SourceImage.RawFormat;
System.Drawing.Bitmap SaveImage= newSystem.Drawing.Bitmap(TargetWidth, TargetHeight);
Graphics g=Graphics.FromImage(SaveImage);
g.Clear(Color.White);if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小
{
IntWidth=TargetWidth;
IntHeight= (IntWidth * SourceImage.Height) /SourceImage.Width;
}else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大
{
IntHeight=TargetHeight;
IntWidth= (IntHeight * SourceImage.Width) /SourceImage.Height;
}else if (SourceImage.Width <= TargetWidth && SourceImage.Height <= TargetHeight) //长宽比目的图片长宽都小
{
IntHeight=SourceImage.Width;
IntWidth=SourceImage.Height;
}else//长宽比目的图片的长宽都大
{
IntWidth=TargetWidth;
IntHeight= (IntWidth * SourceImage.Height) /SourceImage.Width;if (IntHeight > TargetHeight)//重新计算
{
IntHeight=TargetHeight;
IntWidth= (IntHeight * SourceImage.Width) /SourceImage.Height;
}
}//g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
g.DrawImage(SourceImage,0,0, TargetWidth, TargetHeight);
SourceImage.Dispose();returnSaveImage;
}catch(Exception ex)
{
}return null;
}
}
}