Files
KJD/Store.Management.Framework/Helpers/ImageHelper.cs
T
2026-09-11 19:11:12 +08:00

214 lines
5.3 KiB
C#

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
{
///// <summary>
///// 判断是否二维码
///// </summary>
///// <param name="file"></param>
///// <returns></returns>
//public static bool CheckQrCode(byte[] file)
//{
// return GetQRCode(file, out int width, out int height, out Image initImage);
//}
///// <summary>
///// 获取二维码
///// </summary>
///// <param name="strSavePath"></param>
///// <returns></returns>
//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;
//}
/// <summary>
/// 图片大小固定
/// </summary>
/// <param name="IsFixedSize"></param>
/// <param name="width"></param>
/// <param name="height"></param>
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;
}
}
/// <summary>
/// 下载图片 并缓存
/// </summary>
/// <param name="imgUrl"></param>
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;
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="item">http请求参数</param>
/// <param name="file">文件bytes</param>
/// <param name="fileKeyName">上传关键字</param>
/// <param name="fileName">文件名</param>
/// <param name="param">附加参数</param>
public static Result<string> UploadImage(HttpItem item, byte[] file, string fileKeyName = "file", string fileName = "", Dictionary<string, string> param = null)
{
Result<string> result = new Result<string>();
Result<HttpResult> resultHttp = UploadImageHttpResult(item, file, fileKeyName, fileName, param);
result.Success = resultHttp.Success;
if (result.Success)
{
result.Data = resultHttp.Data.Html;
}
return result;
}
public static Result<HttpResult> UploadImageHttpResult(HttpItem item, byte[] file, string fileKeyName = "file", string fileName = "", Dictionary<string, string> param = null)
{
Result<HttpResult> result = new Result<HttpResult>();
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);
}
}