C# ImageHelper 图片帮助类
C# 全选
/// <summary>
/// 图片帮助类
/// </summary>
public class ImageHelper
{
public const string IMAGE_FORMATS = ".jpg;.jpeg;.png;.bmp;.gif;";
/// <summary>
/// 判断是否图片类型
/// </summary>
/// <param name="fileExtension">文件扩展名</param>
/// <returns></returns>
public static bool IsImageType(string fileExtension)
{
return IMAGE_FORMATS.ToLower().Contains(fileExtension.ToLower());
}
/// <summary>
/// byte数组转图片
/// </summary>
/// <param name="bs"></param>
/// <returns></returns>
public static Image FromBytes(byte[] bs)
{
if (bs == null)
{
return null;
}
using (MemoryStream stream = new MemoryStream(bs))
{
return Image.FromStream(stream);
}
}
/// <summary>
/// 指定颜色替换成另一种颜色
/// </summary>
/// <param name="img">原图</param>
/// <param name="w">图宽</param>
/// <param name="h">图高</param>
/// <param name="R">要被替换颜色的RGB的R</param>
/// <param name="G">要被替换颜色的RGB的G</param>
/// <param name="B">要被替换颜色的RGB的B</param>
/// <param name="r">替换色的RGB的R</param>
/// <param name="g">替换色的RGB的G</param>
/// <param name="b">替换色的RGB的B</param>
/// <returns>处理后的结果图像</returns>
public static Bitmap ReplaceColor(Bitmap img, int w, int h, int R, int G, int B, int r, int g, int b, int tolerance = 0)
{
//Bitmap bt = new Bitmap(ConvertTo32Bitmap(img)); //转换32位位图,注意:如果原图是32位,不需要转换!
Bitmap bt = new Bitmap(img);
Rectangle rect = new Rectangle(0, 0, w, h);
BitmapData bmpdata = bt.LockBits(rect, ImageLockMode.ReadWrite, bt.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = Math.Abs(bmpdata.Stride) * bt.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
int len = rgbValues.Length;
byte a = (byte)tolerance;
byte R1 = (byte)R;
byte G1 = (byte)G;
byte B1 = (byte)B;
byte r1 = (byte)r;
byte g1 = (byte)g;
byte b1 = (byte)b;
for (int i = 0; i < len; i += 4)
{
//Format32bppRgb是用4个字节表示一个像素,第一个字节表示RGB的B值,第一个表示为G值,第三个表示为R值,第四个表示为Alpha值
if (Math.Abs(rgbValues[i] - B1) < a && Math.Abs(rgbValues[i + 1] - G1) < a && Math.Abs(rgbValues[i + 2] - R1) < a)
{
rgbValues[i] = b1;
rgbValues[i + 1] = g1;
rgbValues[i + 2] = r1;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bt.UnlockBits(bmpdata);
return bt;
}
/// <summary>
/// Image图像转化为32位位图
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public static Bitmap ConvertTo32Bitmap(Image img)
{
var bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
using (var gr = Graphics.FromImage(bmp))
gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
return bmp;
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImage">源图</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(Image image, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = image;
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
//originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImage">源图</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static Image MakeThumbnail(Image image, int width, int height, string mode)
{
Image originalImage = image;
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
return bitmap;
}
catch (System.Exception e)
{
throw e;
}
finally
{
g.Dispose();
}
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网