110,538
社区成员
发帖
与我相关
我的任务
分享
private string GetImageChars(Bitmap bmpCutImage)
{
StringBuilder chars = new StringBuilder();
for(int w = 0;w < bmpCutImage.Width;w++)
{
for(int h = 0;h < bmpCutImage.Height;h++)
{
Color color = bmpCutImage.GetPixel(w,h);
if(color.R == 255 && color.G == 255 && color.B = 255)
{
chars.Append("0");
}
else
{
chars.Append("1");
}
}
}
return chars.ToString();
}
得到切割图片的字符库.
private Bitmap GetCutImage(Bitmap bmpSource,int x,int y)
{
int width = y - x;
int height = bmpSource.Height;
Bitmap bitmap = new Bitmap(width,height);
Rectangle rectangle = new Rectangle(x,0,width,height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(bmpSource,0,0,rectangle,GraphicsUnit.Pixel);
g.Dispose();
return bitmap;
}
得到切割图:
bmpSource:原图
x:每个字符的left
y:每个字符的right(开始那个方法得到的.)
Bitmap bitmap = new Bitmap("图片路径");
bool flag =true;
string result = "";
for(int w = 0;w < bitmap.Width;w++)
{
int index = -1;
for(int h = 0;h<bitmap.Height;h++)
{
Color color = bitmap.GetPixel(w,h);
if(flag)
{
if(color.R != 255 || color.G != 255 || color.B != 255)
{
index = w;
flag = false;
break;
}
}
else
{
if(color.R == 255 && color.G == 255 && color.B == 255)
{
if(h == bitmap.Height - 1)
{
index = w;
flag = true;
break;
}
}
else
{
break;
}
}
}
if(index > -1)
{
result += index.ToString() + ";"
}
}
result中每两个就能切出一个字符.