/// <summary>
        /// Create a new image for the picturebox
        /// </summary>
        private void UpdateImage()
        {
            if (!this.updateImage || this.tbxSampleText.Lines.Length == 0)
            {
                return;
            }

            using (Bitmap bmpFullSize = TextToImage.Convert(this.tbxSampleText.Text, this.tbxSampleText.Font, this.TextColor, this.BackgroundColor))
            {
                if (bmpFullSize == null)
                {
                    return;
                }

                float magnification = this.Value / 100f;

                Size newSize = new Size(
                    (int)(((float)bmpFullSize.Width * magnification) + 0.5f),
                    (int)(((float)bmpFullSize.Height * magnification) + 0.5f));

                this.pbxOutputImage.Image = new Bitmap(newSize.Width, newSize.Height);

                using (ImageAttributes ia = new ImageAttributes())
                {
                    ia.SetColorMatrix(JMSoftware.Matrices.Grayscale());

                    using (Graphics g = Graphics.FromImage(this.pbxOutputImage.Image))
                    {
                        // select highest quality resize mode
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                        g.DrawImage(
                            bmpFullSize,
                            new Rectangle(0, 0, newSize.Width, newSize.Height),
                            0,
                            0,
                            bmpFullSize.Width,
                            bmpFullSize.Height,
                            GraphicsUnit.Pixel,
                            ia);
                    }
                }
            }

            this.updateImage = false;

            this.pbxOutputImage.Refresh();
        }
示例#2
0
        /// <summary>
        /// Update Value for the passed font
        /// </summary>
        /// <param name="font">Font to be used</param>
        public void CalculateValue(Font font)
        {
            using (Bitmap bitmap = TextToImage.Convert(this.Character.ToString(), font))
            {
                if (bitmap == null)
                {
                    this.Value = -1;

                    return;
                }

                this.Width = bitmap.Width;

                int total = 0;

                unsafe
                {
                    BitmapData data = bitmap.LockBits(
                        new Rectangle(new Point(0, 0), bitmap.Size),
                        ImageLockMode.ReadOnly,
                        PixelFormat.Format24bppRgb);

                    byte *pointer = (byte *)data.Scan0;

                    int padding = data.Stride - (bitmap.Width * 3);

                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            total += (int)(((float)(pointer[2] + pointer[1] + pointer[0]) / 3f) + 0.5);

                            pointer += 3;
                        }

                        pointer += padding;
                    }

                    bitmap.UnlockBits(data);
                }

                this.Value = (int)(((float)total / (float)(this.Width * bitmap.Height)) + 0.5);
            }
        }
示例#3
0
        /// <summary>
        /// Calculate the values for the current character
        /// </summary>
        /// <param name="font">font to be used</param>
        public void CalculateValues(Font font)
        {
            // size of the shrunken character to use for the calculation
            const int   Width  = 4;
            const int   Height = 4;
            const float Area   = 16f;

            using (Bitmap bitmap = TextToImage.Convert(this.Character.ToString(), font))
            {
                using (Bitmap shrunk = new Bitmap(Width, Height))
                {
                    using (Graphics g = Graphics.FromImage(shrunk))
                    {
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                        g.Clear(Color.White);

                        g.DrawImage(bitmap, 0, 0, Width, Height);
                    }

                    unsafe
                    {
                        BitmapData data = shrunk.LockBits(
                            new Rectangle(new Point(0, 0), shrunk.Size),
                            ImageLockMode.ReadOnly,
                            PixelFormat.Format24bppRgb);

                        byte *pointer = (byte *)data.Scan0;

                        int padding = data.Stride - (shrunk.Width * 3);

                        int totalValue = 0;

                        for (int y = 0; y < shrunk.Height; y++)
                        {
                            for (int x = 0; x < shrunk.Width; x++)
                            {
                                totalValue += pointer[2];

                                pointer += 3;
                            }

                            pointer += padding;
                        }

                        // store the average value
                        this.Value = (int)(((float)totalValue / Area) + 0.5);

                        int totalDifference = 0;

                        pointer = (byte *)data.Scan0;

                        for (int y = 0; y < shrunk.Height; y++)
                        {
                            for (int x = 0; x < shrunk.Width; x++)
                            {
                                // add the difference between the average value and this pixel
                                totalDifference += this.Value > pointer[2] ? this.Value - pointer[2] : pointer[2] - this.Value;

                                pointer += 3;
                            }

                            pointer += padding;
                        }

                        // store the score for this character
                        this.Score = (int)(((float)totalDifference / Area) + 0.5);

                        shrunk.UnlockBits(data);
                    }
                }
            }
        }