/// <summary> /// 将IP地址转换为长整型数字 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static long ToIpAddrValue(string obj) { long result = -1; string[] arrnum = obj.Split('.'); if (arrnum.Length != 4) { return(result); } int[] ints = { 0, 0, 0, 0 }; for (int i = 0; i < 4; i++) { ints[i] = ConvertObject.ToInt32(arrnum[i], -1); if (ints[i] < 0 && ints[i] > 255) { return(result); } } result = (long)ints[0] * 256 * 256 * 256 + (long)ints[1] * 256 * 256 + (long)ints[2] * 256 + (long)ints[3]; return(result); }
/// <summary> /// 获取季度的第一天 /// </summary> /// <param name="month">2018Q1</param> /// <returns></returns> public static DateTime GetQuarterFirstday(string quarter) { if (quarter == "") { quarter = GetYearQuarterString(); } int yearIndex = ConvertObject.ToInt32(quarter.Substring(0, 4)); int quarterIndex = ConvertObject.ToInt32(quarter.Substring(5, 1)); switch (quarterIndex) { case 4: return(new DateTime(yearIndex, 10, 1)); case 3: return(new DateTime(yearIndex, 7, 1)); case 2: return(new DateTime(yearIndex, 4, 1)); default: return(new DateTime(yearIndex, 1, 1)); } }
/// <summary> /// 获取月份的第一天 /// </summary> /// <param name="month">yyyyMM</param> /// <returns></returns> public static DateTime GetMonthFirstday(string month) { try { if (month.Length == 6) { int yearIndex = ConvertObject.ToInt32(month.Substring(0, 4)); int monthIndex = ConvertObject.ToInt32(month.Substring(4, 2)); return(new DateTime(yearIndex, monthIndex, 1)); } else { throw new Exception(); } } catch { return(GetMonthFirstday()); } }
/// <summary> /// 获取后一个月 /// </summary> /// <param name="month">当前月</param> /// <param name="nexts">后面的月数(1为后一个月,2为后两个月,-1为前一个月)</param> /// <returns></returns> public static string GetNextMonth(string month, int nexts) { try { if (month.Length == 6) { int yearIndex = ConvertObject.ToInt32(month.Substring(0, 4)); int monthIndex = ConvertObject.ToInt32(month.Substring(4, 2)); return(GetMonthString(new DateTime(yearIndex, monthIndex, 1).AddMonths(nexts))); } else { throw new Exception(); } } catch { DateTime today = DateTime.Now; return(GetMonthString(new DateTime(today.Year, today.Month, 1))); } }
/// <summary> /// 获取当前季度的月份 /// </summary> /// <param name="yearQuarter"></param> /// <returns></returns> public static string[] GetQuarterMonths(string yearQuarter) { int yearIndex = ConvertObject.ToInt32(yearQuarter.Substring(0, 4)); int quarterIndex = ConvertObject.ToInt32(yearQuarter.Substring(5, 1)); switch (quarterIndex) { case 1: return(new String[] { yearIndex + "01", yearIndex + "02", yearIndex + "03" }); case 2: return(new String[] { yearIndex + "04", yearIndex + "05", yearIndex + "06" }); case 3: return(new String[] { yearIndex + "07", yearIndex + "08", yearIndex + "09" }); case 4: return(new String[] { yearIndex + "10", yearIndex + "11", yearIndex + "12" }); } return(new String[] { }); }
public static int GetDictionaryIntValue(Dictionary <string, object> json, string key, int defaultValue) { return(ConvertObject.ToInt32(GetDictionaryStringValue(json, key), defaultValue)); }
/// <summary> /// 转换图片格式 /// </summary> /// <param name="originalStream">源图片流</param> /// <param name="formatWidth">格式化宽度</param> /// <param name="formatHeight">格式化高度</param> /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> /// <param name="newFormat">图片输出格式</param> /// <returns>新图片流</returns> public static Stream FormatPicture(Stream originalStream, int formatWidth, int formatHeight, bool cut, PictureFormat newFormat) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalStream); if (formatWidth > 0 && formatHeight == 0) { formatHeight = ConvertObject.ToInt32(Math.Round(formatWidth * (float)originalImage.Height / originalImage.Width)); } if (formatHeight > 0 && formatWidth == 0) { formatWidth = ConvertObject.ToInt32(Math.Round(formatHeight * (float)originalImage.Width / originalImage.Height)); } int towidth = formatWidth; int toheight = formatHeight; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (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 * formatHeight / towidth; x = 0; y = (originalImage.Height - oh) / 2; } } else { //默认指定高度宽度,失真缩放 //指定宽度缩放 if (formatHeight <= 0) { formatWidth = originalImage.Width * formatHeight / originalImage.Height; } //指定高度缩放 else if (formatWidth <= 0) { formatHeight = originalImage.Height * formatWidth / originalImage.Width; } } //新建一个bmp图片 System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight); //新建一个画板 System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); //设置高质量插值法 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphics.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage( originalImage, new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel ); originalImage.Dispose(); System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(newFormat); MemoryStream newStream = new MemoryStream(); try { //以指定格式保存图片 string outputFile = Path.GetTempPath() + Guid.NewGuid().ToString();// +".jpg"; image.Save(outputFile, imageFormat); FileStream fs = File.Open(outputFile, FileMode.Open); newStream = new MemoryStream(ConvertStream.ToBuffer(fs)); fs.Close(); File.Delete(outputFile); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); } return(newStream); }
/// <summary> /// 格式化图片 /// </summary> /// <param name="fileBinary">源图片二进制</param> /// <param name="formatWidth">格式化宽度</param> /// <param name="formatHeight">格式化高度</param> /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> /// <param name="picFormat">图片输出格式</param> /// <returns>新图片二进制</returns> public static Byte[] FormatPicture(Byte[] fileBinary, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(new MemoryStream(fileBinary)); if (formatWidth > 0 && formatHeight == 0) { formatHeight = ConvertObject.ToInt32(Math.Round(formatWidth * (float)originalImage.Height / originalImage.Width)); } if (formatHeight > 0 && formatWidth == 0) { formatWidth = ConvertObject.ToInt32(Math.Round(formatHeight * (float)originalImage.Width / originalImage.Height)); } int towidth = formatWidth; int toheight = formatHeight; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (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 * formatHeight / towidth; x = 0; y = (originalImage.Height - oh) / 2; } } else { //默认指定高度宽度,失真缩放 //指定宽度缩放 if (formatHeight <= 0) { formatWidth = originalImage.Width * formatHeight / originalImage.Height; } //指定高度缩放 else if (formatWidth <= 0) { formatHeight = originalImage.Height * formatWidth / originalImage.Width; } } //新建一个bmp图片 System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight); //新建一个画板 System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); //设置高质量插值法 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphics.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage( originalImage, new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel ); originalImage.Dispose(); System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(picFormat); //以指定格式保存图片 try { MemoryStream stream = new MemoryStream(); image.Save(stream, imageFormat); return(stream.GetBuffer()); } catch (Exception ex) { throw ex; } finally { //originalImage.Dispose(); image.Dispose(); graphics.Dispose(); } }