using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace Framework
{
public class ImageClass
{
public Image ResourceImage;
public int Width=0;
public int Height=0;
private int ImageWidth;
private int ImageHeight;
private ImageFormat imgFormat;
public string ErrMessage;
public ImageClass(string ImageFileName)
{
ResourceImage = Image.FromFile(ImageFileName);
Width = ResourceImage.Width;
Height = ResourceImage.Height;
imgFormat = ResourceImage.RawFormat;
ErrMessage = "";
}
public ImageClass(byte[] Img)
{
MemoryStream imgStream = new MemoryStream(Img);
try
{
ResourceImage = System.Drawing.Image.FromStream(imgStream);
Width = ResourceImage.Width;
Height = ResourceImage.Height;
imgFormat = ResourceImage.RawFormat;
}
catch (Exception ex)
{
ErrMessage = ex.ToString();
}
}
public bool ThumbnailCallback()
{
return false;
}
public Image GetReducedImage(int Width, int Height)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
return ReducedImage;
}
catch (Exception e)
{
ErrMessage = e.Message;
return null;
}
}
public bool GetReducedImage(int Width, int Height, string targetFilePath)
{
try
{
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ps.Param[0] = p;
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
Graphics g = System.Drawing.Graphics.FromImage(ReducedImage);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
g.DrawImage(ResourceImage, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, ResourceImage.Width, ResourceImage.Height), GraphicsUnit.Pixel);
ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());
if (imgCodecInfo == null)
{
ReducedImage.Save(targetFilePath, imgFormat);
}
else
{
ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
}
ReducedImage.Dispose();
g.Dispose();
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
}
public Image GetReducedImage(double Percent)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent);
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
return ReducedImage;
}
catch (Exception e)
{
ErrMessage = e.Message;
return null;
}
}
public bool GetReducedImage(double Percent, string targetFilePath)
{
try
{
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ps.Param[0] = p;
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent);
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
Graphics g = System.Drawing.Graphics.FromImage(ReducedImage);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
g.DrawImage(ResourceImage, new Rectangle(0, 0, ImageWidth, ImageHeight), new Rectangle(0, 0, ResourceImage.Width, ResourceImage.Height), GraphicsUnit.Pixel);
ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());
if (imgCodecInfo == null)
{
ReducedImage.Save(targetFilePath, imgFormat);
}
else
{
ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
}
ReducedImage.Dispose();
g.Dispose();
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
}
public bool GetReducedImage(string targetFilePath)
{
try
{
ResourceImage.Save(targetFilePath, ImageHelper.GetFormat(targetFilePath));
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
}
public bool CaptureImg(string targetFilePath, int width, int height, int spaceX, int spaceY)
{
try
{
//载入底图
int x = 0; //截取X坐标
int y = 0; //截取Y坐标
//原图宽与生成图片宽 之差
//当小于0(即原图宽小于要生成的图)时,新图宽度为较小者 即原图宽度 X坐标则为0
//当大于0(即原图宽大于要生成的图)时,新图宽度为设置值 即width X坐标则为 sX与spaceX之间较小者
//Y方向同理
int sX = ResourceImage.Width - width;
int sY = ResourceImage.Height - height;
if (sX > 0)
{
x = sX > spaceX ? spaceX : sX;
}
else
{
width = ResourceImage.Width;
}
if (sY > 0)
{
y = sY > spaceY ? spaceY : sY;
}
else
{
height = ResourceImage.Height;
}
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ps.Param[0] = p;
Bitmap ReducedImage = new Bitmap(width, height);
Graphics g = System.Drawing.Graphics.FromImage(ReducedImage);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
g.DrawImage(ResourceImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());
if (imgCodecInfo == null)
{
ReducedImage.Save(targetFilePath, imgFormat);
}
else
{
ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
}
//释放资源
//saveImage.Dispose();
ReducedImage.Dispose();
g.Dispose();
}
catch (Exception ex)
{
ErrMessage = ex.Message;
return false;
}
return true;
}
public bool MergerImg(string targetFilePath, int ImgWidth, int ImgHeight)
{
try
{
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ps.Param[0] = p;
//创建要显示的图片对象,根据参数的个数设置宽度
Bitmap ReducedImage = new Bitmap(ImgWidth, ImgHeight);
Graphics g = Graphics.FromImage(ReducedImage);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
int StartX = (ImgWidth - this.Width) / 2;
int StartY = (ImgHeight - this.Height) / 2;
g.DrawImage(ResourceImage, StartX, StartY, this.Width, this.Height);
//保存图象
ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());
if (imgCodecInfo == null)
{
ReducedImage.Save(targetFilePath, imgFormat);
}
else
{
ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
}
//释放资源
ReducedImage.Dispose();
g.Dispose();
}
catch (Exception ex)
{
ErrMessage = ex.Message;
return false;
}
return true;
}
public bool MergerImg(string targetFilePath, System.Drawing.Image image, int ImgWidth, int ImgHeight)
{
try
{
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ps.Param[0] = p;
//创建要显示的图片对象,根据参数的个数设置宽度
Bitmap ReducedImage = new Bitmap(ImgWidth, ImgHeight);
Graphics g = Graphics.FromImage(ReducedImage);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
int StartX = (ImgWidth - image.Width) / 2;
int StartY = (ImgHeight - image.Height) / 2;
g.DrawImage(image, StartX, StartY, image.Width, image.Height);
//保存图象
ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());
if (imgCodecInfo == null)
{
ReducedImage.Save(targetFilePath, imgFormat);
}
else
{
ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
}
//释放资源
ReducedImage.Dispose();
g.Dispose();
}
catch (Exception ex)
{
ErrMessage = ex.Message;
return false;
}
return true;
}
//返回图片解码器信息用于jpg图片
private ImageCodecInfo GetCodecInfo(string str)
{
string ext = str.Substring(str.LastIndexOf(".") + 1);
string mimeType = "";
switch (ext.ToLower())
{
case "jpe":
case "jpg":
case "jpeg":
mimeType = "image/jpeg";
break;
case "bmp":
mimeType = "image/bmp";
break;
case "png":
mimeType = "image/png";
break;
case "tif":
case "tiff":
mimeType = "image/tiff";
break;
default:
mimeType = "image/jpeg";
break;
}
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}
public void Dispose()
{
ResourceImage.Dispose();
}
}
}
C#放缩、截取、合并图片并生成高质量新图的类
最新推荐文章于 2021-03-19 19:59:36 发布