#region 上传产品图片
/// <summary>
/// 上传产品图片
/// </summary>
/// <returns></returns>
// GET api/Admin/UpLoadProductImg/
[Route("api/Admin/UpLoadProductImg/")]
[HttpPost]
public async Task<ProductImgResult> UpLoadProductImg()
{
string imgurl = string.Empty;
ProductImgModel model = new ProductImgModel();
try
{
var content = Request.Content;
var uploadDir = System.Web.HttpContext.Current.Server.MapPath("~/DownloadFile/");
if (content.Headers.ContentLength > 0)
{
var sp = new MultipartMemoryStreamProvider();
Task.Run(async () => await Request.Content.ReadAsMultipartAsync(sp)).Wait();
HttpContent item = sp.Contents.LastOrDefault();
string remark = string.Empty;
string ProductImgType = string.Empty;
if (sp.Contents.Count > 1)
{
remark = sp.Contents[0].ReadAsStringAsync().Result;
ProductImgType = sp.Contents[1].ReadAsStringAsync().Result;
}
if (item != null)
{
if (item.Headers.ContentDisposition.FileName != null)
{
var ms = item.ReadAsStreamAsync().Result;
//字节转kb
long kb = ms.Length / 1024;
Stream stream = await content.ReadAsStreamAsync();
Image iSource = Image.FromStream(ms);
string url = "";
var file_name = item.Headers.ContentDisposition.FileName;
//FileInfo fileinfo = new FileInfo(file_name);
string[] strArray = file_name.Split('.');
string Suffix = "." + strArray[1].Replace(""", "");
var filename = Guid.NewGuid().ToString("N");
var uploadfile = filename + Suffix;
string uploadDirurl = uploadDir + filename + Suffix;
if (ProductImgType == "1")//缩略图
{
if (iSource.Width != 340 || iSource.Height != 340 || kb > 300)
{
//压缩图片
Tools t = new Tools();
t.CompressSizePictures(iSource, ProductImgType, kb, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
else
{
Tools t = new Tools();
t.ImageFile(iSource, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
}
else if (ProductImgType == "2")//产品详情滚动图片
{
if (kb > 500)
{
//压缩图片
Tools t = new Tools();
t.CompressSizePictures(iSource, ProductImgType, kb, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
else
{
Tools t = new Tools();
t.ImageFile(iSource, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
}
else if (ProductImgType == "3")//产品详情图片
{
if (iSource.Width != 750 || iSource.Height != 750 || kb > 500)
{
//压缩图片
Tools t = new Tools();
t.CompressSizePictures(iSource, ProductImgType, kb, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
else
{
Tools t = new Tools();
t.ImageFile(iSource, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
}
else
{
Tools t = new Tools();
t.ImageFile(iSource, uploadDirurl);
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
try
{
productimage imgmodel = new productimage();
imgmodel.productid = string.Empty;
imgmodel.productimageurl = url;
imgmodel.productimageid = filename;
imgmodel.productimagename = uploadfile;
imgurl = url;
imgmodel.remark = remark;
imgmodel.productimagetype = ProductImgType;
context.productimage.Add(imgmodel);
context.SaveChanges();
}
catch (Exception ex)
{
}
model.productimageid = filename;
model.remark = remark;
model.productimgtype = ProductImgType;
}
}
}
}
catch (Exception ex)
{
return new ProductImgResult { ResCode = ResCode.Fail, ResMsg = ex.ToString() };
}
model.imgurl = imgurl;
return new ProductImgResult { ResCode = ResCode.Ok, model = model, ResMsg = "添加数据成功" };
}
#endregion 上传产品图片
/// <summary>
/// 工具类
/// </summary>
public class Tools
{
/// <summary>
/// 图片压缩尺寸和质量
/// </summary>
/// <param name="iSource">图片</param>
/// <param name="ProductImgType">类型</param>
/// <param name="filesize">实际文件大小</param>
/// <param name="uploadDirurl">文件路径</param>
/// <returns></returns>
public void CompressSizePictures(Image iSource, string ProductImgType, long filesize, string uploadDirurl)
{
long[] qy = new long[1];
int width = 0, height = 0, size = 0;
if (ProductImgType == "1")
{
qy[0] = 100;
//设置指定宽高
width = 340;
height = 340;
size = 300;
}
if (ProductImgType == "2")
{
qy[0] = 100;
//设置指定宽高
width = 750;
size = 500;
height = 564;
}
if (ProductImgType == "3")
{
qy[0] = 100;
//设置指定宽高
width = 750;
size = 500;
height = 750;
}
ImageFormat tFormat = iSource.RawFormat;
int sW = iSource.Width, sH = iSource.Height;//图片尺寸宽高
GetPicZoomSize(ref sW, ref sH, width, height);
Bitmap filemap = new Bitmap(iSource);
//Graphics g = Graphics.FromImage(filemap);
//g.Clear(Color.WhiteSmoke);
//g.CompositingQuality = CompositingQuality.HighQuality;
//g.SmoothingMode = SmoothingMode.HighQuality;
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//g.DrawImage(iSource, new Rectangle((width - sW) / 2, (height - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
//g.Dispose();
EncoderParameters ep = new EncoderParameters();
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
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;
}
}
filemap.Save(uploadDirurl, jpegICIinfo, ep);//dFile是压缩后的新路径
filemap.Dispose();
}
public void ImageFile(Image iSource, string uploadDirurl)
{
Bitmap filemap = new Bitmap(iSource);
filemap.Save(uploadDirurl);//dFile是压缩后的新路径
filemap.Dispose();
}
}