using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace LJLib.Tools.Utils
{
public static class ImgHelper
{
public static Bitmap NewBMPFrom(Image img, int width, int height)
{
var oImg = new Bitmap(width, height);
using (var g = Graphics.FromImage(oImg))
{
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
var dstRect = new Rectangle();
if (srcRect.Width != oImg.Width || srcRect.Height != oImg.Height)
{
if (srcRect.Width * oImg.Height > srcRect.Height * oImg.Width) // 按宽度拉伸
{
dstRect.Width = oImg.Width;
dstRect.Height = dstRect.Width * srcRect.Height / srcRect.Width;
dstRect.X = 0;
dstRect.Y = (oImg.Height - dstRect.Height) / 2;
}
else // 按高度拉伸
{
dstRect.Height = oImg.Height;
dstRect.Width = dstRect.Height * srcRect.Width / srcRect.Height;
dstRect.X = (oImg.Width - dstRect.Width) / 2;
dstRect.Y = 0;
}
}
else // 不拉伸
{
dstRect.Width = oImg.Width;
dstRect.Height = oImg.Height;
dstRect.X = 0; //(oImg.Width - dstRect.Width) / 2;
dstRect.Y = 0; //(oImg.Height - dstRect.Height) / 2;
}
var transparent = Color.FromArgb(0, 255, 255, 255);
g.Clear(transparent);
g.DrawImage(img, dstRect, srcRect, GraphicsUnit.Pixel);
g.Save();
oImg.MakeTransparent(transparent);
}
return oImg;
}
///
/// Byte数组 转 Image
///
///
///
public static Image BytesToImage(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Image image = System.Drawing.Image.FromStream(ms);
return image;
}
///
/// Image 转 Byte数组
///
///
///
public static byte[] ImageToBytes(Image image)
{
ImageFormat format = image.RawFormat;
using (MemoryStream ms = new MemoryStream())
{
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);
}
byte[] buffer = new byte[ms.Length];
//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
}
}
///
/// 添加水印
///
/// 处理图片
/// 添加文字
/// 文字颜色
/// 文字字体
/// 文字位置--x
/// 文字位置--y
///
public static Image AddWaterMark(Image image, string addText, Color color, Font font, string pos_x, string pos_y) //添加文本水印函数
{
try
{
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height); //填充原图
Brush b = new SolidBrush(color); //获取颜色
g.DrawString(addText, font, b, Convert.ToInt16(pos_x), Convert.ToInt16(pos_y)); //在原图中加上水印文字
g.Dispose();
return image;
}
catch (Exception ex)
{
throw;
}
}
///
/// Byte数组 图片添加水印 返回 Byte数组
///
///
///
///
///
///
///
///
public static Byte[] AddWaterMarkWithByte(Byte[] byteArray, string addText, Color color, Font font, string pos_x, string pos_y) //添加文本水印函数
{
try
{
var image = ImgHelper.BytesToImage(byteArray);
Image newimage = ImgHelper.AddWaterMark(image, addText, color, font, pos_x, pos_y);
return ImageToBytes(newimage);
}
catch (Exception ex)
{
throw;
}
}
}
}