using MAX.Core.Commons; using MAX.Core.Helpers; using MAX.Framework.Logger; using Store.Management.Framework.Caches; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Store.Management.Framework.Helpers; public class ImageHelper { ///// ///// 判断是否二维码 ///// ///// ///// //public static bool CheckQrCode(byte[] file) //{ // return GetQRCode(file, out int width, out int height, out Image initImage); //} ///// ///// 获取二维码 ///// ///// ///// //public static bool GetQRCode(byte[] file, out int width, out int height, out Image initImage) //{ // initImage = null; // width = 0; // height = 0; // try // { // MemoryStream ms = new MemoryStream(file); // initImage = Image.FromStream(ms); // ms.Close(); // width = initImage.Width; // height = initImage.Height; // if (initImage.Width < 10 || initImage.Height < 10) // { // initImage.Dispose(); // initImage = null; // return false; // } // BarcodeReader reader = new BarcodeReader(); // var result = reader.Decode((Bitmap)initImage); // if (result == null || string.IsNullOrEmpty(result.Text)) // { // reader = null; // result = null; // return false; // } // reader = null; // result = null; // initImage.Dispose(); // initImage = null; // } // catch // { // return false; // } // return true; //} /// /// 图片大小固定 /// /// /// /// public static void FixedSize(bool IsMainImg, ref int width, ref int height) { if (IsMainImg) { if (width >= 800 && height >= 800) { width = 800; height = 800; } else { if (width > 640) { width = 640; height = height * 640 / width; } if (height > 1920) height = 1920; } } else { if (width > 640) { width = 640; height = height * 640 / width; } if (height > 1920) height = 1920; } } /// /// 下载图片 并缓存 /// /// public static byte[] DownloadImage(string imgUrl) { if (string.IsNullOrEmpty(imgUrl)) { return null; } imgUrl = imgUrl.Trim(); if (imgUrl.StartsWith("//")) { imgUrl = "http:" + imgUrl; } byte[] data = ImageCache.Instance.GetBytes(imgUrl); if (data == null) { data = HttpHelper.DownloadFile(imgUrl); if (data != null) { ImageCache.Instance.TryAdd(imgUrl, data); } } return data; } /// /// 上传图片 /// /// http请求参数 /// 文件bytes /// 上传关键字 /// 文件名 /// 附加参数 public static Result UploadImage(HttpItem item, byte[] file, string fileKeyName = "file", string fileName = "", Dictionary param = null) { Result result = new Result(); Result resultHttp = UploadImageHttpResult(item, file, fileKeyName, fileName, param); result.Success = resultHttp.Success; if (result.Success) { result.Data = resultHttp.Data.Html; } return result; } public static Result UploadImageHttpResult(HttpItem item, byte[] file, string fileKeyName = "file", string fileName = "", Dictionary param = null) { Result result = new Result(); try { // 边界符 var boundary = "----" + Guid.NewGuid().ToString("N"); if (string.IsNullOrEmpty(fileName)) { fileName = Guid.NewGuid().ToString("N").Substring(0, 4) + ".jpg"; } item.PostEncoding = Encoding.UTF8; item.Method = MethodType.Post; item.PostDataType = PostDataType.Byte; item.ContentType = "multipart/form-data; boundary=" + boundary; var memStream = new MemoryStream(); if (param != null) { foreach (var key in param.Keys) { string value = param[key]; memStream.WriteString("--" + boundary); memStream.WriteString($"Content-Disposition: form-data; name=\"{key}\"" + "\r\n"); memStream.WriteString(value); } } memStream.WriteString("--" + boundary); memStream.WriteString($"Content-Disposition: form-data; name=\"{fileKeyName}\"; filename=\"{fileName}\""); memStream.WriteString("Content-Type: image/jpeg\r\n"); memStream.Write(file, 0, file.Length); memStream.WriteString("\r\n"); memStream.WriteString("--" + boundary + "--"); var tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); item.PostdataByte = memStream.ToArray(); var httpresult = HttpHelper.Request(item); result.Success = true; result.Data = httpresult; } catch (Exception ex) { NLogger.Instance.Error(ex, "Http上传图片错误"); result.Msg = ex.Message; } return result; } } static class StreamExtension { public static void WriteString(this MemoryStream memStream, string value) { byte[] valuebyte = Encoding.UTF8.GetBytes(value + "\r\n"); memStream.Write(valuebyte, 0, valuebyte.Length); } }