/// <summary> /// Generate the new bitmap /// </summary> /// <param name="oldImage">A <see cref="System.Drawing.Image"/> source</param> /// <param name="cut">The <see cref="BitmapCutter.Core.API.Cutter"/></param> /// <returns></returns> public static Bitmap GenerateBitmap(Image oldImage, Cutter cut) { if (oldImage == null) throw new ArgumentNullException("oldImage"); Image newBitmap = new Bitmap(cut.SaveWidth, cut.SaveHeight); //Re-paint the oldImage using (Graphics g = Graphics.FromImage(newBitmap)) { g.InterpolationMode = InterpolationMode.High; g.SmoothingMode = SmoothingMode.HighQuality; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(oldImage, new Rectangle(0, 0, cut.SaveWidth, cut.SaveHeight), new Rectangle(0, 0, cut.Width, cut.Height), GraphicsUnit.Pixel); g.Save(); } Bitmap bmp = new Bitmap(cut.CutterWidth, cut.CutterHeight); using (Graphics g = Graphics.FromImage(bmp)) { g.InterpolationMode = InterpolationMode.High; g.SmoothingMode = SmoothingMode.HighQuality; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(newBitmap, 0, 0, new Rectangle(cut.X, cut.Y, cut.CutterWidth, cut.CutterHeight), GraphicsUnit.Pixel); g.Save(); newBitmap.Dispose(); } return bmp; }
/// <summary> /// Generate the new bitmap /// </summary> /// <param name="oldImage">A <see cref="System.Drawing.Image"/> source</param> /// <param name="cut">The <see cref="BitmapCutter.Core.API.Cutter"/></param> /// <returns></returns> public static Bitmap GenerateBitmap(Image oldImage, Cutter cut) { if (oldImage == null) { throw new ArgumentNullException("oldImage"); } Image newBitmap = new Bitmap(cut.SaveWidth, cut.SaveHeight); //Re-paint the oldImage using (Graphics g = Graphics.FromImage(newBitmap)) { g.InterpolationMode = InterpolationMode.High; g.SmoothingMode = SmoothingMode.HighQuality; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(oldImage, new Rectangle(0, 0, cut.SaveWidth, cut.SaveHeight), new Rectangle(0, 0, cut.Width, cut.Height), GraphicsUnit.Pixel); g.Save(); } Bitmap bmp = new Bitmap(cut.CutterWidth, cut.CutterHeight); using (Graphics g = Graphics.FromImage(bmp)) { g.InterpolationMode = InterpolationMode.High; g.SmoothingMode = SmoothingMode.HighQuality; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(newBitmap, 0, 0, new Rectangle(cut.X, cut.Y, cut.CutterWidth, cut.CutterHeight), GraphicsUnit.Pixel); g.Save(); newBitmap.Dispose(); } return(bmp); }
public string GenerateBitmap(string src) { try { if (src.Contains("../")) { src = src.Remove(src.IndexOf("../"), 3); } FileInfo fi = new FileInfo(src); string ext = fi.Extension; string newfileName = "Content/Temp/" + Guid.NewGuid().ToString("N") + ext; //Image.GetThumbnailImageAbort abort = null; Bitmap oldBitmap = new Bitmap(src); HttpContext context = HttpContext.Current; Cutter cut = new Cutter( double.Parse(context.Request["zoom"]), -int.Parse(context.Request["x"]), -int.Parse(context.Request["y"]), int.Parse(context.Request["width"]), int.Parse(context.Request["height"]), oldBitmap.Width, oldBitmap.Height); Bitmap bmp = Helper.GenerateBitmap(oldBitmap, cut); oldBitmap.Dispose(); string temp = Path.Combine(context.Server.MapPath("~/"), newfileName); if (ext.ToLower().Equals(".png")) { bmp.Save(temp, ImageFormat.Png); } else if (ext.ToLower().Equals(".gif")) { bmp.Save(temp, ImageFormat.Gif); } else if (ext.ToLower().Equals(".jpg") || ext.ToLower().Equals(".jpeg")) { bmp.Save(temp, ImageFormat.Jpeg); } else if (ext.ToLower().Equals(".bmp")) { bmp.Save(temp, ImageFormat.Bmp); } bmp.Dispose(); return "{msg:'success',src:'" + "../"+newfileName + "'}"; } catch (Exception ex) { return "{msg:'" + ex.Message + "'}"; } }
public string GenerateBitmap(string src) { try { src = src.Replace("/", "\\"); src = src.Replace("..\\", string.Empty); FileInfo fi = new FileInfo(src); string ext = fi.Extension; string newfileName = "DataUser/ImgAth/Temp/" + DateTime.Now.ToString("yyMMddHHmmssffff") + ".png"; //Image.GetThumbnailImageAbort abort = null; Bitmap oldBitmap = new Bitmap(src); HttpContext context = HttpContext.Current; Cutter cut = new Cutter( double.Parse(context.Request["zoom"]), -int.Parse(context.Request["x"]), -int.Parse(context.Request["y"]), int.Parse(context.Request["width"]), int.Parse(context.Request["height"]), oldBitmap.Width, oldBitmap.Height); Bitmap bmp = Helper.GenerateBitmap(oldBitmap, cut); oldBitmap.Dispose(); string temp = Path.Combine(context.Server.MapPath("~/"), newfileName); bmp.Save(temp, ImageFormat.Png); bmp.Dispose(); return("{msg:'success',src:'../../" + newfileName + "'}"); } catch (Exception ex) { return("{msg:'" + ex.Message + "'}"); } }
public string GenerateBitmap(string src) { try { HttpContext context = HttpContext.Current; FileInfo fi = new FileInfo(src); string ext = fi.Extension; var ran = new Random().Next(1, 1000000); string newfileName = src.Substring(src.LastIndexOf("/") + 1).Replace(src.Substring(src.LastIndexOf(".")), "_" + ran.ToString()) + ".png"; //string newfileName = Guid.NewGuid().ToString("N") + ".png"; src = context.Server.MapPath(src); var ImageUploadPath = ApplicationContext.AppSetting.Image_Upload_Path; Bitmap oldBitmap = new Bitmap(src); Cutter cut = new Cutter( double.Parse(context.Request["zoom"]), -int.Parse(context.Request["x"]), -int.Parse(context.Request["y"]), int.Parse(context.Request["width"]), int.Parse(context.Request["height"]), oldBitmap.Width, oldBitmap.Height); Bitmap bmp = Helper.GenerateBitmap(oldBitmap, cut); oldBitmap.Dispose(); string temp = Path.Combine(context.Server.MapPath(ImageUploadPath), newfileName); bmp.Save(temp, ImageFormat.Png); bmp.Dispose(); return("{msg:'success',src:'" + ImageUploadPath + newfileName + "'}"); } catch (Exception ex) { return("{msg:'" + ex.Message + "'}"); } }
public string GenerateBitmap(string src) { try { src = src.Replace("/", "\\"); src = src.Replace("..\\", string.Empty); string[] srcImg = src.Split('\\'); string imgName = srcImg[srcImg.Length - 1]; string[] str = imgName.Split('B'); string noName = ""; if (str.Length < 2) { string[] strName = imgName.Split('M'); noName = strName[0]; } else { noName = str[0]; } string newName = DateTime.Now.ToString("yyyyMMhh"); FileInfo fi = new FileInfo(src); string ext = fi.Extension; string newfileName = "DataUser/UserIcon/" + noName + "SmallerCon.png"; if (src.Contains("ImgAth"))//附件图片使用变量,解决页面图片缓存问题 { newfileName = "DataUser/ImgAth/Temp/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; } //Image.GetThumbnailImageAbort abort = null; Bitmap oldBitmap = new Bitmap(src); HttpContext context = HttpContext.Current; Cutter cut = new Cutter( double.Parse(context.Request["zoom"]), -int.Parse(context.Request["x"]), -int.Parse(context.Request["y"]), int.Parse(context.Request["width"]), int.Parse(context.Request["height"]), oldBitmap.Width, oldBitmap.Height); Bitmap bmp = Helper.GenerateBitmap(oldBitmap, cut); oldBitmap.Dispose(); string temp = Path.Combine(context.Server.MapPath("~/"), newfileName); bmp.Save(temp, ImageFormat.Png); bmp.Dispose(); System.Drawing.Image myImage = System.Drawing.Image.FromFile(temp); int phWidth = myImage.Width; int phHeight = myImage.Height; GetPicThumbnail(temp, context.Server.MapPath("~/") + "/DataUser/UserIcon/" + noName + "Biger.png", 100, 100); GetPicThumbnail(temp, context.Server.MapPath("~/") + "/DataUser/UserIcon/" + noName + ".png", 60, 60); GetPicThumbnail(temp, context.Server.MapPath("~/") + "/DataUser/UserIcon/" + noName + "Smaller.png", 40, 40); myImage.Dispose(); System.Drawing.Image myImages = System.Drawing.Image.FromFile(src); int w = myImages.Width; int h = myImages.Height; myImages.Dispose(); GetPicThumbnail(src, context.Server.MapPath("~/") + "/DataUser/UserIcon/" + noName + "BigerCon.png", h, w); if (File.Exists(src)) { File.SetAttributes(src, FileAttributes.Normal); File.Delete(src); } return("{msg:'success',src:'../../" + newfileName + "'}"); } catch (Exception ex) { return("{msg:'" + ex.Message + "'}"); } }