///
/// 图片处理
///
public static class ImageHelper
{
public static string CropImage(byte[] content, int x, int y, int cropWidth, int cropHeight)
{
using (MemoryStream stream = new MemoryStream(content))
{
return CropImage(stream, x, y, cropWidth, cropHeight);
}
}
public static string CropImage(Stream content, int x, int y, int cropWidth, int cropHeight)
{
using (Bitmap sourceBitmap = new Bitmap(content))
{
// 将选择好的图片缩放
Bitmap bitSource = new Bitmap(sourceBitmap, sourceBitmap.Width, sourceBitmap.Height);
Rectangle cropRect = new Rectangle(x, y, cropWidth, cropHeight);
using (Bitmap newBitMap = new Bitmap(cropWidth, cropHeight))
{
newBitMap.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
using (Graphics g = Graphics.FromImage(newBitMap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(bitSource, new Rectangle(0, 0, newBitMap.Width, newBitMap.Height), cropRect, GraphicsUnit.Pixel);
return GetBitmapBytes(newBitMap);
}
}
}
}
public static string GetBitmapBytes(Bitmap source)
{
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[4];
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
using (MemoryStream tmpStream = new MemoryStream())
{
source.Save(tmpStream, codec, parameters);
byte[] data = new byte[tmpStream.Length];
tmpStream.Seek(0, SeekOrigin.Begin);
tmpStream.Read(data, 0, (int)tmpStream.Length);
string result = Convert.ToBase64String(data);
return result;
}
}
///
/// 图片转换Base64
///
/// 图片网络地址或本地路径
///
public static string ImageToBase64(string urlOrPath)
{
try
{
if (urlOrPath.Contains("http"))
{
Uri url = new Uri(urlOrPath);
using (var webClient = new WebClient())
{
var imgData = webClient.DownloadData(url);
using (var ms = new MemoryStream(imgData))
{
byte[] data = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(data, 0, Convert.ToInt32(ms.Length));
string netImage = Convert.ToBase64String(data);
return netImage;
}
}
}
else
{
FileStream fileStream = new FileStream(urlOrPath, FileMode.Open);
Stream stream = fileStream as Stream;
byte[] data = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(data, 0, Convert.ToInt32(stream.Length));
string netImage = Convert.ToBase64String(data);
return netImage;
}
}
catch (Exception)
{
return null;
}
}
///
/// 按比例缩放图片
///
///
///
///
public static string ResizeImage(Stream content, int resizeWidth, int resizeHeight)
{
using (Bitmap sourceBitmap = new Bitmap(content))
{
int width = sourceBitmap.Width,
height = sourceBitmap.Height;
if (height > resizeHeight || width > resizeWidth)
{
if ((width * resizeHeight) > (height * resizeWidth))
{
resizeHeight = (resizeWidth * height) / width;
}
else
{
resizeWidth = (width * resizeHeight) / height;
}
}
else
{
resizeWidth = width;
resizeHeight = height;
}
// 将选择好的图片缩放
Bitmap bitSource = new Bitmap(sourceBitmap, resizeWidth, resizeHeight);
bitSource.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
using (MemoryStream stream = new MemoryStream())
{
bitSource.Save(stream, ImageFormat.Jpeg);
byte[] data = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(data, 0, Convert.ToInt32(stream.Length));
string newImage = Convert.ToBase64String(data);
return newImage;
}
}
}
}
|