AtelierElieScripter.ResourceObjects.JapFontResourceObject.GetTextBitmap C# (CSharp) Метод

GetTextBitmap() публичный Метод

Returns Bitmap of Text
public GetTextBitmap ( string text, int maxWidth, int noLines, int lineSpacing ) : Bitmap
text string Text to draw
maxWidth int Maximum Width
noLines int Lines to Draw
lineSpacing int Spacing between lines
Результат System.Drawing.Bitmap
        public Bitmap GetTextBitmap(string text, int maxWidth, int noLines, int lineSpacing)
        {
            Imaging.ImageAttributes attr = new Imaging.ImageAttributes();
            attr.SetColorKey(Color.White, Color.White);
            System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift-jis");

            Byte[] textBytes = (enc.GetBytes(text));

            int w = maxWidth;
            int h = noLines * 12 + (noLines - 1) * lineSpacing;

            Bitmap textBitmap = new Bitmap(w, h);
            int x = 0;
            int y = 0;

            using (Graphics g = Graphics.FromImage(textBitmap))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

                for (int i = 0; i < textBytes.Length; i += 2)
                {

                    uint textChar;
                    try
                    {
                        textChar = (uint)(textBytes[i] << 8) + textBytes[i+1];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        return textBitmap;
                    }

                    if (y > h)
                    {
                        break;
                    }
                    else if (textChar == (enc.GetBytes("¥")[0] << 8) + enc.GetBytes("¥")[1])
                    {
                        x = 0;
                        y += 12 + lineSpacing;
                    }
                    else
                    {
                        if (x >= maxWidth)
                        {
                            x = 0;
                            y += 12 + lineSpacing;
                        }

                        Rectangle drawRect = new Rectangle(x, y, 12, 12);
                        Bitmap bmp = GetChar(textChar);
                        g.DrawImage(bmp, drawRect, 0, 0, 12, 12, GraphicsUnit.Pixel, attr);

                        x += 12;

                    }
                }
            }

            return textBitmap;
        }