Oto kod
private void saykaPanel_Paint(object sender, PaintEventArgs e)
{
string saykaName = "Sayka";
Font usedFont = new Font("Tahoma", 46);
int lastPoint = 0;
for (int i = 0; i < saykaName.Length; i++)
{
Rectangle rect = measureAlphabet(saykaName[i], usedFont);
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(lastPoint + rect.Left, rect.Top, rect.Width, rect.Height));
e.Graphics.DrawString(saykaName[i].ToString(), usedFont, Brushes.Black, new PointF(lastPoint, 0));
lastPoint += rect.Width;
}
}
Rectangle measureAlphabet(char alph, Font font)
{
SizeF size = CreateGraphics().MeasureString(alph.ToString(), font);
Bitmap bmp = new Bitmap((int)size.Width, (int)size.Height);
Graphics grp = Graphics.FromImage(bmp);
grp.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
grp.DrawString(alph.ToString(), font, Brushes.Black, new PointF(0, 0));
Bitmap img = (Bitmap)bmp;
int x = 0, y = 0, width = 0, height = 0;
for (int i = 0; i < img.Width;i++)
for (int j = 0; j < img.Height;j++)
if (img.GetPixel(i, j).B < 2)
{
x = i;
goto jmp1;
}
jmp1: ;
for (int i = img.Width - 1; i >= 0; i--)
for (int j = 0; j < img.Height; j++)
if (img.GetPixel(i, j).B < 2)
{
width = i - x;
goto jmp2;
}
jmp2: ;
for (int i = 0; i < img.Height; i++)
for (int j = 0; j < img.Width; j++)
if (img.GetPixel(j, i).B < 2)
{
y = i;
goto jmp3;
}
jmp3: ;
for (int i = img.Height - 1; i >= 0; i--)
for (int j = 0; j < img.Width; j++)
if (img.GetPixel(j, i).B < 2)
{
height = i - y;
goto jmp4;
}
jmp4: ;
Rectangle resultRectangle = new Rectangle(x, y, width, height);
return resultRectangle;
}
. .
.
.
.
.
Mierzona pierwszy czarny punkt od lewej, a następnie w prawo, potem frm frm i frm dno .. Oceń, czy to pomaga ..
gdzie jest malowany twój "znak"? Mam nadzieję, że nie jest to postać w "RichTextBox" lub "TextBox", ponieważ nie jest łatwo w ogóle radzić sobie z postaciami w polach tekstowych. –
Zobacz http://www.codeproject.com/Articles/2118/Bypass-Graphics-MeasureString-limitations. Wygląda na to, że szukasz 'graphics.MeasureCharacterRanges'? –
Znalazłem ten artykuł, ale MeasureCharacterRanges() nie zmienia się wraz z wysokością dla każdego znaku w łańcuchu - daje to całą wysokość linii dla każdego. – powlette