很奇怪的问题,如果从左上角开始选择的话切出来的图片没有问题 不管选多大,
而且 只要选择的区域是100X100进行切割也没问题。 在其他地方切割会有黑的切出来。
- JScript code
/// 头像预览 function preview(img, selection) { var scaleX = 100 / (selection.width || 1); var scaleY = 100 / (selection.height || 1); $imgPreView.css({ width: Math.round(scaleX * faceSize.w) + 'px', height: Math.round(scaleY * faceSize.h) + 'px', marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' }); updateSubmitData({ x: selection.x1, y: selection.y1, w: selection.width, h: selection.height }); } ///更新提交数据 function updateSubmitData(data) { submitData.x = data.x; submitData.y = data.y; submitData.h = data.h; submitData.w = data.w; }
- C# code
private void RestImage(HttpContext context) { int x = context.Request.Params["x"].TryInt(); int y = context.Request.Params["y"].TryInt(); int w = context.Request.Params["w"].TryInt(); int h = context.Request.Params["h"].TryInt(); string filePath = FilePath.GetPluginIconAbsolutePath(AppVars.PluginIconRootPath, CurrentPlugin.PluginId); MemoryStream st = null; FileStream sw = null; st = ImageHelper.ResetImg(context.Server.MapPath("~/temp/" + CurrentPlugin.PluginId + "_temp.jpg"), w, h, x, y, 100, 100); if (st == null) { Error(context, "获取图片超时,请稍候再试!"); return; } sw = new FileStream(filePath, FileMode.Create); if (sw == null) { Error(context, "获取图片超时,请稍候再试!"); return; } try { st.WriteTo(sw); } catch (Exception ex) { Error(context, "获取图片超时,请稍候再试!"); LogManager.WriteException(ex, Utils.GetIP()); return; } finally { st.Dispose(); sw.Dispose(); } //if (File.Exists(context.Server.MapPath("~/temp/") + CurrentPlugin.PluginId + "_temp.jpg")) //{ // File.Delete(context.Server.MapPath("~/temp/") + CurrentPlugin.PluginId + "_temp.jpg"); //} context.Response.ContentType = "text/plain"; context.Response.Write(JsonHelper.ToJSON(new { done = 1, img = new { src = AppVars.MiscHost + FilePath.GetPluginIconRelativePath(AppVars.PluginIconRootPath, HttpContext.Current.Request["pluginId"].TryInt(0)), h = 100, w = 100 } })); }/// <summary> /// 对缩放的原始图片进行切割 /// </summary> /// <param name="ImgFile">原始图片的位置</param> /// <param name="PicWidth">缩放后的图片宽度</param> /// <param name="PicHeight">缩放后的图片高度</param> /// <param name="PointX">切割缩放后图片的X起点</param> /// <param name="PointY">切割缩放后图片的Y起点</param> /// <param name="CutWidth">缩放图切割的宽度</param> /// <param name="CutHeight">缩放图切割的宽度</param> /// <returns>原始图切割出的流</returns> public static MemoryStream ResetImg(string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight) { Image imgPhoto = null; Bitmap bmPhoto = null; Graphics gbmPhoto = null; MemoryStream ms2 = null; try { imgPhoto = Image.FromFile(ImgFile); bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); gbmPhoto = Graphics.FromImage(bmPhoto); //根据切割时图片的尺寸对原始图片进行切割 gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight, GraphicsUnit.Pixel); ms2 = new MemoryStream(); bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception e) { LogManager.WriteException(e, Utils.GetIP()); } finally { if (imgPhoto != null) { imgPhoto.Dispose(); } if (gbmPhoto != null) { gbmPhoto.Dispose(); } if (bmPhoto != null) { bmPhoto.Dispose(); } } return ms2; }