public void CropAndResizeImage(Image image, string outPutFilePath, string outPuthFileName, int?width = null, int?height = null, bool pngFormat = false) { try { if (!width.HasValue) { width = new int?(image.Width); } if (!height.HasValue) { height = new int?(image.Height); } KalikoImage kalikoImage = new KalikoImage(image); KalikoImage kalikoImage1 = kalikoImage.Scale(new FitScaling(width.Value, height.Value)); if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))); } string str = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName))); if (!pngFormat) { kalikoImage1.SaveJpg(str, (long)99); } else { kalikoImage1.SavePng(str); } kalikoImage1.Dispose(); kalikoImage.Dispose(); } catch (Exception exception) { throw new Exception(exception.Message); } }
public void CropAndResizeImage(HttpPostedFileBase imageFile, string outPutFilePath, string outPuthFileName, int width, int height, bool pngFormat = false) { try { var image = Image.FromStream(imageFile.InputStream); var kalikoImage = new KalikoImage(image); var imgCrop = kalikoImage.Scale(new FitScaling(width, height)); if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))); } var path = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName))); if (!pngFormat) { imgCrop.SaveJpg(path, 99, true); } else { imgCrop.SavePng(path); } kalikoImage.Dispose(); } catch (Exception ex) { throw new Exception(ex.Message); } }
public void CropAndResizeImage(HttpPostedFileBase imageFile, string outPutFilePath, string outPuthFileName, int width, int height, bool pngFormat = false) { try { Image image = Image.FromStream(imageFile.InputStream); //if (!width.HasValue) //{ // width = image.Width; //} //if (!height.HasValue) //{ // height = new int?(image.Height); //} KalikoImage kalikoImage = new KalikoImage(image); kalikoImage.Resize(width, height); //KalikoImage kalikoImage1 = kalikoImage.Scale(new CropScaling(width, height)); //KalikoImage kalikoImage1 = kalikoImage.Scale(new FitScaling(width.Value, height.Value)); if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))); } string str = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName))); if (!pngFormat) { kalikoImage.SaveJpg(str, 99); } else { kalikoImage.SavePng(str); } kalikoImage.Dispose(); //kalikoImage.Dispose(); } catch (Exception exception) { throw new Exception(exception.Message); } }
/// <summary> /// 切割图片 /// </summary> /// <param name="pageCount">切成几张</param> /// <returns></returns> private List <string> kalikoImage(int pageCount) { List <string> imageList = new List <string>(); for (var n = 0; n < pageCount; n++) { KalikoImage ki = new KalikoImage(SealPath); var w = ki.Width / pageCount; // 计算每张图片的宽度 ki.Crop(w * n, 0, w, ki.Height); //string path = System.IO.Path.Combine(config.Value.Substring(0, config.Value.LastIndexOf(".")), string.Format("{0}{1}.png", count, n)); //if (!System.IO.Directory.Exists(config.Value.Substring(0, config.Value.LastIndexOf(".")))) //{ // System.IO.Directory.CreateDirectory(config.Value.Substring(0, config.Value.LastIndexOf(".")));//不存在就创建目录 //} string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp", string.Format("{0}{1}.png", pageCount, n)); ki.SavePng(path); ki.Dispose(); imageList.Add(path); } return(imageList); }
/// <summary> /// 缩略图的截图模式是:当原图宽高比按照缩略图时缩放 /// </summary> /// <param name="originalImagePath"></param> /// <param name="thumbnailPath"></param> /// <param name="extName"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="scale">Crop:取中间部分;Fit:按比例缩放,自动调整尺寸;Pad:按比例缩放,保持尺寸,不足部分填白</param> /// <param name="position">水印位置</param> /// <param name="waterMarkingPath">水印图片路劲</param> public void MakeThumbnail(string originalImagePath, string thumbnailPath, string extName, int width, int height, ThumbnailMethod scale = ThumbnailMethod.Fit, WaterMarkingPosition?position = null, string waterMarkingPath = "") { if (!File.Exists(originalImagePath)) { return; } var image = new KalikoImage(originalImagePath) { BackgroundColor = Color.White }; var format = GetImageFormat(extName); ScalingBase scalingBase = null; switch (scale) { case ThumbnailMethod.Crop: scalingBase = new CropScaling(width, height); break; case ThumbnailMethod.Fit: scalingBase = new FitScaling(width, height); break; case ThumbnailMethod.Pad: scalingBase = new PadScaling(width, height); break; default: scalingBase = new CropScaling(width, height); break; } var imageThumb = image.Scale(scalingBase); AddWaterMarking(position, waterMarkingPath, imageThumb); imageThumb.SaveImage(thumbnailPath, format); image.Dispose(); imageThumb.Dispose(); }
public void CropAndResizeImage(string inPutFilePath, string outPutFilePath, string outPuthFileName, int width, int height, bool pngFormat = false) { try { var image = Image.FromFile(HttpContext.Current.Server.MapPath(string.Concat("~/", inPutFilePath))); //if (!width.HasValue) //{ // width = new int?(image.Width); //} //if (!height.HasValue) //{ // height = new int?(image.Height); //} var kalikoImage = new KalikoImage(image); var kalikoImage1 = kalikoImage.Scale(new PadScaling(width, height, Color.Transparent)); //KalikoImage kalikoImage1 = kalikoImage.Scale(new FitScaling(width.Value, height.Value)); if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))); } var str = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName))); if (!pngFormat) { kalikoImage1.SaveJpg(str, 99); } else { kalikoImage1.SavePng(str); } kalikoImage1.Dispose(); kalikoImage.Dispose(); } catch (Exception exception) { throw new Exception(exception.Message); } }