コード例 #1
0
        public static Icon FromExtension(string fileOrExtension, SystemIconSize size)
        {
            if (fileOrExtension == null)
            {
                throw new ArgumentNullException(nameof(fileOrExtension), PublicResources.ArgumentNull);
            }
            if (!Enum <SystemIconSize> .IsDefined(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), PublicResources.EnumOutOfRangeWithValues(size));
            }
            if (!OSUtils.IsWindows)
            {
                return(SystemIcons.WinLogo);
            }

            if (!Path.HasExtension(fileOrExtension))
            {
                fileOrExtension = Path.GetFileName(fileOrExtension) == fileOrExtension ? '.' + fileOrExtension : ".";
            }

            IntPtr handle = Shell32.GetFileIconHandle(fileOrExtension, size);

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException(PublicResources.ArgumentInvalidString, nameof(fileOrExtension));
            }

            return(Icon.FromHandle(handle).ToManagedIcon());
        }
コード例 #2
0
ファイル: Settings.cs プロジェクト: pmiossec/NBug
        private static bool releaseMode;         // False by default

        static Settings()
        {
            // Crucial startup settings
            Resources     = new PublicResources();
            EntryAssembly = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()) ?? Assembly.GetCallingAssembly();

            // GetEntryAssembly() is null if there is no initial GUI/CLI
            NBugDirectory         = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) ?? Environment.CurrentDirectory;
            AdditionalReportFiles = new List <string>();

            // Default to developer mode settings. Settings this now so that any exception below will be handled with correct settings
            ReleaseMode = false;             // ToDo: This results initial config loading always setup to ThrowExceptions = true;

            // Check to see if the settings are overriden manually. If so, don't load the settings file automatically.
            if (SettingsOverride.Overridden == false)
            {
                /*
                 * Settings file search order:
                 * 1) NBug.config (inside the same folder with 'NBug.dll')
                 * 2) NBug.dll.config (fool proof!) (inside the same folder with 'NBug.dll')
                 * 3) app.config (i.e. MyProduct.exe.config inside the same folder with the main executable 'MyProduct.exe')
                 */
                var path1 = Path.Combine(NBugDirectory, "NBug.config");
                var path2 = Path.Combine(NBugDirectory, "NBug.dll.config");

                /*string path3; // This is automatically handled by System.Configuration*/
                if (File.Exists(path1))
                {
                    try
                    {
                        LoadCustomSettings(XElement.Load(path1));
                        Logger.Trace("Initialized NBug.Settings using the configuration file: " + path1);
                    }
                    catch (Exception exception)
                    {
                        // File is invalid so load default settings
                        LoadAppconfigSettings();
                        Logger.Error("Default configuration file was either corrupt or empty. Loading default app.config settings. File location: " + path1, exception);
                    }
                }
                else if (File.Exists(path2))
                {
                    try
                    {
                        LoadCustomSettings(XElement.Load(path2));
                        Logger.Trace("Initialized NBug.Settings using the configuration file: " + path2);
                    }
                    catch (Exception exception)
                    {
                        // File is invalid so load default settings
                        LoadAppconfigSettings();
                        Logger.Error("Default configuration file was either corrupt or empty. Loading default app.config settings. File location: " + path2, exception);
                    }
                }
                else
                {
                    LoadAppconfigSettings();
                }
            }
        }
コード例 #3
0
        public static Icon[] FromFile(string fileName, SystemIconSize size)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName), PublicResources.ArgumentNull);
            }
            if (!Enum <SystemIconSize> .IsDefined(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), PublicResources.EnumOutOfRangeWithValues(size));
            }
            if (!OSUtils.IsWindows)
            {
                Reflector.EmptyArray <Icon>();
            }

            IntPtr[][] handles = Shell32.ExtractIconHandles(fileName, size);
            Icon[]     result  = new Icon[handles.Length];

            for (int i = 0; i < handles.Length; i++)
            {
                result[i] = Icon.FromHandle(handles[i][0]).ToManagedIcon();
            }

            return(result);
        }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RandomNoiseDitherer"/> class.
 /// <br/>See the <strong>Examples</strong> section for some examples.
 /// </summary>
 /// <param name="strength">The strength of the dithering effect between 0 and 1 (inclusive bounds).
 /// Specify 0 to use an auto value for each dithering session based on the used quantizer.
 /// <br/>See the <strong>Remarks</strong> section of the <see cref="OrderedDitherer"/> class for more details and some examples regarding dithering strength.
 /// The same applies also for the <see cref="RandomNoiseDitherer"/> class. This parameter is optional.
 /// <br/>Default value: <c>0</c>.</param>
 /// <param name="seed">If <see langword="null"/>, then a <a href="https://docs.kgysoft.net/corelibraries/?topic=html/T_KGySoft_CoreLibraries_ThreadSafeRandom.htm" target="_blank">ThreadSafeRandom</a>
 /// instance will be used internally with a time-dependent seed value, and the dithering session will allow parallel processing.
 /// If not <see langword="null"/>, then a <see cref="Random"/> instance will be created for each dithering session with the specified <paramref name="seed"/>, and the dithering session will not allow parallel processing.</param>
 /// <example>
 /// The following example demonstrates how to use the <see cref="RandomNoiseDitherer"/> class.
 /// <code lang="C#"><![CDATA[
 /// public static Bitmap ToDitheredRandomNoise(Bitmap source, IQuantizer quantizer)
 /// {
 ///     IDitherer ditherer = new RandomNoiseDitherer();
 ///
 ///     // a.) this solution returns a new bitmap and does not change the original one:
 ///     return source.ConvertPixelFormat(quantizer.PixelFormatHint, quantizer, ditherer);
 ///
 ///     // b.) alternatively, you can perform the dithering directly on the source bitmap:
 ///     source.Dither(quantizer, ditherer);
 ///     return source;
 /// }]]></code>
 /// <para>The example above may produce the following results:
 /// <list type="table">
 /// <listheader><term>Original image</term><term>Quantized and dithered image</term></listheader>
 /// <item>
 /// <term><div style="text-align:center;width:512px">
 /// <para><img src="../Help/Images/AlphaGradient.png" alt="Color hues with alpha gradient"/>
 /// <br/>Color hues with alpha gradient</para></div></term>
 /// <term>
 /// <div style="text-align:center;width:512px">
 /// <para><img src="../Help/Images/AlphaGradientDefault8bppSilverDitheredRN.gif" alt="Color hues with system default 8 BPP palette, using silver background and random noise dithering"/>
 /// <br/>Quantizing with <see cref="PredefinedColorsQuantizer.SystemDefault8BppPalette">system default 8 BPP palette</see></para></div></term>
 /// </item>
 /// <item>
 /// <term><div style="text-align:center;width:512px">
 /// <para><img src="../Help/Images/GrayShades.gif" alt="Grayscale color shades with different bit depths"/>
 /// <br/>Grayscale color shades</para></div></term>
 /// <term>
 /// <div style="text-align:center;width:512px">
 /// <para><img src="../Help/Images/GrayShadesBWDitheredRN.gif" alt="Grayscale color shades with black and white palette using random noise dithering"/>
 /// <br/>Quantizing with <see cref="PredefinedColorsQuantizer.BlackAndWhite">black and white palette</see></para></div></term>
 /// </item>
 /// </list></para>
 /// </example>
 public RandomNoiseDitherer(float strength = 0f, int?seed = null)
 {
     if (Single.IsNaN(strength) || strength < 0f || strength > 1f)
     {
         throw new ArgumentOutOfRangeException(nameof(strength), PublicResources.ArgumentMustBeBetween(0, 1));
     }
     this.strength = strength;
     this.seed     = seed;
 }
コード例 #5
0
 private OptimizedPaletteQuantizer(Algorithm algorithm, int maxColors, Color backColor, byte alphaThreshold)
 {
     if (maxColors < 2 || maxColors > 256)
     {
         throw new ArgumentOutOfRangeException(nameof(maxColors), PublicResources.ArgumentMustBeBetween(2, 256));
     }
     this.algorithm      = algorithm;
     this.maxColors      = maxColors;
     this.backColor      = new Color32(backColor).ToOpaque();
     this.alphaThreshold = alphaThreshold;
 }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DrawingProgress"/> struct.
        /// </summary>
        /// <param name="operationType">Specifies the type of the drawing operation.</param>
        /// <param name="maximumValue">The maximum value.</param>
        /// <param name="currentValue">The current value.</param>
        public DrawingProgress(DrawingOperation operationType, int maximumValue, int currentValue)
        {
            if (!operationType.IsDefined())
            {
                throw new ArgumentOutOfRangeException(nameof(operationType), PublicResources.EnumOutOfRange(operationType));
            }
            if (maximumValue < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maximumValue), PublicResources.ArgumentMustBeGreaterThanOrEqualTo(0));
            }
            if ((uint)currentValue > (uint)maximumValue)
            {
                throw new ArgumentOutOfRangeException(nameof(currentValue), PublicResources.ArgumentMustBeBetween(0, maximumValue));
            }

            OperationType = operationType;
            MaximumValue  = maximumValue;
            CurrentValue  = currentValue;
        }
コード例 #7
0
        /// <summary>
        /// Creates a native <see cref="IBitmapDataInternal"/> by a quantizer session re-using its palette if possible.
        /// </summary>
        internal static IBitmapDataInternal CreateBitmapData(Bitmap bitmap, ImageLockMode lockMode, IQuantizingSession quantizingSession)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException(nameof(bitmap), PublicResources.ArgumentNull);
            }

            var pixelFormat = bitmap.PixelFormat;

            if (!pixelFormat.IsIndexed() || quantizingSession.Palette == null)
            {
                return(CreateBitmapData(bitmap, lockMode, quantizingSession.BackColor, quantizingSession.AlphaThreshold));
            }

            // checking if bitmap and quantizer palette has the same entries
            if (!quantizingSession.Palette.Equals(bitmap.Palette.Entries))
            {
                return(CreateBitmapData(bitmap, lockMode, quantizingSession.BackColor, quantizingSession.AlphaThreshold));
            }

            if (!lockMode.IsDefined())
            {
                throw new ArgumentOutOfRangeException(nameof(lockMode), PublicResources.EnumOutOfRange(lockMode));
            }

            // here the quantizer and the target bitmap uses the same palette
            switch (pixelFormat)
            {
            case PixelFormat.Format8bppIndexed:
                return(new NativeBitmapData <NativeBitmapDataRow8I>(bitmap, pixelFormat, lockMode, quantizingSession));

            case PixelFormat.Format4bppIndexed:
                return(new NativeBitmapData <NativeBitmapDataRow4I>(bitmap, pixelFormat, lockMode, quantizingSession));

            case PixelFormat.Format1bppIndexed:
                return(new NativeBitmapData <NativeBitmapDataRow1I>(bitmap, pixelFormat, lockMode, quantizingSession));

            default:
                throw new InvalidOperationException(Res.InternalError($"Unexpected indexed format: {pixelFormat}"));
            }
        }
コード例 #8
0
        /// <summary>
        /// Creates a native <see cref="IBitmapDataInternal"/> from a <see cref="Bitmap"/>.
        /// </summary>
        internal static IBitmapDataInternal CreateBitmapData(Bitmap bitmap, ImageLockMode lockMode, Color32 backColor = default, byte alphaThreshold = 128, Palette?palette = null)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException(nameof(bitmap), PublicResources.ArgumentNull);
            }
            if (!lockMode.IsDefined())
            {
                throw new ArgumentOutOfRangeException(nameof(lockMode), PublicResources.EnumOutOfRange(lockMode));
            }
            Debug.Assert(palette == null || backColor.ToOpaque() == palette.BackColor && alphaThreshold == palette.AlphaThreshold);

            var pixelFormat = bitmap.PixelFormat;

            switch (pixelFormat)
            {
            case PixelFormat.Format32bppArgb:
                return(new NativeBitmapData <NativeBitmapDataRow32Argb>(bitmap, pixelFormat, lockMode));

            case PixelFormat.Format32bppPArgb:
                return(new NativeBitmapData <NativeBitmapDataRow32PArgb>(bitmap, pixelFormat, lockMode));

            case PixelFormat.Format32bppRgb:
                return(new NativeBitmapData <NativeBitmapDataRow32Rgb>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold));

            case PixelFormat.Format24bppRgb:
                return(new NativeBitmapData <NativeBitmapDataRow24Rgb>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold));

            case PixelFormat.Format8bppIndexed:
                return(new NativeBitmapData <NativeBitmapDataRow8I>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold, palette));

            case PixelFormat.Format4bppIndexed:
                return(new NativeBitmapData <NativeBitmapDataRow4I>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold, palette));

            case PixelFormat.Format1bppIndexed:
                return(new NativeBitmapData <NativeBitmapDataRow1I>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold, palette));

            case PixelFormat.Format64bppArgb:
                return(new NativeBitmapData <NativeBitmapDataRow64Argb>(bitmap, pixelFormat, lockMode));

            case PixelFormat.Format64bppPArgb:
                return(new NativeBitmapData <NativeBitmapDataRow64PArgb>(bitmap, pixelFormat, lockMode));

            case PixelFormat.Format48bppRgb:
                return(new NativeBitmapData <NativeBitmapDataRow48Rgb>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold));

            case PixelFormat.Format16bppRgb565:
                return(OSUtils.IsWindows
                        ? new NativeBitmapData <NativeBitmapDataRow16Rgb565>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)
                        : new NativeBitmapData <NativeBitmapDataRow16Rgb565Via24Bpp>(bitmap, PixelFormat.Format24bppRgb, lockMode, backColor, alphaThreshold));

            case PixelFormat.Format16bppRgb555:
                return(OSUtils.IsWindows
                        ? new NativeBitmapData <NativeBitmapDataRow16Rgb555>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)
                        : new NativeBitmapData <NativeBitmapDataRow16Rgb555Via24Bpp>(bitmap, PixelFormat.Format24bppRgb, lockMode, backColor, alphaThreshold));

            case PixelFormat.Format16bppArgb1555:
                return(new NativeBitmapData <NativeBitmapDataRow16Argb1555>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold));

            case PixelFormat.Format16bppGrayScale:
                return(new NativeBitmapData <NativeBitmapDataRow16Gray>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold));

            default:
                throw new InvalidOperationException(Res.InternalError($"Unexpected pixel format {pixelFormat}"));
            }
        }