old_flsystem/应用/TBAppraisalTools/Tools.cs

248 lines
9.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Api.Framework.Tools;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace TBAppraisalTools
{
class Tools
{
/// <summary>
/// 把directory文件夹里的png文件生成为gif文件放在giffile
/// </summary>
/// <param name="files">图片的数组</param>
/// <param name="giffile">gif保存路径</param>
/// <param name="time">每帧的时间/ms</param>
/// <param name="repeat">是否重复</param>
public static string PngsToGif(string[] files, string giffile, int time = 0, bool repeat = false)
{
try
{
//一般文件名按顺序排
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.Start(giffile);
//每帧播放时间
e.SetDelay(time);
//1不重复0重复
e.SetRepeat(repeat ? 0 : -1);
for (int i = 0, count = files.Length; i < count; i++)
{
if (!string.IsNullOrEmpty(files[i]) && File.Exists(files[i]))
e.AddFrame(Tools.ReadImageFile(files[i]));
}
e.Finish();
return giffile;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 按比例缩放图片
/// </summary>
/// <param name="SourceImage">图片路径</param>
/// <param name="TargetWidth">宽度</param>
/// <param name="TargetHeight">高度</param>
/// <returns></returns>
public static Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight)
{
int IntWidth; //新的图片宽
int IntHeight; //新的图片高
try
{
System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
Bitmap SaveImage = new Bitmap(TargetWidth, TargetHeight);
Graphics g = Graphics.FromImage(SaveImage);
g.Clear(Color.White);
if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小
{
IntWidth = TargetWidth;
IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
}
else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大
{
IntHeight = TargetHeight;
IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
}
else if (SourceImage.Width <= TargetWidth && SourceImage.Height <= TargetHeight) //长宽比目的图片长宽都小
{
IntHeight = SourceImage.Width;
IntWidth = SourceImage.Height;
}
else//长宽比目的图片的长宽都大
{
IntWidth = TargetWidth;
IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
if (IntHeight > TargetHeight)//重新计算
{
IntHeight = TargetHeight;
IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
}
}
g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
SourceImage.Dispose();
return SaveImage;
}
catch (Exception ex)
{
}
return null;
}
/// <summary>
/// 通过FileStream 来打开文件这样就可以实现不锁定Image文件到时可以让多用户同时访问Image文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Bitmap ReadImageFile(string path)
{
FileStream fs = File.OpenRead(path); //OpenRead
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
fs.Read(image, 0, filelength); //按字节流读取
Image result = Image.FromStream(fs);
fs.Close();
Bitmap bit = new Bitmap(result);
return bit;
}
/// <summary>
/// 下载网络图片
/// </summary>
/// <param name="url">图片的链接</param>
/// <param name="path">下载图片保存的路径</param>
/// <param name="postfix"></param>
public static void DownloadImage(string url, string path)
{
HttpWebResponse rsp = null;
try
{
url = (url.StartsWith("http") ? url : ("http:" + url));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.Expect100Continue = false;
req.Method = "GET";
req.KeepAlive = true;
req.ContentType = "image/*";
rsp = (HttpWebResponse)req.GetResponse();
}
catch (Exception ex)
{
throw ex;
}
Stream stream = null;
try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
Image.FromStream(stream).Save(path);
}
catch
{
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
finally
{
// 释放资源
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
}
/// <summary>
/// 图片放大缩小 变形操作
/// </summary>
/// <param name="img"></param>
/// <param name="rec"></param>
/// <param name="size"></param>
/// <returns></returns>
public static Bitmap CutEllipse(Image img, Rectangle rec, Size size)
{
Bitmap bitmap = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
{
br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillEllipse(br, new Rectangle(Point.Empty, size));
//g.FillRectangle(br, new Rectangle(Point.Empty, size));
}
}
return bitmap;
}
/// <summary>
/// 这方法将以前的地址更新成新的目录地址(防止用户更改了软件目录)
/// </summary>
/// <param name="currentFile">之前完整的路径</param>
/// <param name="node">查找的节点(文件名前的那一段内容)</param>
/// <param name="newPath">软件的目录</param>
/// <returns></returns>
public static string RefreshFileAddress(string currentFile, string node, string newPath)
{
var index = currentFile.IndexOf(node);
var imageNick = currentFile.Remove(0, index + node.Length);//获取图片名和后缀
return Util.MapFile(imageNick, newPath);//当前项目的完成图片路径(以上操作是为了怕用户转移程序路径)
}
/// <summary>
/// 更新文本格式,优惠券模板中label超出背景部分自动换行(比如标题过长)
/// </summary>
/// <param name="totalWidth">总背景的宽</param>
/// <param name="startPoint">文本的X坐标(起点)</param>
/// <param name="srcText">文本内容</param>
/// <param name="tWidth">格式化后的文本真正的宽</param>
/// <returns></returns>
public static string RefreshTextFormat(int totalWidth, int startPoint, string srcText, float tWidth)
{
var surLocation = totalWidth - startPoint;
if (surLocation < tWidth)
{
var nuit = Math.Ceiling(tWidth / srcText.Length);//一个字的宽度
var rowNum = (int)Math.Ceiling(surLocation / nuit);//大约一列最多能放几个字
double rows = srcText.Length / rowNum;//行数
int count = (int)Math.Ceiling(rows);
for (var i = 1; i <= count; i++)
{
srcText = srcText.Insert(rowNum * i, "\r\n");
}
}
return srcText;
}
/// <summary>
/// 通过路径删除所有的文件,以及文件夹
/// </summary>
/// <param name="srcPath"></param>
public static void DelectDir(string srcPath)
{
DirectoryInfo dir = new DirectoryInfo(srcPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
}
}
}
}