我试图绘制BMB图像并且我测量我想要绘制的每个角色的边界框但是什么都没有发生什么是错的呢?
这是绘制文本的代码
private void button2_Click(object sender, EventArgs e)
{
Bitmap bitmap = (Bitmap)Image.FromFile(imagepath);//load the image file
Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
PointF GLocation = new PointF(float.Parse(textBox1.Text), float.Parse(textBox2.Text));
/* PointF XLocation = new PointF(float.Parse(textBox3.Text), float.Parse(textBox4.Text));
PointF HLocation = new PointF(float.Parse(textBox5.Text), float.Parse(textBox6.Text));
PointF ZLocation = new PointF(float.Parse(textBox7.Text), float.Parse(textBox8.Text));
PointF YLocation = new PointF(float.Parse(textBox9.Text), float.Parse(textBox10.Text));
PointF KLocation = new PointF(float.Parse(textBox11.Text), float.Parse(textBox12.Text));
PointF FLocation = new PointF(float.Parse(textBox13.Text), float.Parse(textBox14.Text));
PointF ELocation = new PointF(float.Parse(textBox15.Text), float.Parse(textBox16.Text));*/
Graphics grPhoto = Graphics.FromImage(newBitmap);
grPhoto.DrawImage(bitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, newBitmap.Width, newBitmap.Height, GraphicsUnit.Pixel);
//MessageBox.Show(measureAlphabet('G', arial).ToString());
int x = (int)measureAlphabet('G', arial).Width;
int y = (int)measureAlphabet('G', arial).Height;
Rectangle rec = measureAlphabet('G', arial);
for (int G = int.Parse(textBox1.Text); G <= G + x; G++)
{
for (G = int.Parse(textBox2.Text); G <= G + y; G++)
{
Color pixelcolor = newBitmap.GetPixel(x,y);
if (pixelcolor.R != 255 && pixelcolor.G != 255 && pixelcolor.B != 255)
{
using (Graphics graphics = Graphics.FromImage(newBitmap))
{
using (arial)
{
graphics.DrawString(firstchar, arial, Brushes.Blue, GLocation);
}
}
}
}
}
/* using (Graphics graphics = Graphics.FromImage(newBitmap))
{
using (arial)
{
graphics.DrawString(secondchar, arial, Brushes.Red, XLocation);
graphics.DrawString(thirdchar, arial, Brushes.Brown, HLocation);
graphics.DrawString(fourthchar, arial, Brushes.DarkCyan, ZLocation);
graphics.DrawString(fifthchar, arial, Brushes.DarkGreen, YLocation);
graphics.DrawString(sixthchar, arial, Brushes.Coral, KLocation);
graphics.DrawString(seventhchar, arial, Brushes.Salmon, FLocation);
graphics.DrawString(eighthchar, arial, Brushes.SeaGreen, ELocation);
}
}*/
bitmap.Dispose();
// newBitmap.Dispose();
newBitmap.Save(imagepath);
}
测量边界框的方法
public 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;
}