koobin
我的第一个C++技术Blog
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
2 随笔 :: 1 文章 :: 0 评论 :: 0 Trackbacks
<
2025年2月
>
日
一
二
三
四
五
六
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5
6
7
8
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔档案
2008年8月 (1)
2007年1月 (1)
文章档案
2007年1月 (1)
相册
软件截图
相机
搜索
最新评论
阅读排行榜
1. 函数表达式计算器(388)
2. C++转型操作符(260)
评论排行榜
1. C++转型操作符(0)
2. 函数表达式计算器(0)
一个C#写的bmp图片转txt的程序
1
using
System;
2
using
System.IO;
3
using
System.Collections;
4
using
System.Collections.Generic;
5
using
System.Drawing;
6
using
System.Text;
7
8
namespace
Bmp2Txt
9
{
10
class
Program
11
{
12
static
void
Main(
string
[] args)
13
{
14
int
W
=
4
, H
=
8
;
//
Define the word's height and width
15
16
//
if (args.Length < 2)
17
//
{
18
//
Console.WriteLine("Param must be 2");
19
//
return;
20
//
}
21
string
imageFilePath
=
@"
D:\My Documents\My Pictures\AMD64.jpg
"
;
22
string
textFilePath
=
"
c:\\bmp2txt.txt
"
;
23
24
//
imageFilePath = args[0];
25
//
textFilePath = args[1];
26
27
byte
tchar;
28
int
i, j, k, h, TransWidth, TransHeight, tint;
29
byte
[] ch
=
new
byte
[
95
]
{
30
(
byte
)
'
'
,
31
(
byte
)
'
`
'
,(
byte
)
'
1
'
,(
byte
)
'
2
'
,(
byte
)
'
3
'
,(
byte
)
'
4
'
,(
byte
)
'
5
'
,(
byte
)
'
6
'
,(
byte
)
'
7
'
,(
byte
)
'
8
'
,(
byte
)
'
9
'
,(
byte
)
'
0
'
,(
byte
)
'
-
'
,(
byte
)
'
=
'
,(
byte
)
'
\\
'
,
32
(
byte
)
'
q
'
,(
byte
)
'
w
'
,(
byte
)
'
e
'
,(
byte
)
'
r
'
,(
byte
)
'
t
'
,(
byte
)
'
y
'
,(
byte
)
'
u
'
,(
byte
)
'
i
'
,(
byte
)
'
o
'
,(
byte
)
'
p
'
,(
byte
)
'
[
'
,(
byte
)
'
]
'
,
33
(
byte
)
'
a
'
,(
byte
)
'
s
'
,(
byte
)
'
d
'
,(
byte
)
'
f
'
,(
byte
)
'
g
'
,(
byte
)
'
h
'
,(
byte
)
'
j
'
,(
byte
)
'
k
'
,(
byte
)
'
l
'
,(
byte
)
'
;
'
,(
byte
)
'
\
''
,
34
(
byte
)
'
z
'
,(
byte
)
'
x
'
,(
byte
)
'
c
'
,(
byte
)
'
v
'
,(
byte
)
'
b
'
,(
byte
)
'
n
'
,(
byte
)
'
m
'
,(
byte
)
'
,
'
,(
byte
)
'
.
'
,(
byte
)
'
/
'
,
35
(
byte
)
'
~
'
,(
byte
)
'
!
'
,(
byte
)
'
@
'
,(
byte
)
'
#
'
,(
byte
)
'
$
'
,(
byte
)
'
%
'
,(
byte
)
'
^
'
,(
byte
)
'
&
'
,(
byte
)
'
*
'
,(
byte
)
'
(
'
,(
byte
)
'
)
'
,(
byte
)
'
_
'
,(
byte
)
'
+
'
,(
byte
)
'
|
'
,
36
(
byte
)
'
Q
'
,(
byte
)
'
W
'
,(
byte
)
'
E
'
,(
byte
)
'
R
'
,(
byte
)
'
T
'
,(
byte
)
'
Y
'
,(
byte
)
'
U
'
,(
byte
)
'
I
'
,(
byte
)
'
O
'
,(
byte
)
'
P
'
,(
byte
)
'
{
'
,(
byte
)
'
}
'
,
37
(
byte
)
'
A
'
,(
byte
)
'
S
'
,(
byte
)
'
D
'
,(
byte
)
'
F
'
,(
byte
)
'
G
'
,(
byte
)
'
H
'
,(
byte
)
'
J
'
,(
byte
)
'
K
'
,(
byte
)
'
L
'
,(
byte
)
'
:
'
,(
byte
)
'
"
'
,
38
(
byte
)
'
Z
'
,(
byte
)
'
X
'
,(
byte
)
'
C
'
,(
byte
)
'
V
'
,(
byte
)
'
B
'
,(
byte
)
'
N
'
,(
byte
)
'
M
'
,(
byte
)
'
<
'
,(
byte
)
'
>
'
,(
byte
)
'
?
'
39
}
;
40
int
[] gr
=
new
int
[
95
]
{
41
0
,
42
7
,
22
,
28
,
31
,
31
,
27
,
32
,
22
,
38
,
32
,
40
,
6
,
12
,
20
,
38
,
32
,
26
,
20
,
24
,
40
,
43
29
,
24
,
28
,
38
,
32
,
32
,
26
,
22
,
34
,
24
,
44
,
33
,
32
,
32
,
24
,
16
,
6
,
22
,
26
,
22
,
44
26
,
34
,
29
,
35
,
10
,
6
,
20
,
14
,
22
,
47
,
42
,
34
,
40
,
10
,
35
,
21
,
22
,
22
,
16
,
14
,
45
26
,
40
,
39
,
29
,
38
,
22
,
28
,
36
,
22
,
36
,
30
,
22
,
22
,
36
,
26
,
36
,
25
,
34
,
38
,
24
,
46
36
,
22
,
12
,
12
,
26
,
30
,
30
,
34
,
39
,
42
,
41
,
18
,
18
,
22
47
}
;
48
//
先用起泡排序,将灰度值按从小到大排列,同时调整对应的字符位置
49
for
(i
=
0
; i
<
94
; i
++
)
50
for
(j
=
i
+
1
; j
<
95
; j
++
)
51
{
52
if
(gr[i]
>
gr[j])
53
{
54
tchar
=
ch[i]; tint
=
gr[i];
55
ch[i]
=
ch[j]; gr[i]
=
gr[j];
56
ch[j]
=
tchar; gr[j]
=
tint;
57
}
58
}
59
60
byte
[] rgbValues;
61
int
grayindex, LineBytes;
62
63
//
Create a new bitmap.
64
Bitmap bmp
=
new
Bitmap(imageFilePath);
65
66
//
Lock the bitmap's bits.
67
Rectangle rect
=
new
Rectangle(
0
,
0
, bmp.Width, bmp.Height);
68
System.Drawing.Imaging.BitmapData bmpData
=
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
69
70
//
Get the address of the first line.
71
IntPtr ptr
=
bmpData.Scan0;
72
73
//
Get the bitmap file's Bytes in a line
74
LineBytes
=
bmpData.Stride;
75
76
//
Declare an array to hold the bytes of the bitmap.
77
//
This code is specific to a bitmap with 24 bits per pixels.
78
int
bytes
=
bmp.Width
*
bmp.Height
*
3
;
79
rgbValues
=
new
byte
[bytes];
80
81
//
Copy the RGB values into the array.
82
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues,
0
, bytes);
83
84
//
Unlock the bits.
85
bmp.UnlockBits(bmpData);
86
87
TransWidth
=
bmp.Width
/
W;
//
每行字符的个数
88
TransHeight
=
bmp.Height
/
H;
//
共有多少行字符
89
90
//
Create a text file to output bitmap text
91
FileStream hf
=
new
FileStream(textFilePath, FileMode.Create, FileAccess.Write);
92
93
for
(i
=
0
; i
<
TransHeight; i
++
)
94
{
95
for
(j
=
0
; j
<
TransWidth; j
++
)
96
{
97
grayindex
=
0
;
98
for
(k
=
0
; k
<
H; k
++
)
99
{
100
for
(h
=
0
; h
<
W; h
++
)
101
{
102
//
求出8*16小块中各象素灰度之和
103
for
(
int
l
=
0
; l
<
3
; l
++
)
104
{
105
grayindex
+=
rgbValues[(i
*
H
+
k)
*
LineBytes
+
h
+
j
*
W
*
3
+
l];
106
}
107
}
108
}
109
grayindex
/=
W
*
H
*
3
;
//
除以整个面积
110
grayindex
=
gr[
94
]
*
grayindex
/
255
;
111
k
=
0
;
112
while
(gr[k
+
1
]
<
grayindex)
113
k
++
;
//
寻找灰度最接近的字符
114
hf.WriteByte(ch[k]);
//
将该字符写入文件中
115
}
116
//
每行加一个回车换行符
117
hf.WriteByte((
byte
)
'
\r
'
);
118
hf.WriteByte((
byte
)
'
\n
'
);
119
}
120
hf.Close();
121
bmp.Dispose();
122
Console.WriteLine(
"
Transform succesful
"
);
123
124
}
//
main
125
126
}
127
}
128
129
posted on 2007-01-18 19:17
koobin
阅读(1954)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
网站导航:
博客园
IT新闻
BlogJava
博问
Chat2DB
管理
Powered by:
C++博客
Copyright © koobin