1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System;
- using System.Drawing;
- using System.IO;
- namespace GxPress.Common.Tools
- {
- public static class ImFileHelper
- {
- /// <summary>
- /// 得到图片的 Size 值
- /// </summary>
- /// <param name="bytes">图片的二进制数据</param>
- /// <returns>图片的 Size 值</returns>
- public static Size GetSize(byte[] bytes)
- {
- var size = Size.Empty;
- if (bytes != null && bytes.Length > 0)
- {
- using var ms = new MemoryStream(bytes);
- size = GetSize(ms);
- }
- return size;
- }
- /// <summary>
- /// 得到图片的 Size 值
- /// </summary>
- /// <param name="stream">图片的文件数据流</param>
- /// <returns>图片的 Size 值</returns>
- public static Size GetSize(Stream stream)
- {
- Size size;
- using (var originalImage = Image.FromStream(stream))
- {
- size = originalImage.Size;
- }
- return size;
- }
- /// <summary>
- /// 保存文件
- /// </summary>
- /// <param name="path">文件保存路径</param>
- /// <param name="bytes">文件二进制数据</param>
- /// <returns>保存文件是否成功!</returns>
- public static void Save(string path, byte[] bytes)
- {
- if (string.IsNullOrWhiteSpace(path))
- throw new ArgumentException("文件保存路径不能为空");
- if (bytes == null || bytes.Length == 0)
- throw new ArgumentException("文件二进制数据不能为空");
- var info = new FileInfo(path);
- if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
- info.Delete();
- File.WriteAllBytes(info.FullName, bytes);
- }
- }
- }
|