예제 #1
0
        public void Cache_equally_generated_bitmaps()
        {
            using var sut = new IconCache <IconChar>();
            const IconChar iconChar = IconChar.AddressBook;
            var            font     = FormsIconHelper.FontFamilyFor(iconChar);
            var            bitmap1  = sut.Get(font, iconChar, 32, Color.Black, Color.Transparent);
            var            bitmap2  = sut.Get(font, iconChar, 32, Color.Black, Color.Transparent);

            bitmap2.Should().BeSameAs(bitmap1, "bitmaps with equal keys should not be recreated");
        }
예제 #2
0
        private async Task DrawKey(List <KeyStatus> keysList)
        {
            if (keysList == null || keysList.Count == 0)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"{this.GetType()} DrawKey: keysList is null!");
                return;
            }

            if (titleParameters == null)
            {
                Logger.Instance.LogMessage(TracingLevel.WARN, $"{this.GetType()} DrawKey: TitleParameters is null!");
                return;
            }

            const int KEY_POSITION_Y         = 25;
            const int KEY_PADDING_X          = 15;
            const int KEY_TEXT_SIZE_INCREASE = 5;
            const int ICON_SIZE_PIXELS       = 55;


            using (Bitmap bmp = Tools.GenerateGenericKeyImage(out Graphics graphics))
            {
                int height = bmp.Height;
                int width  = bmp.Width;

                // Draw background image
                if (backgroundImage != null)
                {
                    graphics.DrawImage(backgroundImage, 0, 0, width, height);
                }

                if (isLocked)
                {
                    using (Bitmap icon = FormsIconHelper.ToBitmap(IconChar.Lock, Color.Red, ICON_SIZE_PIXELS))
                    {
                        graphics.DrawImage(icon, new PointF(width / 2 - ICON_SIZE_PIXELS / 2, (int)height - ICON_SIZE_PIXELS));
                    }
                }

                int partWidth = width / keysList.Count;
                using (Font font = new Font(titleParameters.FontFamily, (float)titleParameters.FontSizeInPixels + KEY_TEXT_SIZE_INCREASE, FontStyle.Bold, GraphicsUnit.Pixel))
                {
                    for (int currKey = 0; currKey < keysList.Count; currKey++)
                    {
                        int startPos = (partWidth * currKey) + KEY_PADDING_X;
                        if (keysList[currKey] == null)
                        {
                            Logger.Instance.LogMessage(TracingLevel.ERROR, $"{this.GetType()} DrawKey: Invalid KeyStatus");
                            continue;
                        }

                        Color color = keysList[currKey].IsKeyLocked ? Color.Green : Color.White;
                        graphics.DrawString(keysList[currKey].Key.ToString().Substring(0, 1), font, new SolidBrush(color), new PointF(startPos, KEY_POSITION_Y));
                    }

                    await Connection.SetImageAsync(bmp);

                    graphics.Dispose();
                }
            }
        }