您的位置:程序门 -> vb -> 网络编程



为什么我的程序中的局部马赛克无法实现!~请高手指点!急用!~~谢


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


为什么我的程序中的局部马赛克无法实现!~请高手指点!急用!~~谢
发表于:2008-01-12 22:47:15 楼主
im   mark   as   boolean     '定义i,   mark为窗体级布尔型变量

'mark为是否进行局部马赛克标记

dim   mx   as   integer,   my   as   integer     '存放鼠标在屏幕上的当前坐标

dim   color   as   long     '定义color为窗体级长整型变量

dim   r   as   integer,   g   as   integer,   b   as   integer     'r,g,b为三原色值

dim   starttime   as   date,   endtime   as   date,   spendtime   as   date

private   declare   function   setpixel   lib   "gdi32"   (byval   hdc   as   long,   byval   x   as   long,   byval   y   as   long,   byval   crcolor   as   long)   as   long

private   declare   function   getpixel   lib   "gdi32"   (byval   hdc   as   long,   byval   x   as   long,   byval   y   as   long)   as   long

 

private   sub   command1_click()   '打开文件

commondialog1.filter   =   "所有文件 ¦*.* ¦jpeg文件 ¦*.jpg ¦bmp文件 ¦*.bmp ¦gif文件 ¦*.gif ¦ico文件 ¦*.ico ¦wmf文件 ¦*.wmf ¦dib文件 ¦*.dib ¦cur文件 ¦*.cur"

'设置所选文件类型

commondialog1.dialogtitle   =   "打开"     '将通用对话框标题设置为'打开'

commondialog1.filename   =   ""     '将通用对话框的文件名置空

commondialog1.showopen     '打开“打开文件”通用对话框

if   commondialog1.filename   <>   ""   then

  choicedfile   =   commondialog1.filename     '文件名放入choicedfile变量

  picture1.picture   =   loadpicture(choicedfile)     '在picture1中装入指定的图象

  clipboard.clear     '将剪贴板清空

  clipboard.setdata   picture1.picture     '将指定的图象放入剪贴板

  '装入图象放在picture1,clipboard

else

  exit   sub

end   if

picture1.autosize   =   true

'将picture1设置为可自动调整大小以适应图象的大小

hscroll1.max   =   form1.picture1.width   -   form1.frame1.width

vscroll1.max   =   form1.picture1.height   -   form1.frame1.height   +   2567

'将滚动条与图象进行关联

end   sub

private   sub   command6_click()     '退出程序

clipboard.clear     '将剪贴板清空

end

end   sub

private   sub   command3_click()

mousepointer   =   11     '将鼠标指针设置为沙漏形状

form1.picture1.autoredraw   =   true

mosaic   picture1     '调用马赛克函数

form1.picture1.autoredraw   =   false

mousepointer   =   1     '将鼠标指针设置为箭头形状

end   sub

public   function   mosaic(pic   as   picturebox)     '马赛克函数

dim   row   as   integer,   lin   as   integer

dim   rl   as   integer,   ll   as   integer

dim   xl   as   integer,   yl   as   integer

dim   k   as   integer,   j   as   integer

dim   x   as   integer,   y   as   integer

'row为马赛克块列数-1,lin为马赛克块行数-1,rl为所余块中的列数,ll为所余块中的行数

dim   color   as   long

dim   r   as   integer,   g   as   integer,   b   as   integer

starttime   =   time

row   =   int(pic.scalewidth   /   10)

lin   =   int(pic.scaleheight   /   10)

rl   =   pic.scalewidth   mod   10

ll   =   pic.scaleheight   mod   10

for   y   =   0   to   (lin   -   1)   *   10   step   10

  for   x   =   0   to   (row   -   1)   *   10   step   10

    color   =   getpixel(pic.hdc,   x   +   5,   y   +   5)

    r   =   (color   mod   256)

    b   =   (int(color   /   65536))

    g   =   int((color   -   (b   *   65536)   -   r)   /   256)

    for   k   =   0   to   9

      for   j   =   0   to   9

        setpixel   pic.hdc,   x   +   k,   y   +   j,   rgb(abs(r),   abs(g),   abs(b))

      next   j

    next   k

      pic.refresh

      next   x

  if   rl   <>   0   then

    xl   =   pic.scalewidth   -   rl

    color   =   getpixel(pic.hdc,   xl   +   rl   /   2,   y   +   5)

    r   =   (color   mod   256)

    b   =   (int(color   /   65536))

    g   =   int((color   -   (b   *   65536)   -   r)   /   256)

    for   k   =   0   to   rl   -   1

      for   j   =   0   to   9

        setpixel   pic.hdc,   xl   +   k,   y   +   j,   rgb(abs(r),   abs(g),   abs(b))

      next   j

    next   k

    pic.refresh

  end   if

next   y

if   ll   <>   0   then

  yl   =   pic.scaleheight   -   ll

  for   x   =   0   to   (row   -   1)   *   10   step   10

    color   =   getpixel(pic.hdc,   x   +   5,   yl   +   ll   /   2)

    r   =   (color   mod   256)

    b   =   (int(color   /   65536))

    g   =   int((color   -   (b   *   65536)   -   r)   /   256)

    for   k   =   0   to   9

      for   j   =   0   to   ll   -   1

        setpixel   pic.hdc,   x   +   k,   y   +   j,   rgb(abs(r),   abs(g),   abs(b))


      next   j

    next   k

  pic.refresh

  next   x

  if   rl   <>   0   then

    color   =   getpixel(pic.hdc,   xl   +   rl   /   2,   yl   +   ll   /   2)

    r   =   (color   mod   256)

    b   =   (int(color   /   65536))

    g   =   int((color   -   (b   *   65536)   -   r)   /   256)

    for   k   =   0   to   rl   -   1

      for   j   =   0   to   ll   -   1

        setpixel   pic.hdc,   x   +   k,   y   +   j,   rgb(abs(r),   abs(g),   abs(b))


      next   j

    next   k

    pic.refresh

  end   if

end   if

endtime   =   time

spendtime   =   endtime   -   starttime

end   function

private   sub   command2_click()     '保存文件

commondialog1.filter   =   "bmp文件 ¦*.bmp"     '设置保存文件的类型

commondialog1.dialogtitle   =   "保存文件"

'将通用对话框标题设置为'保存文件'

commondialog1.filename   =   ""     '将通用对话框的文件名置空

commondialog1.showsave     '打开“保存文件”对话框

commondialog1.defaultext   =   bmp     '设置缺省的文件扩展名为bmp

if   commondialog1.filename   <>   ""   then

  choicedfile   =   commondialog1.filename

  savepicture   picture1.image,   choicedfile     '按输入的文件名保存文件

else

  exit   sub

end   if

end   sub

private   sub   command4_click()     '局部马赛克

if   mark   =   false   then

  mark   =   true

  'mark为进行局部马赛克的标记,为真进行局部马赛克处理,为假则不处理

  picture1.autoredraw   =   true

else

  mark   =   false     '关闭局部马赛克功能

  picture1.autoredraw   =   false

end   if

end   sub

private   sub   picture1_click()     '在图象中单击鼠标处进行局部马赛克

dim   xl   as   integer,   yl   as   integer

dim   k   as   integer,   j   as   integer

if   mark   =   true   then     'mark   =   true则允许进行局部马赛克

if   picture1.scalewidth   -   1   -   mx   > =   30   and   picture1.scaleheight   -   1   -   my   > =   30   then

'检测当前鼠标的位置,防止处理边缘溢出

for   yl   =   my   to   my   +   20   step   10     '局部马赛克处理

  for   xl   =   mx   to   mx   +   20   step   10

    color   =   getpixel(picture1.hdc,   xl   +   5,   yl   +   5)

    '取每个马赛克小块的中心象素的颜色为填充整个小块的颜色

    r   =   (color   mod   256)

    b   =   (int(color   /   65536))

    g   =   int((color   -   (b   *   65536)   -   r)   /   256)

    for   k   =   0   to   9     '填充整个马赛克小块的颜色

      for   j   =   0   to   9

          setpixel   picture1.hdc,   xl   +   k,   yl   +   j,   rgb(abs(r),   abs(g),   abs(b))


      next   j

    next   k

    picture1.refresh     '图象刷新

  next   xl

next   yl

end   if

end   if

end   sub

private   sub   command5_click()     '复位

picture1.picture   =   clipboard.getdata     '将剪贴板中保存的图象装入picture1

hscroll1.max   =   form1.picture1.width   -   form1.frame1.width

vscroll1.max   =   form1.picture1.height   -   form1.frame1.height   +   2567

'将滚动条的最大值分别重新赋值

'2567=frame1.height-picture1.height,在相同高度下frame与picture相差2567

end   sub

 

private   sub   form_load()     '窗体的初始位置

left   =   800

top   =   800

end   sub

private   sub   hscroll1_change()     '水平滚动条

picture1.left   =   -hscroll1.value

end   sub

private   sub   vscroll1_change()     '垂直滚动条

picture1.top   =   -vscroll1.value

end   sub

private   sub   picture1_mousemove(button   as   integer,   shift   as   integer,   x   as   single,   y   as   single)

'得到当前鼠标的位置

mx   =   x

my   =   y

end   sub

private   sub   exitm_click()

clipboard.clear

end

end   sub

end   sub
发表于:2008-01-13 16:22:291楼 得分:0
option   explicit
private   declare   function   setpixel   lib   "gdi32"   (byval   hdc   as   long,   byval   x   as   long,   byval   y   as   long,   byval   crcolor   as   long)   as   long
private   declare   function   getpixel   lib   "gdi32"   (byval   hdc   as   long,   byval   x   as   long,   byval   y   as   long)   as   long
private   sub   command1_click()
        '在(100,100)的位置作50*50的马赛克,马赛克的颗粒度为5个像素
        mosaic   picture1,   100,   100,   50,   50,   5
end   sub
private   sub   mosaic(pic   as   picturebox,   x   as   long,   y   as   long,   width   as   long,   heigh   as   long,   mosdetail   as   long)
        dim   i   as   long,   j   as   long,   k   as   long,   l   as   long
        dim   color   as   long
        '暂存pic.scalemode
        pic.scalemode   =   3
       
        for   i   =   0   to   width   step   mosdetail
                for   j   =   0   to   heigh   step   mosdetail
                        color   =   getpixel(pic.hdc,   x   +   i,   y   +   j)
                        for   k   =   0   to   mosdetail   -   1
                                for   l   =   0   to   mosdetail   -   1
                                        setpixel   pic.hdc,   x   +   i   +   k,   y   +   j   +   l,   color
                                        doevents
                                next
                        next
                next
        next
       
end   sub


快速检索

最新资讯
热门点击