Beispiel #1
0
        }     //Label_UPCA

        public static int getFontsize(Barcode barcode, int wid, int hgt, string lbl)
        {
            //Returns the optimal font size for the specified dimensions
            int fontSize = 10;

            if (lbl.Length > 0)
            {
                Image fakeImage = barcode.CreateBitmap(1, 1); //As we cannot use CreateGraphics() in a class library, so the fake image is used to load the Graphics.

                // Make a Graphics object to measure the text.
                using (Graphics gr = Graphics.FromImage(fakeImage))
                {
                    for (int i = 1; i <= 100; i++)
                    {
                        using (Font test_font = new Font("Arial", i * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel))
                        {
                            // See how much space the text would
                            // need, specifying a maximum width.
                            SizeF text_size = gr.MeasureString(lbl, test_font);
                            if ((text_size.Width > wid) || (text_size.Height > hgt))
                            {
                                fontSize = i - 1;
                                break;
                            }
                        }
                    }
                }
            }
            ;

            return(fontSize);
        }