C# 图片截取 压缩
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
namespace
{
public class CutImageHelper
{
/// <summary>
/// 截取图片方法
/// </summary>
/// <param name="stream">图片</param>
/// <param name="dataStream">字节</param>
public static bool CutImage(byte[] stream, out byte[] dataStream)
{
//byte[]==>Bitmap
MemoryStream ms1 = new MemoryStream(stream);
Bitmap bitmap = (Bitmap)Image.FromStream(ms1);
ms1.Close();
int get = bitmap.Width <= bitmap.Height ? bitmap.Width / 2 : bitmap.Height / 2;
int width = bitmap.Width / 2 - get / 2;
int height = bitmap.Height / 2 - get / 2;
if ((width < get) && (height < get))
{
if (((width + get) <= bitmap.Width) && ((height + get) <= bitmap.Height))
{
Bitmap destBitmap = new Bitmap(get, get);//目标图
Rectangle destRect = new Rectangle(0, 0, get, get);//矩形容器
Rectangle srcRect = new Rectangle(width, height, get, get);
Graphics graphics = Graphics.FromImage(destBitmap);
graphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
ImageFormat format = ImageFormat.Png;
//压缩
Compress(destBitmap);
//Bitmap==>byte[]
MemoryStream ms = new MemoryStream();
destBitmap.Save(ms, ImageFormat.Bmp);
dataStream = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以
ms.Close();
return true;
}
else
{
dataStream = new byte[0];
return false;
}
}
else
{
dataStream = new byte[0];
return false;
}
}
public static bool Compress(byte[] stream, out byte[] dataStream)
{
MemoryStream ms1 = new MemoryStream(stream);
Bitmap bitmap = (Bitmap)Image.FromStream(ms1);
ms1.Close();
ImageCodecInfo myImageCodecInfo;
//获得JPEG格式的编码器
myImageCodecInfo = GetEncoderInfo("image/jpeg");
System.Drawing.Imaging.Encoder myEncoder =System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters= new EncoderParameters(1); ;
EncoderParameter myEncoderParameter= new EncoderParameter(myEncoder, 15L);
myEncoderParameters.Param[0] = myEncoderParameter;
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, myImageCodecInfo, myEncoderParameters);
dataStream = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以
ms.Close();
return true;
}
public static void Compress(Bitmap bmp)
{
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 15L);
myEncoderParameters.Param[0] = myEncoderParameter;
}
public static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
public static void savePath(Bitmap bmp, string str)//保存路径
{
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 15L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save(str, myImageCodecInfo, myEncoderParameters);
}
}
}