HttpPostedFile file = context.Request.Files["avatar_file"];
string datasize = context.Request.Params["avatar_data"];
//{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0} 剪裁之后参数
JavaScriptSerializer jss = new JavaScriptSerializer();
ImgSize imagesize = jss.Deserialize<ImgSize>(datasize);
byte[] FileByte = SetFileToByteArray(file);//图片数组
string strtExtension = System.IO.Path.GetExtension(file.FileName);//图片格式
MemoryStream ms1 = new MemoryStream(FileByte);
Bitmap sBitmap = (Bitmap)Image.FromStream(ms1);
Rectangle section = new Rectangle(new Point(imagesize.ToInt(imagesize.x), imagesize.ToInt(imagesize.y)), new Size(imagesize.ToInt(imagesize.width), imagesize.ToInt(imagesize.height)));
Bitmap CroppedImage = MakeThumbnailImage(sBitmap, section.Width, section.Height, section.Width, section.Height, section.X, section.Y);
class ImgSize
{
//{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0}
public double x { get; set; }
public double y { get; set; }
public double width { get; set; }
public double height { get; set; }
public int rotate { get; set; }
public int ToInt(double doubleValue)
{
return Convert.ToInt32(doubleValue);
}
}
/// <summary>
/// 将From表单file文件转化为byte数组
/// </summary>
/// <param name="File">from提交文件流</param>
/// <returns></returns>
private byte[] SetFileToByteArray(HttpPostedFile File)
{
Stream stream = File.InputStream;
byte[] AyyayByte = new byte[File.ContentLength];
stream.Read(AyyayByte, 0, File.ContentLength);
stream.Close();
return AyyayByte;
}
/// <summary>
/// 裁剪图片并保存
/// </summary>
/// <param name="Image">图片信息</param>
/// <param name="maxWidth">缩略图宽度</param>
/// <param name="maxHeight">缩略图高度</param>
/// <param name="cropWidth">裁剪宽度</param>
/// <param name="cropHeight">裁剪高度</param>
/// <param name="X">X轴</param>
/// <param name="Y">Y轴</param>
public static Bitmap MakeThumbnailImage(Image originalImage, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
{
Bitmap b = new Bitmap(cropWidth, cropHeight);
try
{
using (Graphics g = Graphics.FromImage(b))
{
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel);
Image displayImage = new Bitmap(b, maxWidth, maxHeight);
displayImage.Save("E:\\cutimg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Bitmap bit = new Bitmap(b, maxWidth, maxHeight);
return bit;
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
b.Dispose();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有