using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Common.system
{
///
/// 图片帮助类
/// 创建人:赵耀
/// 创建时间:2018年11月1日
///
public class ImageHelper
{
///
/// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
///
/// 图片位置
///
public static Bitmap ReadBitmapFile(string path)
{
try
{
if (File.Exists(path))
{
FileStream fs = File.OpenRead(path); //OpenRead
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
fs.Read(image, 0, filelength); //按字节流读取
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
fs.Close();
Bitmap bit = new Bitmap(result);
return bit;
}
else
return null;
}
catch (Exception)
{
return null;
}
}
///
/// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
///
/// 图片位置
///
public static Image ReadImageFile(string path)
{
try
{
if (File.Exists(path))
{
FileStream fs = File.OpenRead(path); //OpenRead
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
fs.Read(image, 0, filelength); //按字节流读取
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
fs.Close();
return result;
}
else
return null;
}
catch (Exception)
{
return null;
}
}
#region 截图统一方法
///
/// 截图通用方法 创建人:赵耀 创建时间:2020年8月11日
///
/// 截图的区域,设置new Rectangle(0, 0, 0, 0)为截全屏
/// 图片存储路径,为空或null则保存到临时文件夹
/// 压缩等级,0到100,0 最差质量,100 最佳
///
public static BitmapImage GetScreenshot(Rectangle ScreenSize, string ImageSavePath, long level = -1)
{
//try
//{
// System.Drawing.Size bounds = PrimaryScreen.DESKTOP;
// //if (string.IsNullOrEmpty(ImageSavePath))
// //{
// // ImageSavePath = GetTempImagePath();//如果为空则指定临时存储路径
// //}
// double scaleWidth = (bounds.Width * 1.0) / SystemParameters.PrimaryScreenWidth;
// double scaleHeight = (bounds.Height * 1.0) / SystemParameters.PrimaryScreenHeight;
// int width = bounds.Width;
// int height = bounds.Height;
// if (ScreenSize.Size != new System.Drawing.Size(0, 0))
// {
// width = (int)(ScreenSize.Size.Width * scaleWidth);
// height = (int)(ScreenSize.Size.Height * scaleHeight);
// }
// int l = (int)(ScreenSize.X * scaleWidth);
// int t = (int)(ScreenSize.Y * scaleHeight);
// //if (_Bitmap != null)
// //{
// // _Bitmap.Dispose();
// //}
// Bitmap _Bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// using (Graphics g = Graphics.FromImage(_Bitmap))
// {
// g.CopyFromScreen(l, t, 0, 0, _Bitmap.Size, CopyPixelOperation.SourceCopy);
// Compress(_Bitmap, ImageSavePath, level);
// }
// Uri uri = new Uri(ImageSavePath, UriKind.Absolute);
// BitmapImage bimg = new BitmapImage(uri);
// GC.Collect();
// return bimg;
//}
//catch (Exception ex)
//{
// //LogHelper.WriteErrLog("【截图】(GetBitmapSource)截图失败," + ex.Message, ex);
return null;
//}
}
//private static Bitmap _Bitmap = null;
//public static string GetRGB(int x, int y)
//{
// StringBuilder sb = new StringBuilder();
// try
// {
// Color color = _Bitmap.GetPixel(x, y);
// sb.Append("RGB:(");
// sb.Append(color.R.ToString());
// sb.Append(",");
// sb.Append(color.G.ToString());
// sb.Append(",");
// sb.Append(color.B.ToString());
// sb.Append(")");
// }
// catch (Exception ex)
// {
// throw ex;
// }
// return sb.ToString();
//}
#endregion 截图统一方法
#region 图片压缩
///
/// 图片压缩(降低质量以减小文件的大小) 创建人:赵耀 创建时间:2020年8月11日
///
/// 传入的Bitmap对象
/// 压缩后的图片保存路径
/// 压缩等级,-1为config配置的值,0到100,0 最差质量,100 最佳
public static void Compress(Bitmap srcBitMap, string destFile, long level)
{
if (level <= -1)
{
int ImageCompressionLevel = int.Parse(FileToolsCommon.GetConfigValue("ImageCompressionLevel"));
level = ImageCompressionLevel;
}
Stream s = new FileStream(destFile, FileMode.Create);
Compress(srcBitMap, s, level);
s.Close();
}
///
/// 图片压缩(降低质量以减小文件的大小) 创建人:赵耀 创建时间:2020年8月11日
///
/// 传入的Bitmap对象
/// 压缩后的Stream对象
/// 压缩等级,0到100,0 最差质量,100 最佳
public static void Compress(Bitmap srcBitmap, Stream destStream, long level)
{
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
//获取表示jpeg编解码器的imagecodecinfo对象
myImageCodecInfo = GetEncoderInfo("image/jpeg");
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, level);
myEncoderParameters.Param[0] = myEncoderParameter;
srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
}
///
/// 获取解码器 创建人:赵耀 创建时间2020年8月11日
///
///
///
private 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;
}
///
/// 压缩图片 -测试方法 待完善 暂无用
///
/// 原图片
/// 压缩后保存图片地址
/// 压缩质量(数字越小压缩率越高)1-100
/// 压缩后图片的最大大小
/// 是否是第一次调用
///
public static bool CompressImage(Bitmap _bitmap, string dFile, int flag = 90, int size = 300)
{
////如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
//FileInfo firstFileInfo = new FileInfo(sFile);
//if (sfsc == true && firstFileInfo.Length < size * 1024)
//{
// firstFileInfo.CopyTo(dFile);
// return true;
//}
//Image iSource = Image.FromFile(sFile);
Image iSource = _bitmap;
ImageFormat tFormat = iSource.RawFormat;
int dHeight = iSource.Height / 2;
int dWidth = iSource.Width / 2;
int sW, sH;
//按比例缩放
System.Drawing.Size tem_size = new System.Drawing.Size(iSource.Width, iSource.Height);
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
{
sW = dWidth;
sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(System.Drawing.Color.WhiteSmoke);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
if (flag > 0)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
FileInfo fi = new FileInfo(dFile);
if (fi.Length > 1024 * size)
{
flag -= 10;
CompressImage(_bitmap, dFile, flag, size);
}
}
//ob.Save(dFile, jpegICIinfo, ep);
//FileInfo fi = new FileInfo(dFile);
//bool IsAgain = false;
//if (fi.Length > 1024 * size)
//{
// fi.CopyTo(dFile + fi.Length);
// fi.Delete();
// Bitmap newbitmap = ReadImageFile(dFile + fi.Length);
// CompressImage(newbitmap, dFile, flag, size);
// FileToolsCommon.DeleteFile(dFile + fi.Length);
//}
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
#endregion 图片压缩
#region 截屏指定UI控件
///
/// 保存图片
///
/// 需要截图的UI控件
/// 图片保存地址 命名 1.png
/// 保存宽
/// 保存高
public static void SaveUIToImage(FrameworkElement ui, string filePathName, int width, int height)
{
try
{
System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create);
RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96d, 96d,
PixelFormats.Pbgra32);
bmp.Render(ui);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
fs.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#endregion
}
}