欢迎光临
我们一直在努力

将文本藏入图片 选择自 VirleneCheng 的 Blog-.NET教程,评论及其它

建站超值云服务器,限时71元/月

一般看来文字与图片是毫不相同的,但是它们却有共同点。图片是由一个个点组成的,而这些点的颜色值可由数字组成,文字可由ascii码表示,这就使得数字成为它们之间沟通道桥梁。因此就可以将文本藏入图片中。

这可以用visual basic 6.0实现,首先我们将文字转化为数字,再将图片中的每个点的rgb值取出,将数字每三个分别与r值,g值,b值相加或相减,接着把rgb值还原为图片中的点,至此我们已经将文本藏入图片。要取出文本怎么办呢?我们可以把源图片与目标图片进行对比,将到的差值转化为文本,就实现了文本的还原。

具体作法:先建立窗体文件frmpicturetext.frm和模块文件modpicturetext.bas

模块文件:

public declare function getobject lib "gdi32" alias "getobjecta" (byval _

hobject as long, byval ncount as long, lpobject as any) as long

用于获得图片的象素

public declare function getpixel lib "gdi32" (byval hdc as long, byval x _

as long, byval y as long) as long 用于获得图片指定点的rgb值

public type bitmap

bmtype as long

bmwidth as long

bmheight as long

bmwidthbytes as long

bmplanes as integer

bmbitspixel as integer

bmbits as long

end type

function hexdec(number as string) as integer 将十六进制转化为十进制

dim n as integer, dec as string, tmp as integer

for n = 1 to len(number)

dec = mid(number, n, 1)

if asc(dec) >= 65 then

dec = ucase(dec)

dec = format(asc(dec) – 55)

end if

tmp = val(dec) * 16 ^ (len(number) – n)

hexdec = hexdec + tmp

next n

end function

窗体文件:建立两个图片框:picsource用于显示源图片,picobject用于显示目标图片,

建立两个文本框:txtsource用于显示源文本,txtobject用于显示还原的文本,并设置为各多行显示,建立两个命令按钮:cmdtexttopicture用于把文本藏入图片,cmdpicturetotext用于还原文本。

private sub form_load()

picsource.autoredraw = true: picobject.autoredraw = true

picsource.autosize = true: picobject.autosize = true

picsource.picture = loadpicture("c:\test.bmp")

picobject.height = picsource.height 设置目标图片框的height和

picobject.width = picsource.width width属性与源图片相同,保证

目标图片的大小和源文件相同

end sub

private sub cmdtexttopicture_click()

dim numx as integer, chrtmp as string, numtmp as integer, numy as integer, word as string

dim soupixel as bitmap, soutop as integer, souleft as integer

dim soucolor as long, sougetcolor as string, numn as integer

dim tmpword as string, numdifred as integer, numdifgreen as integer, numdifblue as integer

dim newred as integer, newgreen as integer, newblue as integer

on error resume next

kill "c:\temp1.txt"

open "c:\temp1.txt" for append as #1 将文本转化为数字,并存入文件

for numx = 1 to len(txtsource.text)

numtmp = asc(mid(txtsource.text, numx, 1))

chrtmp = format(numtmp)

if numtmp >= 0 then chrtmp = "+" & chrtmp

for numy = 1 to len(chrtmp)

word = format(asc(mid(chrtmp, numy, 1)))

print #1, word;

next numy

next numx

close #1

open "c:\temp1.txt" for input as #2

getobject picsource.picture.handle, len(soupixel), soupixel

picobject.picture = nothing: picobject.cls

for soutop = 0 to soupixel.bmheight – 1

for souleft = 0 to soupixel.bmwidth – 1

取出图片各点的rgb值

soucolor = getpixel(picsource.hdc, souleft, soutop)

sougetcolor = hex(soucolor)

numn = 6 – len(sougetcolor)

sougetcolor = string(numn, "0") & sougetcolor

取出三个数字

if not (eof(2)) then

tmpword = input(3, #2)

numdifred = val(left(tmpword, 1))

numdifgreen = val(mid(tmpword, 2, 1))

numdifblue = val(right(tmpword, 1))

end if

把数字与r值,g值,b值相加或相减

newred = hexdec(right(sougetcolor, 2)) – numdifred

if newred < 0 then newred = hexdec(right(getcolor, 2)) + numdifred

newgreen = hexdec(mid(sougetcolor, 3, 2)) – numdifgreen

if newgreen < 0 then newgreen = hexdec(mid(sougetcolor, 3, 2)) + numdifgreen

newblue = hexdec(left(sougetcolor, 2)) – numdifblue

if newblue < 0 then newblue = hexdec(left(sougetcolor, 2)) + numdifblue

numdifred = 0: numdifgreen = 0: numdifblue = 0

doevents

形成目标图片

picobject.pset (souleft, soutop), rgb(newred, newgreen, newblue)

next souleft

next soutop

close #2

savepicture picobject.image, "c:\object.bmp"

picobject.picture = loadpicture("c:\object.bmp")

end sub

private sub cmdpicturetotext_click()

dim pixel as bitmap

dim soutop as integer, souleft as integer

dim soucolor as long, objcolor as long, sougetcolor as string, objgetcolor as string

dim soured as integer, sougreen as integer, soublue as integer

dim objred as integer, objgreen as integer, objblue as integer

dim soun as integer, objn as integer

dim numdifred as integer, chrdifred as string

dim numdifgreen as integer, chrdifgreen as string

dim numdifblue as integer, chrdifblue as string

dim difference as string, numtmp as integer, chrtmp as string, tmpword as string, word as string

on error resume next

getobject picsource.picture.handle, len(pixel), pixel 获取图片的象素

kill "c:\temp2.txt" 如果存在"temp2.txt"文件,则将它清除

open "c:\temp2.txt" for append as #3

for soutop = 0 to pixel.bmheight – 1

for souleft = 0 to pixel.bmwidth – 1

获得源图片各点的rgb值

soucolor = getpixel(picsource.hdc, souleft, soutop)

sougetcolor = hex(soucolor)

soun = 6 – len(sougetcolor)

sougetcolor = string(soun, "0") & sougetcolor

soured = hexdec(right(sougetcolor, 2)) 转化为red,green,blue的值

sougreen = hexdec(mid(sougetcolor, 3, 2))

soublue = hexdec(left(sougetcolor, 2))

获得目标图片各点的rgb值

objcolor = getpixel(picobject.hdc, souleft, soutop)

objgetcolor = hex(objcolor)

objn = 6 – len(objgetcolor)

objgetcolor = string(objn, "0") & objgetcolor

objred = hexdec(right(objgetcolor, 2))

objgreen = hexdec(mid(objgetcolor, 3, 2))

objblue = hexdec(left(objgetcolor, 2))

numdifred = soured – objred 将差值存入文件

chrdifred = format(numdifred)

if numdifred < 0 then chrdifred = format(objred – soured)

numdifgreen = sougreen – objgreen

chrdifgreen = format(numdifgreen)

if numdifgreen < 0 then chrdifgreen = format(objgreen – sougreen)

numdifblue = soublue – objblue

chrdifblue = format(numdifblue)

if numdifblue < 0 then chrdifblue = format(objblue – soublue)

difference = chrdifred & chrdifgreen & chrdifblue

print #3, difference;

next souleft

next soutop

close #3

open "c:\temp2.txt" for input as #4 从文件还原文字

do while not eof(4)

numtmp = input(2, #4)

chrtmp = chr(val(numtmp))

if (len(tmpword) > 1) and (chrtmp = "+" or chrtmp = "-") then

word = chr(val(tmpword))

txtobject.text = txtobject.text & word

tmpword = ""

end if

tmpword = tmpword & chrtmp

loop

txtobject.text = txtobject.text & chr(val(tmpword))

close #4

end sub

以上程序在windows98系统中vb6.0中调试通过。

综上所述,此方法对图片各点的rgb值的修改范围为0~9,很难区别目标图片与源图片,因此可以用于文件的加密。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 将文本藏入图片 选择自 VirleneCheng 的 Blog-.NET教程,评论及其它
分享到: 更多 (0)

相关推荐

  • 暂无文章