123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 |
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.IO;
- using System.Windows.Forms;
- namespace PaintDotNet
- {
- /// <summary>
- /// Defines miscellaneous constants and static functions.
- /// </summary>
- public sealed class Utility
- {
- private Utility()
- {
- }
- public static Keys LetterOrDigitCharToKeys(char c)
- {
- if (c >= 'a' && c <= 'z')
- {
- return (Keys)((int)(c - 'a') + (int)Keys.A);
- }
- else if (c >= 'A' && c <= 'Z')
- {
- return (Keys)((int)(c - 'A') + (int)Keys.A);
- }
- else if (c >= '0' && c <= '9')
- {
- return (Keys)((int)(c - '0') + (int)Keys.D0);
- }
- else
- {
- return Keys.None;
- }
- }
- public static Control FindFocus()
- {
- foreach (Form form in Application.OpenForms)
- {
- Control focused = FindFocus(form);
- if (focused != null)
- {
- return focused;
- }
- }
- return null;
- }
- private static Control FindFocus(Control c)
- {
- if (c.Focused)
- {
- return c;
- }
- foreach (Control child in c.Controls)
- {
- Control f = FindFocus(child);
- if (f != null)
- {
- return f;
- }
- }
- return null;
- }
- public static void DrawColorRectangle(Graphics g, Rectangle rect, Color color, bool drawBorder)
- {
- int inflateAmt = drawBorder ? -2 : 0;
- Rectangle colorRectangle = Rectangle.Inflate(rect, inflateAmt, inflateAmt);
- Brush colorBrush = new LinearGradientBrush(colorRectangle, Color.FromArgb(255, color), color, 90.0f, false);
- HatchBrush backgroundBrush = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.FromArgb(191, 191, 191), Color.FromArgb(255, 255, 255));
- if (drawBorder)
- {
- g.DrawRectangle(Pens.Black, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
- g.DrawRectangle(Pens.White, rect.Left + 1, rect.Top + 1, rect.Width - 3, rect.Height - 3);
- }
- PixelOffsetMode oldPOM = g.PixelOffsetMode;
- g.PixelOffsetMode = PixelOffsetMode.Half;
- g.FillRectangle(backgroundBrush, colorRectangle);
- g.FillRectangle(colorBrush, colorRectangle);
- g.PixelOffsetMode = oldPOM;
- backgroundBrush.Dispose();
- colorBrush.Dispose();
- }
- public static Font CreateFont(string name, float size, FontStyle style)
- {
- Font returnFont;
- try
- {
- returnFont = new Font(name, size, style);
- }
- catch (Exception)
- {
- returnFont = new Font(FontFamily.GenericSansSerif, size);
- }
- return returnFont;
- }
- public static readonly Color TransparentKey = Color.FromArgb(192, 192, 192);
- private static bool allowGCFullCollect = true;
- public static bool AllowGCFullCollect
- {
- get
- {
- return allowGCFullCollect;
- }
- set
- {
- allowGCFullCollect = value;
- }
- }
- public static void GCFullCollect()
- {
- if (AllowGCFullCollect)
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- public static bool IsArrowKey(Keys keyData)
- {
- Keys key = keyData & Keys.KeyCode;
- if (key == Keys.Up || key == Keys.Down || key == Keys.Left || key == Keys.Right)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool DoesControlHaveMouseCaptured(Control control)
- {
- bool result = false;
- result |= control.Capture;
- foreach (Control c in control.Controls)
- {
- result |= DoesControlHaveMouseCaptured(c);
- }
- return result;
- }
- public static void SplitRectangle(Rectangle rect, Rectangle[] rects)
- {
- int height = rect.Height;
- for (int i = 0; i < rects.Length; ++i)
- {
- Rectangle newRect = Rectangle.FromLTRB(rect.Left,
- rect.Top + ((height * i) / rects.Length),
- rect.Right,
- rect.Top + ((height * (i + 1)) / rects.Length));
- rects[i] = newRect;
- }
- }
-
- public static bool CheckNumericUpDown(NumericUpDown upDown)
- {
- int a;
- bool result = int.TryParse(upDown.Text, out a);
- if (result && (a <= (int)upDown.Maximum) && (a >= (int)upDown.Minimum))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static void SetNumericUpDownValue(NumericUpDown upDown, decimal newValue)
- {
- if (upDown.Value != newValue)
- {
- upDown.Value = newValue;
- }
- }
- public static void SetNumericUpDownValue(NumericUpDown upDown, int newValue)
- {
- SetNumericUpDownValue(upDown, (decimal)newValue);
- }
- public static void ErrorBox(IWin32Window parent, string message)
- {
- MessageBox.Show(parent, message, PdnInfo.GetBareProductName(), MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- public static DialogResult AskYesNo(IWin32Window parent, string question)
- {
- return MessageBox.Show(parent, question, PdnInfo.GetBareProductName(), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
- }
- public static Icon ImageToIcon(Image image)
- {
- return ImageToIcon(image, Utility.TransparentKey);
- }
- public static Icon ImageToIcon(Image image, bool disposeImage)
- {
- return ImageToIcon(image, Utility.TransparentKey, disposeImage);
- }
- public static Icon ImageToIcon(Image image, Color seeThru)
- {
- return ImageToIcon(image, seeThru, false);
- }
- /// <summary>
- /// Converts an Image to an Icon.
- /// </summary>
- /// <param name="image">The Image to convert to an icon. Must be an appropriate icon size (32x32, 16x16, etc).</param>
- /// <param name="seeThru">The color that will be treated as transparent in the icon.</param>
- /// <param name="disposeImage">Whether or not to dispose the passed-in Image.</param>
- /// <returns>An Icon representation of the Image.</returns>
- public static Icon ImageToIcon(Image image, Color seeThru, bool disposeImage)
- {
- Bitmap bitmap = new Bitmap(image);
- for (int y = 0; y < bitmap.Height; ++y)
- {
- for (int x = 0; x < bitmap.Width; ++x)
- {
- if (bitmap.GetPixel(x, y) == seeThru)
- {
- bitmap.SetPixel(x, y, Color.FromArgb(0));
- }
- }
- }
- Icon icon = Icon.FromHandle(bitmap.GetHicon());
- bitmap.Dispose();
- if (disposeImage)
- {
- image.Dispose();
- }
- return icon;
- }
- /// <summary>
- /// Converts a RectangleF to RectangleF by rounding down the Location and rounding
- /// up the Size.
- /// </summary>
- public static Rectangle RoundRectangle(RectangleF rectF)
- {
- float left = (float)Math.Floor(rectF.Left);
- float top = (float)Math.Floor(rectF.Top);
- float right = (float)Math.Ceiling(rectF.Right);
- float bottom = (float)Math.Ceiling(rectF.Bottom);
- return Rectangle.Truncate(RectangleF.FromLTRB(left, top, right, bottom));
- }
- /// <summary>
- /// Allows you to find the bounding box for a "region" that is described as an
- /// array of bounding boxes.
- /// </summary>
- /// <param name="rectsF">The "region" you want to find a bounding box for.</param>
- /// <returns>A RectangleF structure that surrounds the Region.</returns>
- public static RectangleF GetRegionBounds(RectangleF[] rectsF, int startIndex, int length)
- {
- if (rectsF.Length == 0)
- {
- return RectangleF.Empty;
- }
- float left = rectsF[startIndex].Left;
- float top = rectsF[startIndex].Top;
- float right = rectsF[startIndex].Right;
- float bottom = rectsF[startIndex].Bottom;
- for (int i = startIndex + 1; i < startIndex + length; ++i)
- {
- RectangleF rectF = rectsF[i];
- if (rectF.Left < left)
- {
- left = rectF.Left;
- }
- if (rectF.Top < top)
- {
- top = rectF.Top;
- }
- if (rectF.Right > right)
- {
- right = rectF.Right;
- }
- if (rectF.Bottom > bottom)
- {
- bottom = rectF.Bottom;
- }
- }
- return RectangleF.FromLTRB(left, top, right, bottom);
- }
- /// <summary>
- /// Allows you to find the bounding box for a "region" that is described as an
- /// array of bounding boxes.
- /// </summary>
- /// <param name="rectsF">The "region" you want to find a bounding box for.</param>
- /// <returns>A RectangleF structure that surrounds the Region.</returns>
- public static Rectangle GetRegionBounds(Rectangle[] rects, int startIndex, int length)
- {
- if (rects.Length == 0)
- {
- return Rectangle.Empty;
- }
- int left = rects[startIndex].Left;
- int top = rects[startIndex].Top;
- int right = rects[startIndex].Right;
- int bottom = rects[startIndex].Bottom;
- for (int i = startIndex + 1; i < startIndex + length; ++i)
- {
- Rectangle rect = rects[i];
- if (rect.Left < left)
- {
- left = rect.Left;
- }
- if (rect.Top < top)
- {
- top = rect.Top;
- }
- if (rect.Right > right)
- {
- right = rect.Right;
- }
- if (rect.Bottom > bottom)
- {
- bottom = rect.Bottom;
- }
- }
- return Rectangle.FromLTRB(left, top, right, bottom);
- }
- public static double Clamp(double x, double min, double max)
- {
- if (x < min)
- {
- return min;
- }
- else if (x > max)
- {
- return max;
- }
- else
- {
- return x;
- }
- }
- public static int Clamp(int x, int min, int max)
- {
- if (x < min)
- {
- return min;
- }
- else if (x > max)
- {
- return max;
- }
- else
- {
- return x;
- }
- }
- public static byte ClampToByte(double x)
- {
- if (x > 255)
- {
- return 255;
- }
- else if (x < 0)
- {
- return 0;
- }
- else
- {
- return (byte)x;
- }
- }
- public static byte ClampToByte(float x)
- {
- if (x > 255)
- {
- return 255;
- }
- else if (x < 0)
- {
- return 0;
- }
- else
- {
- return (byte)x;
- }
- }
- public static byte ClampToByte(int x)
- {
- if (x > 255)
- {
- return 255;
- }
- else if (x < 0)
- {
- return 0;
- }
- else
- {
- return (byte)x;
- }
- }
- public static float Lerp(float from, float to, float frac)
- {
- return (from + frac * (to - from));
- }
- public static double Lerp(double from, double to, double frac)
- {
- return (from + frac * (to - from));
- }
- public static void ShowHelp(Control parent,int type)
- {
- //string helpFileUrlFormat = PdnResources.GetString("HelpFile.Url.Format");
- //string baseSiteUrl = Application.StartupPath + @"\最新显微镜图像处理与分析系统 (修正版本)20210206.chm";//InvariantStrings.WebsiteUrl;
- //string helpFileUrl = baseSiteUrl;// string.Format(helpFileUrlFormat, baseSiteUrl);
- string filePath = Application.StartupPath + @"\最新显微镜图像处理与分析系统 (修正版本)20210206.chm";
- if (!System.IO.File.Exists(filePath))
- {
- return;
- }
- if (type == 1)
- {
- Help.ShowHelp(parent, filePath);
- }
- else {
- Help.ShowHelp(parent, filePath, HelpNavigator.Index);
- }
- //JScript\JScript
- //PdnInfo.OpenUrl(parent, helpFileUrl);
- }
- /// <summary>
- /// Reads a 16-bit unsigned integer from a Stream in little-endian format.
- /// </summary>
- /// <param name="stream"></param>
- /// <returns>-1 on failure, else the 16-bit unsigned integer that was read.</returns>
- public static int ReadUInt16(Stream stream)
- {
- int byte1 = stream.ReadByte();
- if (byte1 == -1)
- {
- return -1;
- }
- int byte2 = stream.ReadByte();
- if (byte2 == -1)
- {
- return -1;
- }
- return byte1 + (byte2 << 8);
- }
- public static void WriteUInt16(Stream stream, UInt16 word)
- {
- stream.WriteByte((byte)(word & 0xff));
- stream.WriteByte((byte)(word >> 8));
- }
- public static void WriteUInt24(Stream stream, int uint24)
- {
- stream.WriteByte((byte)(uint24 & 0xff));
- stream.WriteByte((byte)((uint24 >> 8) & 0xff));
- stream.WriteByte((byte)((uint24 >> 16) & 0xff));
- }
- public static void WriteUInt32(Stream stream, UInt32 uint32)
- {
- stream.WriteByte((byte)(uint32 & 0xff));
- stream.WriteByte((byte)((uint32 >> 8) & 0xff));
- stream.WriteByte((byte)((uint32 >> 16) & 0xff));
- stream.WriteByte((byte)((uint32 >> 24) & 0xff));
- }
- /// <summary>
- /// Reads a 24-bit unsigned integer from a Stream in little-endian format.
- /// </summary>
- /// <param name="stream"></param>
- /// <returns>-1 on failure, else the 24-bit unsigned integer that was read.</returns>
- public static int ReadUInt24(Stream stream)
- {
- int byte1 = stream.ReadByte();
- if (byte1 == -1)
- {
- return -1;
- }
- int byte2 = stream.ReadByte();
- if (byte2 == -1)
- {
- return -1;
- }
- int byte3 = stream.ReadByte();
- if (byte3 == -1)
- {
- return -1;
- }
- return byte1 + (byte2 << 8) + (byte3 << 16);
- }
- /// <summary>
- /// Reads a 32-bit unsigned integer from a Stream in little-endian format.
- /// </summary>
- /// <param name="stream"></param>
- /// <returns>-1 on failure, else the 32-bit unsigned integer that was read.</returns>
- public static long ReadUInt32(Stream stream)
- {
- int byte1 = stream.ReadByte();
- if (byte1 == -1)
- {
- return -1;
- }
- int byte2 = stream.ReadByte();
- if (byte2 == -1)
- {
- return -1;
- }
- int byte3 = stream.ReadByte();
- if (byte3 == -1)
- {
- return -1;
- }
- int byte4 = stream.ReadByte();
- if (byte4 == -1)
- {
- return -1;
- }
- return unchecked((long)((uint)(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24))));
- }
- public static int ReadFromStream(Stream input, byte[] buffer, int offset, int count)
- {
- int totalBytesRead = 0;
- while (totalBytesRead < count)
- {
- int bytesRead = input.Read(buffer, offset + totalBytesRead, count - totalBytesRead);
- if (bytesRead == 0)
- {
- throw new IOException("ran out of data");
- }
- totalBytesRead += bytesRead;
- }
- return totalBytesRead;
- }
- public static void Swap<T>(ref T a, ref T b)
- {
- T t;
- t = a;
- a = b;
- b = t;
- }
- public static byte FastScaleByteByByte(byte a, byte b)
- {
- int r1 = a * b + 0x80;
- int r2 = ((r1 >> 8) + r1) >> 8;
- return (byte)r2;
- }
- }
- }
|