public enum ColorChannelRGBA { R, G, B, A } public enum CompressionLevel : long { /// <summary> /// Specifies the LZW compression scheme. /// </summary> CompressionLZW = 2, /// <summary> /// Specifies the CCITT3 compression scheme. /// </summary> CompressionCCITT3 = 3, /// <summary> /// Specifies the CCITT4 compression scheme. /// </summary> CompressionCCITT4 = 4, /// <summary> /// Specifies the RLE compression scheme. /// </summary> CompressionRle = 5, /// <summary> /// Specifies no compression. /// </summary> CompressionNone = 6, } public enum ImagePosition { } public static class DrawingExtensions { public static ColorMatrix DefaultColorMatrix { //return new ColorMatrix(new float[][] //{ // new float[] { 1f, 0, 0, 0, 0 }, // new float[] { 0, 1f, 0, 0, 0 }, // new float[] { 0, 0, 1f, 0, 0 }, // new float[] { 0, 0, 0, 1f, 0 }, // new float[] { 0, 0, 0, 0, 1f } //}); get { return new ColorMatrix(new float[][] { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); } } #region ColorMatrix public static ColorMatrix Reset(this ColorMatrix obj) { return new ColorMatrix(new float[][] { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); } public static float[][] ToArray(this ColorMatrix obj) { float[][] colorMatrixElements = { new float[] {obj[0,0], obj[0,1], obj[0,2], obj[0,3], obj[0,4]}, new float[] {obj[1,0], obj[1,1], obj[1,2], obj[1,3], obj[1,4]}, new float[] {obj[2,0], obj[2,1], obj[2,2], obj[2,3], obj[2,4]}, new float[] {obj[3,0], obj[3,1], obj[3,2], obj[3,3], obj[3,4]}, new float[] {obj[4,0], obj[4,1], obj[4,2], obj[4,3], obj[4,4]} }; return colorMatrixElements; } public static float[] GetMatrixRow(this ColorMatrix obj, int rowindex) { if (!rowindex.Between(0, 4)) throw new ArgumentOutOfRangeException("rowindex", "rowindex must be between 0 and 4"); float[] rtnflarr = new float[5]; for (int i = 0; i < 5; i++) rtnflarr[i] = obj[rowindex, i]; return rtnflarr; } public static ColorMatrix SetMatrixRow(this ColorMatrix obj, int rowindex, params float[] columns) { if (!rowindex.Between(0, 4)) throw new ArgumentOutOfRangeException("rowindex", "rowindex must be between 0 and 4"); if (!columns.Length.Between(1, 5)) throw new ArgumentOutOfRangeException("row", "row array must an length between 1 and 5"); float[][] tmparr = obj.ToArray(); tmparr[rowindex] = columns; return new ColorMatrix(tmparr); } public static ColorMatrix SetMatrixValue(this ColorMatrix obj, int rowindex, int columnindex, float value) { if (!rowindex.Between(0, 4)) throw new ArgumentOutOfRangeException("rowindex", "rowindex must be between 0 and 4"); if (!columnindex.Between(0, 4)) throw new ArgumentOutOfRangeException("columnindex", "columnindex must an between 0 and 4"); obj[rowindex, columnindex] = value; return obj; } public static ColorMatrix MakeBrightnessMatrix(this ColorMatrix obj, float value) { // return obj.ScaleColor(value, value, value); return new ColorMatrix(new float[][] { new float[] { 1f, 0, 0, 0, 0 }, new float[] { 0, 1f, 0, 0, 0 }, new float[] { 0, 0, 1f, 0, 0 }, new float[] { 0, 0, 0, 1f, 0 }, new float[] { value, value, value, 0, 1f } }); } public static ColorMatrix MakeContrastMatrix(this ColorMatrix obj, float value) { //return obj.Reset().SetMatrixValue(0, 0, value).SetMatrixValue(1, 1, value).SetMatrixValue(2, 2, value); return new ColorMatrix(new float[][] { new float[] { value, 0, 0, 0, 0 }, new float[] { 0, value, 0, 0, 0 }, new float[] { 0, 0, value, 0, 0 }, new float[] { 0, 0, 0, 1f, 0 }, new float[] { 0, 0, 0, 0, 1f } }); } public static ColorMatrix MakeTintMatrix(this ColorMatrix obj, float value) { //return obj.SetMatrixRow(3, ((float)value / 100f), ((float)value / 100f), ((float)value / 100f), 1f, 0f); return new ColorMatrix(new float[][] { new float[] { 1f, 0, 0, 0, 0 }, new float[] { 0, 1f, 0, 0, 0 }, new float[] { 0, 0, 1f, 0, 0 }, new float[] { ((float)value / 100f), ((float)value / 100f), ((float)value / 100f), 1f, 0 }, new float[] { 0, 0, 0, 0, 1f } }); } public static ColorMatrix MakeInvertColorMatrix(this ColorMatrix obj) { //return obj.Reset().SetMatrixValue(0, 0, -1).SetMatrixValue(1, 1, -1).SetMatrixValue(2, 2, -1); return new ColorMatrix(new float[][] { new float[] { -1f, 0, 0, 0, 0 }, new float[] { 0, -1f, 0, 0, 0 }, new float[] { 0, 0, -1f, 0, 0 }, new float[] { 0, 0, 0, 1f, 0 }, new float[] { 0, 0, 0, 0, 1f } }); } public static ColorMatrix MakeColorMatrix(this ColorMatrix obj) { //{ 1f, 0f, 0f }, { 0.5f, 1.0f, 0.5f }, { 0.2f, 0.4f, 0.6f } return new ColorMatrix(new float[][] { new float[] { 1f, 0, 0, 0, 0 }, new float[] { 0.5f, 1f, 0.5f, 0, 0 }, new float[] { 0.2f, 0.4f, 0.6f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); } public static ColorMatrix ScaleColor(this ColorMatrix obj, int Red, int Green, int Blue) { if (!Red.Between(-100, 100)) throw new ArgumentOutOfRangeException("Red", "Red must be between -100 and 100 percent"); if (!Green.Between(-100, 100)) throw new ArgumentOutOfRangeException("Green", "Green must be between -100 and 100 percent"); if (!Blue.Between(-100, 100)) throw new ArgumentOutOfRangeException("Blue", "Blue must be between -100 and 100 percent"); obj.Matrix40 = ((float)Red / 20f); obj.Matrix41 = ((float)Green / 20f); obj.Matrix42 = ((float)Blue / 20f); return obj; } public static ColorMatrix ScaleColor(this ColorMatrix obj, float Red, float Green, float Blue) { if (!Red.Between(-5, 5)) throw new ArgumentOutOfRangeException("Red", "Red must be between -5 and 5 percent"); if (!Green.Between(-5, 5)) throw new ArgumentOutOfRangeException("Green", "Green must be between -5 and 5 percent"); if (!Blue.Between(-5, 5)) throw new ArgumentOutOfRangeException("Blue", "Blue must be between -5 and 5 percent"); obj.Matrix40 = Red; obj.Matrix41 = Green; obj.Matrix42 = Blue; return obj; } public static ColorMatrix ScaleColor(this ColorMatrix obj, int Percent) { if (!Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("Percent", "Percent must be between -100 and 100"); return obj.ScaleColor(Percent, Percent, Percent); } public static ColorMatrix Multiply(this ColorMatrix obj, ColorMatrix multiplier) { return obj.Multiply(multiplier.ToArray()); } public static ColorMatrix Multiply(this ColorMatrix obj, float[][] multiplier) { ColorMatrix rtnmatrix = new ColorMatrix(); float[] matrixcolumn = new float[5]; for (int j = 0; j < 5; j++) { for (int x = 0; x < 5; x++) matrixcolumn[x] = obj[x, j]; for (int y = 0; y < 5; y++) { float s = 0; for (int x = 0; x < 5; x++) s += multiplier[y][x] * matrixcolumn[x]; rtnmatrix[y, j] = s; } } return rtnmatrix; } public static ColorMatrix Rotate(this ColorMatrix obj, float degrees) { double r = degrees * System.Math.PI / 180; // degrees to radians //ColorMatrix rtnmatrix = obj.SetMatrixRow(0, new float[] { (float)System.Math.Cos(r), (float)System.Math.Sin(r), 0, 0, 0 }); //rtnmatrix = rtnmatrix.SetMatrixRow(1, new float[] { (float)-System.Math.Sin(r), (float)-System.Math.Cos(r), 0, 0, 0 }); //ColorMatrix rtnmatrix = obj.SetMatrixRow(0, new float[] { (float)Math.Cos(r), (float)Math.Sin(r), 0, 0, 0 }); //rtnmatrix = rtnmatrix.SetMatrixRow(1, new float[] { (float)-Math.Sin(r), (float)-Math.Cos(r), 0, 0, 0 }); //rtnmatrix = rtnmatrix.SetMatrixRow(2, new float[] { 0, 0, 2, 0, 0 }); float[][] colorMatrixElements = { new float[] {(float)System.Math.Cos(r), (float)System.Math.Sin(r), 0, 0, 0}, new float[] {(float)-System.Math.Sin(r), (float)System.Math.Cos(r), 0, 0, 0}, new float[] {0, 0, 2, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} //obj.GetMatrixRow(2), //obj.GetMatrixRow(3), //obj.GetMatrixRow(4) }; return new ColorMatrix(colorMatrixElements); //return rtnmatrix; } public static ColorMatrix Rotate(this ColorMatrix obj, float degrees, ColorChannelRGBA channel) { double rad = degrees * (Math.PI / 180d); int x = 0; int y = 0; switch (channel) { case ColorChannelRGBA.R: y = 2; x = 1; break; case ColorChannelRGBA.G: y = 0; x = 2; break; case ColorChannelRGBA.B: y = 1; x = 0; break; case ColorChannelRGBA.A: y = 0; x = 0; break; default: y = 0; x = 0; break; } float[][] farray = DrawingExtensions.DefaultColorMatrix.ToArray(); farray[x][x] = farray[y][y] = (float)Math.Cos(rad); float s = (float)Math.Sin(rad); //if (channel == ColorChannelRGBA.G) //{ farray[y][x] = s; farray[x][y] = -s; //} //else //{ // farray[y][x] = -s; // farray[x][y] = s; //} //Multiply(m, order); return obj.Multiply(farray); } #endregion #region Image #region Help Methods NOT EXTENSIONS and not public private static Image CombineFrames(Image firstpage, Bitmap[] pages, EncoderParameters ep) { Bitmap Tiff = null; MemoryStream ms = new MemoryStream(); foreach (Bitmap page in pages) { if (Tiff == null) { if (firstpage != null) { Tiff = new Bitmap(firstpage); Tiff.Save(ms, ImageFormat.Tiff.GetImageCodecInfo(), ep); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); Tiff.SaveAdd(page, ep); } else { Tiff = new Bitmap(page); Tiff.Save(ms, ImageFormat.Tiff.GetImageCodecInfo(), ep); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); } } else Tiff.SaveAdd(page, ep); } ms.Flush(); Image rtnimg = Image.FromStream(ms); ms.Close(); return rtnimg; } private static void CombineFrames(Image firstpage, Bitmap[] pages, EncoderParameters ep, string filename) { Bitmap Tiff = null; FileStream ms = new FileStream(filename, FileMode.Create); foreach (Bitmap page in pages) { if (Tiff == null) { if (firstpage != null) { Tiff = new Bitmap(firstpage); Tiff.Save(ms, ImageFormat.Tiff.GetImageCodecInfo(), ep); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); Tiff.SaveAdd(page, ep); } else { Tiff = new Bitmap(page); Tiff.Save(ms, ImageFormat.Tiff.GetImageCodecInfo(), ep); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); } } else Tiff.SaveAdd(page, ep); } ms.Flush(); ms.Close(); } #endregion #region Help Methods NOT EXTENSIONS and public public static void FixGenericGDIplusError(ref Image img) { Bitmap bmp = new Bitmap(img.Width, img.Height); bmp.MakeTransparent(); Graphics g = Graphics.FromImage(bmp); g.DrawImage(img, bmp.GetRectangle(), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); img.Dispose(); img = bmp; } #endregion public static Image AddFrames(this Image obj, Bitmap[] pages) { EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); return CombineFrames(obj, pages, ep); } public static Image AddFrames(this Image obj, Bitmap[] pages, CompressionLevel compress) { EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); return CombineFrames(obj, pages, ep); } public static Image AddFrames(this Image obj, Bitmap[] pages, CompressionLevel compress, ColorDepth ColorDepth) { EncoderParameters ep = new EncoderParameters(3); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compress); ep.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, (long)ColorDepth); return CombineFrames(obj, pages, ep); } public static Image Brightness(this Image obj, int Percent) { if (!Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("Percent", "Percent must be between -100 and 100"); return obj.SetColorMatrix(DefaultColorMatrix.MakeBrightnessMatrix((float)Percent / 100f)); } public static Image Compress(this Image obj, ImageFormat format, int quality) { if (quality < 0 || quality > 100) throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); MemoryStream ms = new MemoryStream(); EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality); obj.Save(ms, format.GetImageCodecInfo(), ep); ms.Flush(); return Image.FromStream(ms); } public static Image Contrast(this Image obj, int Percent) { if (!Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("Percent", "Percent must be between -100 and 100"); return obj.SetColorMatrix(DefaultColorMatrix.MakeContrastMatrix((float)Percent / 100f)); } public static Image Crop(this Image obj, Rectangle cropArea) { try { Bitmap bmpImage = new Bitmap(obj); Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat); return (Image)(bmpCrop); } catch { throw; } } public static Image DrawString(this Image obj, string text, Font font, Brush brush, ContentAlignment textAlign, TextRenderingHint textRender) { Bitmap myBitmap = new Bitmap(obj); using (Graphics grObj = Graphics.FromImage(myBitmap)) { grObj.TextRenderingHint = textRender; StringFormat strFormat = new StringFormat(); #region Alignment Switch switch (textAlign) { case ContentAlignment.BottomCenter: { strFormat.Alignment = StringAlignment.Center; strFormat.LineAlignment = StringAlignment.Far; break; } case ContentAlignment.BottomLeft: { strFormat.Alignment = StringAlignment.Near; strFormat.LineAlignment = StringAlignment.Far; break; } case ContentAlignment.BottomRight: { strFormat.Alignment = StringAlignment.Far; strFormat.LineAlignment = StringAlignment.Far; break; } case ContentAlignment.MiddleCenter: { strFormat.Alignment = StringAlignment.Center; strFormat.LineAlignment = StringAlignment.Center; break; } case ContentAlignment.MiddleLeft: { strFormat.Alignment = StringAlignment.Near; strFormat.LineAlignment = StringAlignment.Center; break; } case ContentAlignment.MiddleRight: { strFormat.Alignment = StringAlignment.Far; strFormat.LineAlignment = StringAlignment.Center; break; } case ContentAlignment.TopCenter: { strFormat.Alignment = StringAlignment.Center; strFormat.LineAlignment = StringAlignment.Near; break; } case ContentAlignment.TopLeft: { strFormat.Alignment = StringAlignment.Near; strFormat.LineAlignment = StringAlignment.Near; break; } case ContentAlignment.TopRight: { strFormat.Alignment = StringAlignment.Far; strFormat.LineAlignment = StringAlignment.Near; break; } default: { strFormat.Alignment = StringAlignment.Near; strFormat.LineAlignment = StringAlignment.Near; break; } } #endregion grObj.DrawString(text, font, brush, new RectangleF(0, 0, obj.Width, obj.Height), strFormat); } return (Image)myBitmap; } public static Image DrawString(this Image obj, string text, Font font, Brush brush, ContentAlignment textAlign) { return obj.DrawString(text, font, brush, textAlign, TextRenderingHint.SystemDefault); } public static Image DrawString(this Image obj, string text, ContentAlignment textAlign) { return obj.DrawString(text, SystemFonts.DefaultFont, Brushes.Black, textAlign); } public static Image[] GetFrames(this Image obj, ImageFormat format) { try { if (obj.RawFormat.Equals(ImageFormat.Tiff) || obj.RawFormat.Equals(ImageFormat.Gif)) { List<Image> rtnarr = new List<Image>(); FrameDimension fd = new FrameDimension(obj.FrameDimensionsList[0]); for (int i = 0; i < obj.GetFrameCount(fd); i++) { obj.SelectActiveFrame(fd, i); using (MemoryStream byteStream = new MemoryStream()) { obj.Save(byteStream, format); rtnarr.Add(Image.FromStream(byteStream)); } } return rtnarr.ToArray(); } else return new Image[] { obj }; } catch { return new Image[] { obj }; } } public static Rectangle GetRectangle(this Image obj) { return new Rectangle(0, 0, obj.Width, obj.Height); } public static Rectangle GetRectangleTilt(this Image obj) { return new Rectangle(0, 0, obj.Height, obj.Width); } public static Image GrayScale(this Image obj) { ColorMatrix colorMatrix = new ColorMatrix(new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); return obj.SetColorMatrix(colorMatrix); } public static Image Invert(this Image obj) { return obj.SetColorMatrix(DefaultColorMatrix.MakeInvertColorMatrix()); } public static Image Merge(this Image obj, Image mergeWith, ContentAlignment alignment) { int xpos, ypos; switch (alignment) { case ContentAlignment.BottomCenter: xpos = (obj.Width - mergeWith.Width) / 2; ypos = (obj.Height - mergeWith.Height); break; case ContentAlignment.BottomLeft: xpos = 0; ypos = (obj.Height - mergeWith.Height); break; case ContentAlignment.BottomRight: xpos = (obj.Width - mergeWith.Width); ypos = (obj.Height - mergeWith.Height); break; case ContentAlignment.MiddleCenter: xpos = (obj.Width - mergeWith.Width) / 2; ypos = (obj.Height - mergeWith.Height) / 2; break; case ContentAlignment.MiddleLeft: xpos = 0; ypos = (obj.Height - mergeWith.Height) / 2; break; case ContentAlignment.MiddleRight: xpos = (obj.Width - mergeWith.Width); ypos = (obj.Height - mergeWith.Height) / 2; break; case ContentAlignment.TopCenter: xpos = (obj.Width - mergeWith.Width) / 2; ypos = 0; break; case ContentAlignment.TopLeft: xpos = 0; ypos = 0; break; case ContentAlignment.TopRight: xpos = (obj.Width - mergeWith.Width); ypos = 0; break; default: xpos = 0; ypos = 0; break; } Bitmap img = new Bitmap(obj); Graphics g = Graphics.FromImage(img); g.DrawImageUnscaled(mergeWith, xpos, ypos); g.Save(); return img; } public static Image MergeToTiff(this Bitmap[] obj) { EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); return CombineFrames(null, obj, ep); } public static void MergeToTiff(this Bitmap[] obj, string filename) { EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); CombineFrames(null, obj, ep, filename); } public static Image Resize(this Image obj, float Percent) { return obj.Resize(Percent, obj.PixelFormat); } public static Image Resize(this Image obj, float Percent, InterpolationMode interpolationMode) { return obj.Resize(Percent, obj.PixelFormat, interpolationMode); } public static Image Resize(this Image obj, float Percent, PixelFormat pixelFormat) { return obj.Resize(Percent, pixelFormat, obj.HorizontalResolution, obj.VerticalResolution); } public static Image Resize(this Image obj, float Percent, PixelFormat pixelFormat, InterpolationMode interpolationMode) { return obj.Resize(Percent, pixelFormat, obj.HorizontalResolution, obj.VerticalResolution, interpolationMode); } public static Image Resize(this Image obj, float Percent, PixelFormat pixelFormat, float horizontalResolution, float verticalResolution) { return obj.Resize(Percent, pixelFormat, horizontalResolution, verticalResolution, InterpolationMode.HighQualityBicubic); } public static Image Resize(this Image obj, float Percent, PixelFormat pixelFormat, float horizontalResolution, float verticalResolution, InterpolationMode interpolationMode) { try { float nPercent = Percent; // ((float)Percent / 100); int destWidth = (int)Math.Round(((float)obj.Width * nPercent)); int destHeight = (int)Math.Round(((float)obj.Height * nPercent)); Bitmap bmObj = new Bitmap(destWidth, destHeight, pixelFormat); bmObj.SetResolution(horizontalResolution, verticalResolution); using (Graphics grObj = Graphics.FromImage(bmObj)) { grObj.Clear(Color.Transparent); grObj.InterpolationMode = interpolationMode; grObj.DrawImage(obj, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, obj.Width, obj.Height), GraphicsUnit.Pixel); } return (Image)bmObj; } catch { throw; } } public static Image Resize(this Image obj, int Width, int Height) { return obj.Resize(Width, Height, obj.PixelFormat); } public static Image Resize(this Image obj, int Width, int Height, InterpolationMode interpolationMode) { return obj.Resize(Width, Height, obj.PixelFormat, interpolationMode); } public static Image Resize(this Image obj, int Width, int Height, PixelFormat pixelFormat) { return obj.Resize(Width, Height, pixelFormat, obj.HorizontalResolution, obj.VerticalResolution); } public static Image Resize(this Image obj, int Width, int Height, PixelFormat pixelFormat, InterpolationMode interpolationMode) { return obj.Resize(Width, Height, pixelFormat, obj.HorizontalResolution, obj.VerticalResolution, interpolationMode); } public static Image Resize(this Image obj, int Width, int Height, PixelFormat pixelFormat, float horizontalResolution, float verticalResolution) { return obj.Resize(Width, Height, pixelFormat, horizontalResolution, verticalResolution, InterpolationMode.HighQualityBicubic); } public static Image Resize(this Image obj, int Width, int Height, PixelFormat pixelFormat, float horizontalResolution, float verticalResolution, InterpolationMode interpolationMode) { try { int sourceWidth = obj.Width; int sourceHeight = obj.Height; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)Width / (float)sourceWidth); nPercentH = ((float)Height / (float)sourceHeight); if (nPercentH < nPercentW) { nPercent = nPercentH; destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2); } else { nPercent = nPercentW; destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2); } int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap bmObj = new Bitmap(Width, Height, pixelFormat); bmObj.SetResolution(horizontalResolution, verticalResolution); using (Graphics grObj = Graphics.FromImage(bmObj)) { grObj.Clear(Color.Transparent); grObj.InterpolationMode = interpolationMode; grObj.DrawImage(obj, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel); } return (Image)bmObj; } catch { throw; } } public static Image Resize(this Image obj, int Size, bool KeepAspectRatio) { if (KeepAspectRatio) { if (obj.Width >= obj.Height) return obj.Resize(((float)Size / (float)obj.Width)); else return obj.Resize(((float)Size / (float)obj.Height)); } else return obj.Resize(Size, Size); } public static Image Resize(this Image obj, int Size, bool KeepAspectRatio, InterpolationMode interpolationMode) { if (KeepAspectRatio) { if (obj.Width >= obj.Height) return obj.Resize(((float)Size / (float)obj.Width), interpolationMode); else return obj.Resize(((float)Size / (float)obj.Height), interpolationMode); } else return obj.Resize(Size, Size, interpolationMode); } public static Image Resize(this Image obj, int Size, bool KeepAspectRatio, PixelFormat pixelFormat, InterpolationMode interpolationMode) { if (KeepAspectRatio) { if (obj.Width >= obj.Height) return obj.Resize(((float)Size / (float)obj.Width), pixelFormat, interpolationMode); else return obj.Resize(((float)Size / (float)obj.Height), pixelFormat, interpolationMode); } else return obj.Resize(Size, Size, pixelFormat, interpolationMode); } public static Image Rotate(this Image obj, int angle) { Bitmap bmObj = new Bitmap(obj.Width, obj.Height); Graphics grObj = Graphics.FromImage(bmObj); grObj.TranslateTransform((float)obj.Width / 2, (float)obj.Height / 2); grObj.RotateTransform(angle); grObj.TranslateTransform(-(float)obj.Width / 2, -(float)obj.Height / 2); grObj.DrawImage(obj, new Point(0, 0)); return bmObj; } public static Image ScaleColor(this Image obj, int R_Percent, int G_Percent, int B_Percent) { if (!R_Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("R_Percent", "R_Percent must be between -100 and 100"); if (!G_Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("G_Percent", "G_Percent must be between -100 and 100"); if (!B_Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("B_Percent", "B_Percent must be between -100 and 100"); return obj.SetColorMatrix(DefaultColorMatrix.ScaleColor(R_Percent, G_Percent, B_Percent)); } public static Image SetColorMatrix(this Image obj, ColorMatrix colormatrix) { return obj.SetColorMatrix(colormatrix, GraphicsUnit.Pixel); } public static Image SetColorMatrix(this Image obj, ColorMatrix colormatrix, GraphicsUnit graphicunit) { try { Bitmap bmObj = new Bitmap(obj.Width, obj.Height); using (Graphics g = Graphics.FromImage(bmObj)) { ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colormatrix); g.DrawImage(obj, new Rectangle(0, 0, obj.Width, obj.Height), 0, 0, obj.Width, obj.Height, graphicunit, attributes); } return (Image)bmObj; } catch { throw; } } public static Image ToImage(this byte[] obj) { MemoryStream ms = new MemoryStream(obj); Image returnImage = Image.FromStream(ms); return returnImage; } public static byte[] ToByteArray(this Image obj) { byte[] rtnval = null; using (MemoryStream ms = new MemoryStream()) { Image img = obj.FixGenericGDIplusError(); img.Save(ms, img.RawFormat); rtnval = ms.GetBuffer(); } return rtnval; } public static Stream ToStream(this Image obj) { MemoryStream ms = new MemoryStream(); Image img = obj.FixGenericGDIplusError(); img.Save(ms, img.RawFormat); return ms; } public static Icon ToIcon(this Image obj) { try { Bitmap bmObj = new Bitmap(obj); return Icon.FromHandle(bmObj.GetHicon()); } catch { try { MemoryStream buffer = new MemoryStream(); obj.Save(buffer, ImageFormat.Icon); buffer.Position = 0; return new Icon(buffer); } catch { throw; } } } public static Icon ToIcon(this Image obj, int Size, bool KeepAspectRatio) { return obj.Resize(Size, KeepAspectRatio).ToIcon(); } public static Image Tint(this Image obj, int Percent) { if (!Percent.Between(-100, 100)) throw new ArgumentOutOfRangeException("Percent", "Percent must be between -100 and 100"); return obj.SetColorMatrix(DefaultColorMatrix.MakeTintMatrix((float)Percent)); } public static Image FixGenericGDIplusError(this Image img) { Bitmap bmp = new Bitmap(img.Width, img.Height); bmp.MakeTransparent(); Graphics g = Graphics.FromImage(bmp); g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); return bmp; } public static string ToBase64(this Image obj) { return Convert.ToBase64String(obj.ToByteArray()); } public static Image FromBase64(this string obj) { return Convert.FromBase64String(obj).ToImage(); } #endregion #region Font /// <summary> /// Sets the style of the font. /// </summary> /// <param name="obj">The font type to change</param> /// <param name="style">style to apply</param> /// <returns>new font that has the new style applied.</returns> /// <remarks>Developed by Paw Jershauge. Find more Extensions and C# code on: http://pawjershauge.blogspot.com</remarks> public static Font SetStyle(this Font obj, FontStyle style) { return new Font(obj, style); } #endregion public static ImageCodecInfo GetImageCodecInfo(this ImageFormat obj) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) if (codec.FormatID == obj.Guid) return codec; return null; } }
September 20, 2010
Image/Drawing Extensions
Some Image/Drawing Extensions I picked up or created along the way.
April 7, 2010
PropertyGrid fails to group a property marked as being in Category Behavior.
I found a small bug in the PropertyGrid when running 64bit operating systems.
I have some classes where I want to categorize some properties, so for that I ofcause use the CategoryAttribute class. thats common knowledge.
But all my properties maked as being under the category "Behavior" is listed under a group/category called "Type"
see screendump.
So far i tested this on:
Windows 7 (64 bit): Bug confirmed
Windows 7 (32 bit):No bug
Vista (64 bit): Bug confirmed
Vista (32 bit):No bug
Xp (32 bit):No bug
Bug found
The following list of category names will be converted when running the danish version of the CLR
The forllowing data was found under: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\da\System.Windows.Forms.Resources.dll
see screendump.
Windows 7 (32 bit):
Vista (64 bit): Bug confirmed
Vista (32 bit):
Xp (32 bit):
The following list of category names will be converted when running the danish version of the CLR
- Category("Appearance") = Udseende
- Category("Behavior") = Type (the correct word for this would be "adfærd" or "opførsel" and NOT Type)
- Category("Data") = Data
- Category("Font") = Skrifttype
- Category("List") = Oversigt (the correct word for this would be "List" or "Liste" and NOT Oversigt)
- Category("Misc") = Diverse
- Category("Position") = Placering
- Category("Scale") = Skalering
- Category("Text") = Tekst
- PropertyCategoryAppearance=Udseende
- PropertyCategoryBehavior=Type
- PropertyCategoryData=Data
- PropertyCategoryFont=Skrifttype
- PropertyCategoryList=Oversigt
- PropertyCategoryMisc=Diverse
- PropertyCategoryPosition=Placering
- PropertyCategoryScale=Skalering
- PropertyCategoryText=Tekst
March 12, 2010
DateTime Extensions
Some Extensions i grabbed along the way or made up. I often have use for som simple extension, so heres my DateTime Extensions
DateTime Examples:
Having the following demo Variable called DemoDate set to 2010/6/29 13:15:59.321 ( Tuesday 29 of June ) we will test the Extensions I've made.
Just to illustrate heres the calendar for June & July in 2010.
DateTime Examples:
Having the following demo Variable called DemoDate set to 2010/6/29 13:15:59.321 ( Tuesday 29 of June ) we will test the Extensions I've made.
DateTime DemoDate = new DateTime(2010, 6, 29, 13, 15, 59, 321); // representing an datetime equal to 2010/6/29 13:15:59.321 ( Tuesday 29 of June ) DemoDate.AddFinancialDays(4) // returns 2010-07-05 13:15:59.321 DemoDate.BeginningOfDay() // returns 2010-06-29 00:00:00.000 DemoDate.BeginningOfMonth() // returns 2010-06-01 00:00:00.000 DemoDate.Between(new DateTime(2010, 3, 1), new DateTime(2010, 7, 1)) // returns True DemoDate.CountFinancialDays(new DateTime(2010, 7, 9, 12, 0, 0, 0)) // returns 7 DemoDate.CountFinancialDays(new DateTime(2010, 7, 9, 14, 0, 0, 0)) // returns 8 DemoDate.EndOfDay() // returns 2010-06-29 23:59:59.999 DemoDate.EndOfMonth() // returns 2010-06-30 23:59:59.999 DemoDate.FindClosestWeekDay() // returns 2010-06-29 13:15:59.321 DemoDate.FirstDayOfWeekInMonth(DayOfWeek.Thursday) // returns 2010-06-03 00:00:00.000 DemoDate.FirstWeekDayOfMonth() // returns 2010-06-01 00:00:00.000 DemoDate.GetDateByWeek(45, DayOfWeek.Wednesday) // returns 2010-11-10 00:00:00.000 DemoDate.IsWeekend() // returns False DemoDate.LastDayOfWeekInMonth(DayOfWeek.Saturday) // returns 2010-06-26 00:00:00.000 DemoDate.LastWeekDayOfMonth() // returns 2010-06-30 00:00:00.000 DemoDate.Next(DayOfWeek.Monday) // returns 2010-07-05 13:15:59.321 DemoDate.Previous(DayOfWeek.Friday) // returns 2010-06-25 13:15:59.321 DemoDate.Quarter() // returns 2 DemoDate.SetDate(2020) // returns 2020-06-29 13:15:59.321 NOTE: although we only wanted to set the month, the day will shift also, since the 29 of february 2019 does not exist. DemoDate.SetDate(2019, 2) // returns 2019-03-01 13:15:59.321 DemoDate.SetDate(2021, 3, 12) // returns 2021-03-12 13:15:59.321 DemoDate.SetTime(20) // returns 2010-06-29 20:15:59.321 DemoDate.SetTime(21, 35) // returns 2010-06-29 21:35:59.321 DemoDate.SetTime(17, 45, 3) // returns 2010-06-29 17:45:03.321 DemoDate.SetTime(9, 12, 59, 600) // returns 2010-06-29 09:12:59.600 DemoDate.ToMicrosoftNumber() // returns 1214745359,321
Just to illustrate heres the calendar for June & July in 2010.
June 2010 | |||||||
Week | Mon | Tue | Wen | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|---|
22 | 31 | 1 | 2 | 3 | 4 | 5 | 6 |
23 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
24 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
25 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
26 | 28 | 29 | 30 | 1 | 2 | 3 | 4 |
July 2010 | |||||||
Week | Mon | Tue | Wen | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|---|
26 | 28 | 29 | 30 | 1 | 2 | 3 | 4 |
27 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
28 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
29 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
30 | 26 | 27 | 28 | 29 | 30 | 31 | 1 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using PawJershauge.Extensions.ValueTypes; namespace PawJershauge.Extensions { namespace DateAndTime { public static class DateTimeExtensions { /// <summary> /// Returns the first day of week with in the month. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="dow">What day of week to find the first one of in the month.</param> /// <returns>Returns DateTime object that represents the first day of week with in the month.</returns> public static DateTime FirstDayOfWeekInMonth(this DateTime obj, DayOfWeek dow) { DateTime firstDay = new DateTime(obj.Year, obj.Month, 1); int diff = firstDay.DayOfWeek - dow; if (diff > 0) diff -= 7; return firstDay.AddDays(diff * -1); } /// <summary> /// Returns the first weekday (Financial day) of the month /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns DateTime object that represents the first weekday (Financial day) of the month</returns> public static DateTime FirstWeekDayOfMonth(this DateTime obj) { DateTime firstDay = new DateTime(obj.Year, obj.Month, 1); for (int i = 0; i < 7; i++) { if (firstDay.AddDays(i).DayOfWeek != DayOfWeek.Saturday && firstDay.AddDays(i).DayOfWeek != DayOfWeek.Sunday) return firstDay.AddDays(i); } return firstDay; } /// <summary> /// Returns the last day of week with in the month. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="dow">What day of week to find the last one of in the month.</param> /// <returns>Returns DateTime object that represents the last day of week with in the month.</returns> public static DateTime LastDayOfWeekInMonth(this DateTime obj, DayOfWeek dow) { DateTime lastDay = new DateTime(obj.Year, obj.Month, DateTime.DaysInMonth(obj.Year, obj.Month)); DayOfWeek lastDow = lastDay.DayOfWeek; int diff = dow - lastDow; if (diff > 0) diff -= 7; return lastDay.AddDays(diff); } /// <summary> /// Returns the last weekday (Financial day) of the month /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns DateTime object that represents the last weekday (Financial day) of the month</returns> public static DateTime LastWeekDayOfMonth(this DateTime obj) { DateTime lastDay = new DateTime(obj.Year, obj.Month, DateTime.DaysInMonth(obj.Year, obj.Month)); for (int i = 0; i < 7; i++) { if (lastDay.AddDays(i * -1).DayOfWeek != DayOfWeek.Saturday && lastDay.AddDays(i * -1).DayOfWeek != DayOfWeek.Sunday) return lastDay.AddDays(i * -1); } return lastDay; } /// <summary> /// Returns the closest Weekday (Financial day) Date /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns the closest Weekday (Financial day) Date</returns> public static DateTime FindClosestWeekDay(this DateTime obj) { if (obj.DayOfWeek == DayOfWeek.Saturday) return obj.AddDays(-1); if (obj.DayOfWeek == DayOfWeek.Sunday) return obj.AddDays(1); else return obj; } /// <summary> /// Returns the very end of the given month (the last millisecond of the last hour for the given date) /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns the very end of the given month (the last millisecond of the last hour for the given date)</returns> public static DateTime EndOfMonth(this DateTime obj) { return new DateTime(obj.Year, obj.Month, DateTime.DaysInMonth(obj.Year, obj.Month), 23, 59, 59, 999); } /// <summary> /// Returns the Start of the given month (the fist millisecond of the given date) /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns the Start of the given month (the fist millisecond of the given date)</returns> public static DateTime BeginningOfMonth(this DateTime obj) { return new DateTime(obj.Year, obj.Month, 1, 0, 0, 0, 0); } /// <summary> /// Returns the very end of the given day (the last millisecond of the last hour for the given date) /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns the very end of the given day (the last millisecond of the last hour for the given date)</returns> public static DateTime EndOfDay(this DateTime obj) { return obj.SetTime(23, 59, 59, 999); } /// <summary> /// Returns the Start of the given day (the fist millisecond of the given date) /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns the Start of the given day (the fist millisecond of the given date)</returns> public static DateTime BeginningOfDay(this DateTime obj) { return obj.SetTime(0, 0, 0, 0); } /// <summary> /// Returns a given datetime according to the week of year and the specified day within the week. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="week">A number of whole and fractional weeks. The value parameter can only be positive.</param> /// <param name="dayofweek">A DayOfWeek to find in the week</param> /// <returns>A DateTime whose value is the sum according to the week of year and the specified day within the week.</returns> public static DateTime GetDateByWeek(this DateTime obj, int week, DayOfWeek dayofweek) { if (week > 0 && week < 54) { DateTime FirstDayOfyear = new DateTime(obj.Year, 1, 1); int daysToFirstCorrectDay = (((int)dayofweek - (int)FirstDayOfyear.DayOfWeek) + 7) % 7; return FirstDayOfyear.AddDays(7 * (week - 1) + daysToFirstCorrectDay); } else return obj; } private static int Sub(DayOfWeek s, DayOfWeek e) { if ((s - e) > 0) return (s - e) - 7; if ((s - e) == 0) return -7; return (s - e); } /// <summary> /// Returns first next occurence of specified DayOfTheWeek /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="day">A DayOfWeek to find the next occurence of</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the enum value represented by the day.</returns> public static DateTime Next(this DateTime obj, DayOfWeek day) { return obj.AddDays(Sub(obj.DayOfWeek, day) * -1); } /// <summary> /// Returns next "first" occurence of specified DayOfTheWeek /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="day">A DayOfWeek to find the previous occurence of</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the enum value represented by the day.</returns> public static DateTime Previous(this DateTime obj, DayOfWeek day) { return obj.AddDays(Sub(day, obj.DayOfWeek)); } private static DateTime SetDateWithChecks(DateTime obj, int year, int month, int day, int? hour, int? minute, int? second, int? millisecond) { DateTime StartDate; if (year == 0) StartDate = new DateTime(obj.Year, 1, 1, 0, 0, 0, 0); else { if (DateTime.MaxValue.Year < year) StartDate = new DateTime(DateTime.MinValue.Year, 1, 1, 0, 0, 0, 0); else if (DateTime.MinValue.Year > year) StartDate = new DateTime(DateTime.MaxValue.Year, 1, 1, 0, 0, 0, 0); else StartDate = new DateTime(year, 1, 1, 0, 0, 0, 0); } if (month == 0) StartDate = StartDate.AddMonths(obj.Month - 1); else StartDate = StartDate.AddMonths(month - 1); if (day == 0) StartDate = StartDate.AddDays(obj.Day - 1); else StartDate = StartDate.AddDays(day - 1); if (!hour.HasValue) StartDate = StartDate.AddHours(obj.Hour); else StartDate = StartDate.AddHours(hour.Value); if (!minute.HasValue) StartDate = StartDate.AddMinutes(obj.Minute); else StartDate = StartDate.AddMinutes(minute.Value); if (!second.HasValue) StartDate = StartDate.AddSeconds(obj.Second); else StartDate = StartDate.AddSeconds(second.Value); if (!millisecond.HasValue) StartDate = StartDate.AddMilliseconds(obj.Millisecond); else StartDate = StartDate.AddMilliseconds(millisecond.Value); return StartDate; } /// <summary> /// Returns the original DateTime with Hour part changed to supplied hour parameter /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="hour">A number of whole and fractional hours. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetTime(this DateTime obj, int hour) { return SetDateWithChecks(obj, 0, 0, 0, hour, null, null, null); } /// <summary> /// Returns the original DateTime with Hour and Minute parts changed to supplied hour and minute parameters /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="hour">A number of whole and fractional hours. The value parameter can be negative or positive.</param> /// <param name="minute">A number of whole and fractional minutes. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetTime(this DateTime obj, int hour, int minute) { return SetDateWithChecks(obj, 0, 0, 0, hour, minute, null, null); } /// <summary> /// Returns the original DateTime with Hour, Minute and Second parts changed to supplied hour, minute and second parameters /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="hour">A number of whole and fractional hours. The value parameter can be negative or positive.</param> /// <param name="minute">A number of whole and fractional minutes. The value parameter can be negative or positive.</param> /// <param name="second">A number of whole and fractional seconds. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetTime(this DateTime obj, int hour, int minute, int second) { return SetDateWithChecks(obj, 0, 0, 0, hour, minute, second, null); } /// <summary> /// Returns the original DateTime with Hour, Minute, Second and Millisecond parts changed to supplied hour, minute, second and millisecond parameters /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="hour">A number of whole and fractional hours. The value parameter can be negative or positive.</param> /// <param name="minute">A number of whole and fractional minutes. The value parameter can be negative or positive.</param> /// <param name="second">A number of whole and fractional seconds. The value parameter can be negative or positive.</param> /// <param name="millisecond">A number of whole and fractional milliseconds. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetTime(this DateTime obj, int hour, int minute, int second, int millisecond) { return SetDateWithChecks(obj, 0, 0, 0, hour, minute, second, millisecond); } /// <summary> /// Returns DateTime with changed Year part /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="year">A number of whole and fractional years. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetDate(this DateTime obj, int year) { return SetDateWithChecks(obj, year, 0, 0, null, null, null, null); } /// <summary> /// Returns DateTime with changed Year and Month part /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="year">A number of whole and fractional years. The value parameter can be negative or positive.</param> /// <param name="month">A number of whole and fractional month. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetDate(this DateTime obj, int year, int month) { return SetDateWithChecks(obj, year, month, 0, null, null, null, null); } /// <summary> /// Returns DateTime with changed Year, Month and Day part /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="year">A number of whole and fractional years. The value parameter can be negative or positive.</param> /// <param name="month">A number of whole and fractional month. The value parameter can be negative or positive.</param> /// <param name="day">A number of whole and fractional day. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the numbers represented by the parameters.</returns> public static DateTime SetDate(this DateTime obj, int year, int month, int day) { return SetDateWithChecks(obj, year, month, day, null, null, null, null); } /// <summary> /// Adds the specified number of financials days to the value of this instance. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="days">A number of whole and fractional financial days. The value parameter can be negative or positive.</param> /// <returns>A DateTime whose value is the sum of the date and time represented by this instance and the number of financial days represented by days.</returns> public static DateTime AddFinancialDays(this DateTime obj, int days) { int addint = Math.Sign(days); for (int i = 0; i < (Math.Sign(days) * days); i++) { do { obj = obj.AddDays(addint); } while (obj.IsWeekend()); } return obj; } /// <summary> /// Calculate Financial days between two dates. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="otherdate">End or start date to calculate to or from.</param> /// <returns>Amount of financial days between the two dates</returns> public static int CountFinancialDays(this DateTime obj, DateTime otherdate) { TimeSpan ts = (otherdate - obj); int addint = Math.Sign(ts.Days); int unsigneddays = (Math.Sign(ts.Days) * ts.Days); int businessdays = 0; for (int i = 0; i < unsigneddays; i++) { obj = obj.AddDays(addint); if (!obj.IsWeekend()) businessdays++; } return businessdays; } /// <summary> /// Converts any datetime to the amount of seconds from 1972.01.01 00:00:00 /// Microsoft sometimes uses the amount of seconds from 1972.01.01 00:00:00 to indicate an datetime. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Total seconds past since 1972.01.01 00:00:00</returns> public static double ToMicrosoftNumber(this DateTime obj) { return (obj - new DateTime(1972, 1, 1, 0, 0, 0, 0)).TotalSeconds; } /// <summary> /// Returns true if the day is Saturday or Sunday /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>boolean value indicating if the date is a weekend</returns> public static bool IsWeekend(this DateTime obj) { return (obj.DayOfWeek == DayOfWeek.Saturday || obj.DayOfWeek == DayOfWeek.Sunday); } /// <summary> /// Returns true if the date is between or equal to one of the two values. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <param name="startvalue">Start date to check for</param> /// <param name="endvalue">End date to check for</param> /// <returns>boolean value indicating if the date is between or equal to one of the two values</returns> public static bool Between(this DateTime obj, DateTime startDate, DateTime endDate) { return obj.Ticks.Between(startDate.Ticks, endDate.Ticks); } /// <summary> /// Get the quarter that the datetime is in. /// </summary> /// <param name="obj">DateTime Base, from where the calculation will be preformed.</param> /// <returns>Returns 1 to 4 that represenst the quarter that the datetime is in.</returns> public static int Quarter(this DateTime obj) { return ((obj.Month - 1) / 3) + 1; } } } }
Etiketter:
C#,
DateTime,
Extension,
Framework 3.5,
Managed types
Subscribe to:
Posts (Atom)