/// <summary> /// 通用图片上传类 /// </summary> /// <param name="postedFile">HttpPostedFile控件</param> /// <param name="savePath">保存路径【sys.config配置路径】</param> /// <param name="oImgWidth">图片宽度</param> /// <param name="oImgHeight">图片高度</param> /// <param name="cMode">剪切类型</param> /// <param name="fileName"></param> /// <returns>【0-系统配置错误,1-上传图片成功,2-格式错误,3-超过文件上传大小】</returns> public static int FileSaveAs(HttpPostedFile postedFile, string savePath, int oImgWidth, int oImgHeight, CutMode cMode, ref string fileName) { try { //获取上传文件的扩展名 string sEx = Path.GetExtension(postedFile.FileName); if (!CheckValidExt(AllowFormat, sEx)) { return(2); //格式错误 } //获取上传文件的大小 int postFileSize = postedFile.ContentLength / 1024; if (postFileSize > AllowSize) { return(3); //超过文件上传大小 } if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } //重命名名称 string newfileName = DateTime.Now.Year + DateTime.Now.Month.ToString() + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond.ToString("000"); string fName = "s" + newfileName + sEx; string fullPath = Path.Combine(savePath, fName); postedFile.SaveAs(fullPath); //重命名名称 string sNewfileName = DateTime.Now.Year + DateTime.Now.Month.ToString() + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond.ToString("000"); string sFName = sNewfileName + sEx; fileName = sFName; string sFullPath = Path.Combine(savePath, sFName); CreateSmallPhoto(fullPath, oImgWidth, oImgHeight, sFullPath, PicWater, WordWater, cMode); if (File.Exists(fullPath)) { File.Delete(fullPath); } //压缩 if (postFileSize > 100) { CompressPhoto(sFullPath, 100); } return(1); } catch { return(-1); } }
public void CutPaper(CutMode mode) { Int16 iMode = (Int16)mode; string sCommand = AsciiCode.GS + (char)'V' + (char)iMode; if (this.PRINTER_TYPE == CONN_TYPE.serial) { this.PRINTER_PORT.Write(sCommand); } }
/// <summary> /// Load the data from a Tab delimited string of data. Each column is separated by a Tab and each row by a LineFeed character. /// </summary> public void LoadData(string data) { mSourceGrid = null; mCutMode = CutMode.None; mStartDragPosition = new Position(0, 0); StringToData(data, out mSourceRange, out mSourceValues); mClipboardDataObject = new System.Windows.Forms.DataObject(); mClipboardDataObject.SetData(RANGEDATA_FORMAT, this); mClipboardDataObject.SetData(typeof(string), DataToString(mSourceValues, mSourceRange)); }
/// <summary> /// Load the specified range data into a string array. This method use the cell editor to get the value. /// </summary> /// <param name="sourceGrid"></param> /// <param name="sourceRange"></param> /// <param name="startDragPosition">Starting drag position. Used only for calculating drop destination range.</param> /// <param name="cutMode">Cut mode. Can be used to remove the data from the source when pasting it to the destination or immediately.</param> public void LoadData(GridVirtual sourceGrid, Range sourceRange, Position startDragPosition, CutMode cutMode) { mSourceGrid = sourceGrid; mCutMode = cutMode; mStartDragPosition = startDragPosition; mSourceRange = sourceRange; mSourceValues = new string[mSourceRange.RowsCount, mSourceRange.ColumnsCount]; int arrayRow = 0; for (int r = mSourceRange.Start.Row; r <= mSourceRange.End.Row; r++, arrayRow++) { int arrayCol = 0; for (int c = mSourceRange.Start.Column; c <= mSourceRange.End.Column; c++, arrayCol++) { Position posCell = new Position(r, c); Cells.ICellVirtual cell = sourceGrid.GetCell(posCell); CellContext cellContext = new CellContext(sourceGrid, posCell, cell); if (cell != null && cell.Editor != null) { mSourceValues[arrayRow, arrayCol] = cell.Editor.ValueToString(cell.Model.ValueModel.GetValue(cellContext)); } else if (cell != null) { mSourceValues[arrayRow, arrayCol] = cellContext.GetDisplayText(); } } } //Cut Data if (CutMode == CutMode.CutImmediately && sourceGrid != null) { for (int sr = sourceRange.Start.Row; sr <= sourceRange.End.Row; sr++) { for (int sc = sourceRange.Start.Column; sc <= sourceRange.End.Column; sc++) { Position pos = new Position(sr, sc); Cells.ICellVirtual cell = sourceGrid.GetCell(sr, sc); CellContext cellContext = new CellContext(sourceGrid, pos, cell); if (cell.Editor != null) { cell.Editor.ClearCell(cellContext); } } } } mClipboardDataObject = new System.Windows.Forms.DataObject(); mClipboardDataObject.SetData(RANGEDATA_FORMAT, this); mClipboardDataObject.SetData(typeof(string), DataToString(mSourceValues, mSourceRange)); }
/// <summary> /// Load the specified range data into a string array. This method use the cell editor to get the value. /// </summary> /// <param name="sourceGrid"></param> /// <param name="sourceRange"></param> /// <param name="cutMode">Cut mode. Can be used to remove the data from the source when pasting it to the destination or immediately.</param> public static RangeData LoadData(GridVirtual sourceGrid, Range sourceRange, CutMode cutMode) { RangeData data = new RangeData(sourceGrid); //mCutMode = cutMode; data.mSourceRange = sourceRange; data.mSourceValues = new object[sourceRange.RowsCount, GetVisibleColumnCount(sourceGrid, sourceRange)]; int arrayRow = 0; for (int r = sourceRange.Start.Row; r <= sourceRange.End.Row; r++, arrayRow++) { int arrayCol = 0; for (int c = sourceRange.Start.Column; c <= sourceRange.End.Column; c++) { if (sourceGrid.Columns.IsColumnVisible(c) == false) { continue; } Position posCell = new Position(r, c); Cells.ICellVirtual cell = sourceGrid.GetCell(posCell); CellContext cellContext = new CellContext(sourceGrid, posCell, cell); /*if (cell != null && cell.Editor != null && cell.Editor.IsStringConversionSupported()) * data.mSourceValues[arrayRow, arrayCol] = cell.Editor.ValueToString( cell.Model.ValueModel.GetValue(cellContext) ); * else if (cell != null) * data.mSourceValues[arrayRow, arrayCol] = cellContext.DisplayText;*/ if (cell != null) { data.mSourceValues[arrayRow, arrayCol] = cellContext.Value; } arrayCol++; } } //Cut Data if (cutMode == CutMode.CutImmediately && sourceGrid != null) { sourceGrid.ClearValues(new RangeRegion(sourceRange)); } data.mClipboardDataObject = new System.Windows.Forms.DataObject(); data.mClipboardDataObject.SetData(RANGEDATA_FORMAT, data); string[,] values = DataToStringArray(sourceGrid, data.mSourceRange); data.mClipboardDataObject.SetData(typeof(string), StringArrayToString(values)); return(data); }
public SelectionDrag(CutMode cutMode) { mCutMode = cutMode; }
/// <summary> /// 裁剪图片 /// </summary> /// <param name="originalImage"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="mode"></param> /// <param name="autoFill">是否填充到宽高</param> /// <param name="HightMode">是否高质量</param> /// <returns></returns> public static Image CutImg(Image originalImage, int width, int height, CutMode mode, bool autoFill = true, bool HightMode = true) { //将要达到的宽度 int towidth = width; //将要达到的高度 int toheight = height; int x = 0; int y = 0; //原始宽度 int ow = originalImage.Width; //原始高度 int oh = originalImage.Height; switch (mode) { case CutMode.WIDTH_HEIGHT: //指定高宽缩放(可能变形) break; case CutMode.WIDTH: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case CutMode.HEIGHT: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case CutMode.CUT: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; case CutMode.AUTO: //自动,在宽高内 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { toheight = originalImage.Height * width / originalImage.Width; } else { towidth = originalImage.Width * height / originalImage.Height; } break; default: break; } if (originalImage.Width < width) { //towidth = originalImage.Width; ow = originalImage.Width; } if (originalImage.Height < height) { //toheight = originalImage.Height; oh = originalImage.Height; } //达到的宽高大于原始图宽高 if (towidth > ow && toheight > oh) { towidth = ow; toheight = oh; } if (!autoFill) { width = towidth; height = toheight; } Image bitmap = new System.Drawing.Bitmap(width, height); Graphics g = System.Drawing.Graphics.FromImage(bitmap); if (HightMode) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; } g.Clear(Color.White); int a = (height - toheight) / 2; int b = (width - towidth) / 2; g.DrawImage(originalImage, new Rectangle(b, a, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); // System.IO.MemoryStream ms = new System.IO.MemoryStream(); //bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //originalImage.Dispose(); //bitmap.Dispose(); g.Dispose(); return(bitmap); }
/// <summary> /// Load the specified range data into a string array. This method use the cell editor to get the value. /// </summary> /// <param name="sourceGrid"></param> /// <param name="sourceRange"></param> /// <param name="startDragPosition">Starting drag position. Used only for calculating drop destination range.</param> /// <param name="cutMode">Cut mode. Can be used to remove the data from the source when pasting it to the destination or immediately.</param> public void LoadData(GridVirtual sourceGrid, Range sourceRange, Position startDragPosition, CutMode cutMode) { mSourceGrid = sourceGrid; mCutMode = cutMode; mStartDragPosition = startDragPosition; mSourceRange = sourceRange; mSourceValues = new string[mSourceRange.RowsCount, mSourceRange.ColumnsCount]; int arrayRow = 0; for (int r = mSourceRange.Start.Row; r <= mSourceRange.End.Row; r++, arrayRow++) { int arrayCol = 0; for (int c = mSourceRange.Start.Column; c <= mSourceRange.End.Column; c++, arrayCol++) { Position posCell = new Position(r, c); Cells.ICellVirtual cell = sourceGrid.GetCell(posCell); CellContext cellContext = new CellContext(sourceGrid, posCell, cell); if (cell != null && cell.Editor != null && cell.Editor.IsStringConversionSupported()) mSourceValues[arrayRow, arrayCol] = cell.Editor.ValueToString( cell.Model.ValueModel.GetValue(cellContext) ); else if (cell != null) mSourceValues[arrayRow, arrayCol] = cellContext.DisplayText; } } //Cut Data if (CutMode == CutMode.CutImmediately && sourceGrid != null) { sourceGrid.ClearValues(new RangeRegion(sourceRange)); //for (int sr = sourceRange.Start.Row; sr <= sourceRange.End.Row; sr++) // for (int sc = sourceRange.Start.Column; sc <= sourceRange.End.Column; sc++) // { // Position pos = new Position(sr, sc); // Cells.ICellVirtual cell = sourceGrid.GetCell(sr, sc); // CellContext cellContext = new CellContext(sourceGrid, pos, cell); // if (cell.Editor != null) // cell.Editor.ClearCell(cellContext); // } } mClipboardDataObject = new System.Windows.Forms.DataObject(); mClipboardDataObject.SetData(RANGEDATA_FORMAT, this); mClipboardDataObject.SetData(typeof(string), DataToString(mSourceValues, mSourceRange)); }
/// <summary> /// 生成定制宽高的图片 /// </summary> /// <param name="originalImagePath">原始图片路径</param> /// <param name="thumbnailPath">定制图片路径</param> /// <param name="width">定制宽度</param> /// <param name="height">定制高度</param> /// <param name="mode">图片裁切模式</param> /// <param name="HightMode">图片质量</param> public static void MakeCutImg(string originalImagePath, string thumbnailPath, int width, int height, CutMode mode, bool HightMode) { using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath)) { //图片裁切 using (System.Drawing.Image customerImage = CutImg(originalImage, width, height, mode, HightMode)) { //EventLog.WriteLog(customerImage.Width.ToString()); //图片保存 customerImage.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } } }
public EpsonCommander Cut(CutMode mode) { _buffer.Append(GS + "V" + ((char)(int)mode)); return(this); }
/// <summary> /// 裁剪图片 /// </summary> /// <param name="PostedFile">HttpPostedFile控件</param> /// <param name="SaveFolder">保存路径【sys.config配置路径】</param> /// <param name="oImgWidth">图片宽度</param> /// <param name="oImgHeight">图片高度</param> /// <param name="cMode">剪切类型</param> /// <returns>返回上传信息</returns> public ResponseMessage FileCutSaveAs(System.Web.HttpPostedFile PostedFile, string SaveFolder, int oImgWidth, int oImgHeight, CutMode cMode) { ResponseMessage rm = new ResponseMessage(); try { //获取上传文件的扩展名 string sEx = System.IO.Path.GetExtension(PostedFile.FileName); if (!CheckValidExt(SetAllowFormat, sEx)) { TryError(rm, 2); return(rm); } //获取上传文件的大小 double PostFileSize = PostedFile.ContentLength / 1024.0 / 1024.0; if (PostFileSize > SetAllowSize) { TryError(rm, 3); return(rm); //超过文件上传大小 } if (!System.IO.Directory.Exists(SaveFolder)) { System.IO.Directory.CreateDirectory(SaveFolder); } //重命名名称 string NewfileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString("000"); string fName = "s" + NewfileName + sEx; string fullPath = Path.Combine(SaveFolder, fName); PostedFile.SaveAs(fullPath); //重命名名称 string sNewfileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString("000"); string sFName = sNewfileName + sEx; rm.IsError = false; rm.FileName = sFName; string sFullPath = Path.Combine(SaveFolder, sFName); rm.filePath = sFullPath; rm.WebPath = "/" + sFullPath.Replace(HttpContext.Current.Server.MapPath("~/"), "").Replace("\\", "/"); CreateSmallPhoto(fullPath, oImgWidth, oImgHeight, sFullPath, SetPicWater, SetWordWater, cMode); if (File.Exists(fullPath)) { File.Delete(fullPath); } //压缩 if (PostFileSize > 100) { CompressPhoto(sFullPath, 100); } } catch (Exception ex) { TryError(rm, ex.Message); } return(rm); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="orgFileStream">源图路径(物理路径)</param> /// <param name="targetFileName">缩略图路径(物理路径)</param> /// <param name="tarWidth">缩略图宽度</param> /// <param name="tarHeight">缩略图高度</param> /// <param name="rWidth">源图中截获一个矩形区域的宽度</param> /// <param name="rHeight">源图中截获一个矩形区域的宽度</param> protected void MakeThumbnail(Stream orgFileStream, string targetFileName, int tarWidth, int tarHeight, string markerfile, string character, WatermarkPosition wp, int rWidth, int rHeight) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(orgFileStream); if (tarWidth == 0 && tarHeight == 0) { originalImage.Save(targetFileName); return; //tarWidth = originalImage.Width; //tarHeight = originalImage.Height; } else if (tarWidth < 0 || tarHeight < 0) { tarWidth = originalImage.Width; tarHeight = originalImage.Height; } //目标大小 int towidth = tarWidth; int toheight = tarHeight; int x = 0; int y = 0; //当需要填充白边时,两个方向的座标的起始位置 int posx = 0; int posy = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (rWidth > 0 && rHeight > 0) { ow = rWidth; oh = rHeight; } if (toheight == 0 || towidth == 0) cMode = CutMode.Scale; //如果有一个为0,表示仅限一边,其它按比例压缩 switch (cMode) { case CutMode.Scale://按比例自适应宽高(不变形) if (towidth == 0) { double rateo = (double)oh / (double)toheight; towidth = (int)(ow * rateo); } else if (toheight == 0) { double rateh = (double)ow / (double)towidth; toheight = (int)(oh * rateh); } else { if (_IsFillRect)//如果要将图片填充满,最后形成的图片是一张大小与要求一样的图 { double rateo = (double)ow / (double)oh; double raten = (double)towidth / (double)toheight; if (rateo > raten) //如果原图比目标宽,则Y座标需要计算 { posy = (int)(toheight - (double)towidth / (double)ow * oh) / 2;//Y向居中起始位置 } else { posx = (int)(towidth - (double)toheight / (double)oh * ow) / 2;//X向居中起始位置 } } else //最后形成的图片可能比目标图片小,但宽和高有一个与要求的大小一致 { if ((double)ow / (double)oh > (double)towidth / (double)toheight) { toheight = (int)(oh * tarWidth / (double)ow); } else { towidth = (int)(ow * tarHeight / (double)oh); } } } break; case CutMode.Pull://指定高宽缩放(可能变形) break; case CutMode.Cut://指定高宽裁减(不变形) if ((double)ow / (double)oh > (double)towidth / (double)toheight) { //oh = originalImage.Height; int ow1 = ow; ow = (int)(ow * towidth / (double)toheight); y = 0; x = (ow1 - ow) / 2; } else { //ow = originalImage.Width; int oh1 = oh; oh = ow * tarHeight / towidth; x = 0; y = (oh1 - oh) / 2; } break; //从指定位置剪切固定大小的图 case CutMode.CutSpec: x = _PosX; y = _PosY; if (rWidth <= 0 && rHeight <= 0) { ow = towidth; oh = toheight; } break; default: break; } //ImageCodecInfo ici = Getco long lngDefinition = 90; EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lngDefinition); //新建一个bmp图片 System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(towidth, toheight, PixelFormat.Format32bppArgb); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; //System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1]; //System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1); //eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); //清空画布并以白色背景色填充 g.Clear(System.Drawing.Color.White); //在指定位置并且按指定大小绘制原图片的指定部分 if (_IsFillRect) { g.DrawImage(originalImage, new System.Drawing.Rectangle(posx, posy, towidth - posx * 2, toheight - posy * 2), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); } else { g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); } if (wp != WatermarkPosition.None) { if (WaterType.Text == wType) { if (character == null) character = "NULL"; AddWatermarkText(ref g, towidth, toheight, character, wp); } else if (markerfile != null) { Image wimg = Image.FromFile(markerfile); AddWatermarkImage(ref g, towidth, toheight, wimg, wp); } } try { SaveImage(bitmap, 100L, targetFileName); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
/// <summary> /// 构造函数 初始化生成图的宽高, 水印类型, 裁剪模式 /// </summary> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="type">水印类型</param> /// <param name="mode">裁剪模式</param> public mImage(int width, int height, int type, int mode) { widthImage = width; heightImage = height; this.cMode = (CutMode)mode; this.wType = (WaterType)type; }
/// <summary> /// Load the specified range data into a string array. This method use the cell editor to get the value. /// </summary> /// <param name="sourceGrid"></param> /// <param name="sourceRange"></param> /// <param name="cutMode">Cut mode. Can be used to remove the data from the source when pasting it to the destination or immediately.</param> public static RangeData LoadData(GridVirtual sourceGrid, Range sourceRange, CutMode cutMode) { RangeData data = new RangeData(sourceGrid); //mCutMode = cutMode; data.mSourceRange = sourceRange; data.mSourceValues = new object[sourceRange.RowsCount, GetVisibleColumnCount(sourceGrid, sourceRange)]; int arrayRow = 0; for (int r = sourceRange.Start.Row; r <= sourceRange.End.Row; r++, arrayRow++) { int arrayCol = 0; for (int c = sourceRange.Start.Column; c <= sourceRange.End.Column; c++) { if (sourceGrid.Columns.IsColumnVisible(c) == false) { continue; } Position posCell = new Position(r, c); Cells.ICellVirtual cell = sourceGrid.GetCell(posCell); CellContext cellContext = new CellContext(sourceGrid, posCell, cell); /*if (cell != null && cell.Editor != null && cell.Editor.IsStringConversionSupported()) * data.mSourceValues[arrayRow, arrayCol] = cell.Editor.ValueToString( cell.Model.ValueModel.GetValue(cellContext) ); * else if (cell != null) * data.mSourceValues[arrayRow, arrayCol] = cellContext.DisplayText;*/ //[email protected] : LoadData method is changed to support cut and copy activities. if (cell != null) { if (cutMode == CutMode.CutImmediately) { if ((cell.ClipboardModes & ClipboardMode.Cut) == ClipboardMode.Cut) { data.mSourceValues[arrayRow, arrayCol] = cellContext.Value; } } else if (cutMode == CutMode.None) { if ((cell.ClipboardModes & ClipboardMode.Copy) == ClipboardMode.Copy) { data.mSourceValues[arrayRow, arrayCol] = cellContext.Value; } } } arrayCol++; } } data.mClipboardDataObject = new System.Windows.Forms.DataObject(); data.mClipboardDataObject.SetData(RANGEDATA_FORMAT, data); string[,] values = DataToStringArray(sourceGrid, data.mSourceRange); data.mClipboardDataObject.SetData(typeof(string), StringArrayToString(values)); //Cut Data //[email protected] : Cut operation is moved below the copy operation to retain the data if (cutMode == CutMode.CutImmediately && sourceGrid != null) { sourceGrid.ClearValues(new RangeRegion(sourceRange), ClipboardMode.Cut); } return(data); }
/// <summary> /// 上传的图片按比例裁切 /// </summary> /// <param name="originalImage">原始图片</param> /// <param name="width">将要达到的宽度</param> /// <param name="height">将要达到的高度</param> /// <param name="mode">裁切模式</param> /// <param name="HightMode">图片质量</param> /// <returns></returns> public static System.Drawing.Image CutImg(System.Drawing.Image originalImage, int width, int height, CutMode mode, bool HightMode) { //将要达到的宽度 int towidth = width; //将要达到的高度 int toheight = height; int x = 0; int y = 0; //原始宽度 int ow = originalImage.Width; //原始高度 int oh = originalImage.Height; switch (mode) { case CutMode.WIDTH_HEIGHT: //指定高宽缩放(可能变形) break; case CutMode.WIDTH: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case CutMode.HEIGHT: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case CutMode.CUT: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; case CutMode.AUTO: //自动,在宽高内 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { toheight = originalImage.Height * width / originalImage.Width; } else { towidth = originalImage.Width * height / originalImage.Height; } break; default: break; } //if (originalImage.Width < width) //{ // //towidth = originalImage.Width; // ow = originalImage.Width; //} //if (originalImage.Height < height) //{ // //toheight = originalImage.Height; // oh = originalImage.Height; //} ////达到的宽高大于原始图宽高 //if (towidth > ow && toheight > oh) //{ // towidth = ow; // toheight = oh; //} System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); using (Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { if (HightMode) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; } g.Clear(Color.White); //int a = (height - toheight) / 2; //int b = (width - towidth) / 2; g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); } return(bitmap); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="fileName">源图路径(绝对路径)</param> /// <param name="newFileName">缩略图路径(绝对路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnailImage(string fileName, string newFileName, int width, int height, CutMode mode) { Image originalImage = Image.FromFile(fileName); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case CutMode.HW: //指定高宽缩放(补白) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } else { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } break; case CutMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case CutMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case CutMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 Bitmap b = new Bitmap(towidth, toheight); try { //新建一个画板 Graphics g = Graphics.FromImage(b); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.White); //g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower())); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); b.Dispose(); } }
/// <summary> /// Load the specified range data into a string array. This method use the cell editor to get the value. /// </summary> /// <param name="sourceGrid"></param> /// <param name="sourceRange"></param> /// <param name="cutMode">Cut mode. Can be used to remove the data from the source when pasting it to the destination or immediately.</param> public static RangeData LoadData(GridVirtual sourceGrid, Range sourceRange, CutMode cutMode) { RangeData data = new RangeData(sourceGrid); //mCutMode = cutMode; data.mSourceRange= sourceRange; data.mSourceValues = new object[sourceRange.RowsCount, GetVisibleColumnCount(sourceGrid, sourceRange)]; int arrayRow = 0; for (int r = sourceRange.Start.Row; r <= sourceRange.End.Row; r++, arrayRow++) { int arrayCol = 0; for (int c = sourceRange.Start.Column; c <= sourceRange.End.Column; c++) { if (sourceGrid.Columns.IsColumnVisible(c) == false) continue; Position posCell = new Position(r, c); Cells.ICellVirtual cell = sourceGrid.GetCell(posCell); CellContext cellContext = new CellContext(sourceGrid, posCell, cell); /*if (cell != null && cell.Editor != null && cell.Editor.IsStringConversionSupported()) data.mSourceValues[arrayRow, arrayCol] = cell.Editor.ValueToString( cell.Model.ValueModel.GetValue(cellContext) ); else if (cell != null) data.mSourceValues[arrayRow, arrayCol] = cellContext.DisplayText;*/ if (cell != null) data.mSourceValues[arrayRow, arrayCol] = cellContext.Value; arrayCol++; } } //Cut Data if (cutMode == CutMode.CutImmediately && sourceGrid != null) { sourceGrid.ClearValues(new RangeRegion(sourceRange)); } data.mClipboardDataObject = new System.Windows.Forms.DataObject(); data.mClipboardDataObject.SetData(RANGEDATA_FORMAT, data); string[,] values = DataToStringArray(sourceGrid, data.mSourceRange); data.mClipboardDataObject.SetData(typeof(string), StringArrayToString(values)); return data; }
public void LoadData(GridVirtual sourceGrid, Range sourceRange, Position startDragPosition, CutMode cutMode) { LoadData(sourceGrid, sourceRange, Position.Empty, cutMode); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="filename">源文件</param> /// <param name="nWidth">缩略图宽度</param> /// <param name="nHeight">缩略图高度</param> /// <param name="destfile">缩略图保存位置</param> /// <param name="png">图片水印</param> /// <param name="text">文本水印</param> /// <param name="cMode">剪切模式</param> public static void CreateSmallPhoto(string filename, int nWidth, int nHeight, string destfile, string png, string text, CutMode cMode) { Image img = Image.FromFile(filename); if (nWidth <= 0) { nWidth = img.Width; } if (nHeight <= 0) { nHeight = img.Height; } int towidth = nWidth; int toheight = nHeight; switch (cMode) { case CutMode.CutWh: //指定高宽缩放(可能变形) break; case CutMode.CutW: //指定宽,高按比例 toheight = img.Height * nWidth / img.Width; break; case CutMode.CutH: //指定高,宽按比例 towidth = img.Width * nHeight / img.Height; break; case CutMode.CutNo: //缩放不剪裁 int maxSize = (nWidth >= nHeight ? nWidth : nHeight); if (img.Width >= img.Height) { towidth = maxSize; toheight = img.Height * maxSize / img.Width; } else { toheight = maxSize; towidth = img.Width * maxSize / img.Height; } break; default: break; } nWidth = towidth; nHeight = toheight; ImageFormat thisFormat = img.RawFormat; var cutSize = new Size(nWidth, nHeight); if (cMode != CutMode.CutNo) { cutSize = CutRegion(nWidth, nHeight, img); } var outBmp = new Bitmap(cutSize.Width, cutSize.Height); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.White); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; int nStartX = (img.Width - cutSize.Width) / 2; int nStartY = (img.Height - cutSize.Height) / 2; //int x1 = (outBmp.Width - nWidth) / 2; //int y1 = (outBmp.Height - nHeight) / 2; if (cMode != CutMode.CutNo) { g.DrawImage(img, new Rectangle(0, 0, nWidth, nHeight), nStartX, nStartY, cutSize.Width, cutSize.Height, GraphicsUnit.Pixel); } else { g.DrawImage(img, new Rectangle(0, 0, nWidth, nHeight), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); } g.Dispose(); // 以下代码为保存图片时,设置压缩质量 var encoderParams = new EncoderParameters(); var quality = new long[1]; quality[0] = 100; var encoderParam = new EncoderParameter(Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。 ImageCodecInfo[] arrayIci = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegIci = arrayIci.FirstOrDefault(t => t.FormatDescription.Equals("JPEG")); if (jpegIci != null) { outBmp.Save(destfile, jpegIci, encoderParams); } else { outBmp.Save(destfile, thisFormat); } img.Dispose(); outBmp.Dispose(); if (!string.IsNullOrEmpty(png)) { AttachPng(png, destfile); } if (!string.IsNullOrEmpty(text)) { AttachText(text, destfile); } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(绝对路径)</param> /// <param name="thumbnailPath">缩略图路径(绝对路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, CutMode mode) { System.Drawing.Image originalImage = null; try { originalImage = System.Drawing.Image.FromFile(originalImagePath); } catch (Exception e) { throw e; } int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case CutMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case CutMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case CutMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //保存缩略图 bitmap.Save(thumbnailPath); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
/// <summary> /// Maximum string length /// </summary> /// <param name="originalText">Original text</param> /// <param name="maxLength">Maximum length</param> /// <param name="cutMode">Cut mode</param> /// <returns>String</returns> public static string MaxStringLength(string originalText, int maxLength, CutMode cutMode) { if (originalText.Length <= maxLength) return originalText; if (cutMode == CutMode.Begin) return originalText.Substring( originalText.Length - maxLength, maxLength); else if (cutMode == CutMode.End) return originalText.Substring(0, maxLength); else // logic: if ( cutMode == CutModes.BothEnds ) return originalText.Substring( (originalText.Length - maxLength) / 2, maxLength); }
/// <summary> /// 根据路径 生成 缩略图 /// </summary> /// <param name="p_strSource">传回的图片路径</param> /// <param name="p_strSave">图片保存的新路径</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="mode"></param> /// <param name="q">质量</param> public static void Thumbnail(string p_strSource, string p_strSave, int width, int height, CutMode mode, long q = 100L) { FileInfo fileInfo = new FileInfo(p_strSave); if (!fileInfo.Directory.Exists) { fileInfo.Directory.Create(); } Image image = Image.FromFile(p_strSource); int num = width; int num2 = height; int x = 0; int y = 0; int num3 = image.Width; int num4 = image.Height; switch (mode) { case CutMode.WH: break; case CutMode.W: num2 = image.Height * width / image.Width; break; case CutMode.H: num = image.Width * height / image.Height; break; case CutMode.Cut: if ((double)image.Width / (double)image.Height < (double)num / (double)num2) { num4 = image.Height; num3 = image.Height * num / num2; y = 0; x = (image.Width - num3) / 2; break; } num3 = image.Width; num4 = image.Width * height / num; x = 0; y = (image.Height - num4) / 2; break; default: if (image.Width > image.Height) { num2 = image.Height * width / image.Width; } else { num = image.Width * height / image.Height; } break; } Image image2 = new Bitmap(num, num2); Graphics graphics = Graphics.FromImage(image2); graphics.InterpolationMode = InterpolationMode.High; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.Clear(Color.White); graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num3, num4), GraphicsUnit.Pixel); try { ImageCodecInfo encoderInfo = IMGUtility.GetEncoderInfo("image/jpeg"); Encoder quality = Encoder.Quality; EncoderParameters encoderParameters = new EncoderParameters(1); EncoderParameter encoderParameter = new EncoderParameter(quality, q); encoderParameters.Param[0] = encoderParameter; image2.Save(p_strSave, encoderInfo, encoderParameters); } catch (Exception ex) { image.Dispose(); image2.Dispose(); graphics.Dispose(); throw ex; } finally { image.Dispose(); image2.Dispose(); graphics.Dispose(); } }