本文转自

http://vistaswx.com/blog/article/category/tutorial/page/2

导读:

本节介绍了一下Gdi+中进行数据直接读写的方法

1.GdipBitmapGetPixel

2.GdipBitmapSetPixel

3.GdipBitmapLockBits & GdipBitmapUnlockBits

1.GdipBitmapGetPixel

我们似乎一直在纳闷,GDI+中怎么拿到图片的数据呢?(我们之前一直都是绘制)。这一节,你就会拿数据了。

拿数据,无非就是取得一个点的ARGB值,而且往往我们要获取所有的点的值。

纵观GDI+的模块中的API,我们寻觅到了这个API:GdipBitmapGetPixel。如名,它是用于得到像素(点)的,事实上的确是。这个函数传出的变量包含了指定点的颜色值(AARRGGBB[16])。

此函数用起来及其方便,像VB中的Point(X,Y)函数一样。

例如:

 
Dim color As Long
GdipBitmapGetPixel bitmap, 0, 0, color
Debug.Print Hex(color)

这样我们就拿到了bitmap中点(0,0)的颜色。

再说回来,怎么拿到所有颜色呢?无非如此罢了:

 
Dim color() As Long
Dim i As Long, j As Long
 
ReDim color(BitmapWidth - 1, BitmapHeight - 1)
For i = 0 To BitmapWidth - 1
    For j = 0 To BitmapHeight - 1
        GdipBitmapGetPixel bitmap, i, j, color(i, j)
    Next
Next

注意:

其中,bitmap就是拿颜色的位图;

BitmapWidth,BitmapHeight分别是用GdipGetImageWidth,GdipGetImageHeight得到的Bitmap的尺寸。

2.GdipBitmapSetPixel

类比着,我们设置Bitmap中的某一个点也是很容易的,API是GdipBitmapSetPixel。代码类似于GdipBitmapGetPixel。

例如:

 
GdipBitmapSetPixel bitmap, 0, 0, &HFF000000

这句把bitmap位图中点(0,0)设置为黑色。

3.GdipBitmapLockBits & GdipBitmapUnlockBits

看看名字,LockBits,锁定……锁定是什么呢……这个先不要管他。不知你有没有发现前面2个得到颜色/设置颜色的GDI+函数巨慢无比?(不过貌似比VB中的Point快)。为什么呢?其实我也不知道(怎么可能呢?)

在我这台破烂电脑上,一幅800*800左右的图片,Get所有的Pixel花费了约1s……,那么1024*768怎么办?更大的怎么办?有没有更快的?

答案是,有。用什么?就是这个——GdipBitmapLockBits。

GdipBitmapLockBits,GdipBitmapUnlockBits是一对函数,他们可以将Bitmap上数据映射到内存和将内存中位图数据写回Bitmap,这个函数是一整块的,非常适合处理一大片的点——例如整个图像。

演示代码:

 
Option Explicit
 
Dim bitmap As Long, rc As RECTL
Dim data() As Long
 
Dim graphics As Long
 
Private Sub Form_Load()
    InitGDIPlus
 
    GdipCreateBitmapFromFile StrPtr("c:TestImg.png"), bitmap
    GdipGetImageWidth bitmap, rc.Right
    GdipGetImageHeight bitmap, rc.Bottom
 
    ReDim data(rc.Right - 1, rc.Bottom - 1)
 
    Dim BmpData As BitmapData
    With BmpData
        .Width = rc.Right
        .Height = rc.Bottom
 
        .PixelFormat = GpPixelFormat.PixelFormat32bppARGB
        .scan0 = VarPtr(data(0, 0))
        .stride = 4 * CLng(rc.Right)
    End With
 
    GdipBitmapLockBits bitmap, rc, ImageLockModeUserInputBuf Or ImageLockModeWrite Or ImageLockModeRead, GpPixelFormat.PixelFormat32bppARGB, BmpData
 
    Dim i As Long, j As Long, s As String
    For i = 0 To rc.Bottom - 1
        For j = 0 To rc.Right - 1
            data(j, i) = data(j, i) + data(j, i)
        Next
    Next
 
    GdipBitmapUnlockBits bitmap, BmpData
 
    GdipCreateFromHDC Me.hDC, graphics
    GdipDrawImageRectI graphics, bitmap, 0, 0, rc.Right, rc.Bottom
    GdipDeleteGraphics graphics
 
    GdipDisposeImage bitmap
End Sub
 
Private Sub Form_Unload(Cancel As Integer)
    TerminateGDIPlus
End Sub

我们先注意BmpData结构体,它实际上就是数据的设置。

scan0内存地址我们写的是VarPtr(data(0,0))。

stride = 4 * CLng(rc.Right):扫描宽度,这一个实际上就是二维数组每隔多少字节增加第一维,其中4是Long的存储长度,4个字节。

我们可以把数组的存储想象为一个矩形,gdi+放数据就是从左到右放,放到了边界则从下一行的第一个再从左向右放……

GdipBitmapLockBits

我们首先用它来得到位图数据,方式是ImageLockModeWrite Or ImageLockModeRead(ImageLockModeUserInputBuf必须写)。即我们传给他的data(),它将把位图数据写入;而在GdipBitmapUnlockBits时候将data()数据写入位图。注意:如果只是Write模式,那么你将得不到原始数据。

data(j, i) = data(j, i) + data(j, i) 这句是一句简单的颜色处理变换,让我们能看到处理效果

GdipBitmapUnlockBits:完成最后的写入&释放工作。后面几句是绘制这个新图片用。

注意:这里LockBits得到的数据data()是data(y,x),即顺序是(列,行)。Lockbits时候也要按照传入长度和宽度,不然VB会抛给你“Visual Basic 遇到问题需要关闭……

VB6 GDI+ 入门教程[9] Bitmap魔法(2):数据读写的更多相关文章

  1. VB6 GDI+ 入门教程[8] Bitmap魔法(1):创建

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[8] Bitmap魔法(1):创建 2009 年 9 月 ...

  2. VB6 GDI+ 入门教程[7] Graphics 其他内容

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[7] Graphics 其他内容 2009 年 9 月 ...

  3. VB6 GDI+ 入门教程[1] GDI+介绍

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[1] GDI+介绍 2009 年 6 月 18 日 17 ...

  4. VB6 GDI+ 入门教程[2] GDI+初始化

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[2] GDI+初始化 2009 年 6 月 18 日 7 ...

  5. VB6 GDI+ 入门教程[3] 笔、刷子、矩形、椭圆绘制

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[3] 笔.刷子.矩形.椭圆绘制 2009 年 6 月 1 ...

  6. VB6 GDI+ 入门教程[4] 文字绘制

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[4] 文字绘制 2009 年 6 月 18 日 7条评论 ...

  7. VB6 GDI+ 入门教程[5] 基础绘图小结

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[5] 基础绘图小结 2009 年 6 月 18 日 4条 ...

  8. VB6 GDI+ 入门教程[6] 图片

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[6] 图片 2009 年 6 月 19 日 15条评论 ...

  9. testng入门教程16数据驱动(把数据写在xml)

    testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...

随机推荐

  1. reactjs入门到实战(五)---- props详解

    1>>>基础的props使用     不可修改父属性    getDefaultProps   对于外界/父组件的属性值,无法直接修改,它是只读的. <script type= ...

  2. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  3. Intent传递对象——Serializable和Parcelable区别

    为什么要将对象序列化? 1.永久性保存对象,保存对象的字节序列到本地文件中: 2.用过序列化对象在网络中传递对象: 3.通过序列化对象在进程间传递对象. 1.实现Serializable接口 Seri ...

  4. linux 切换多个jdk脚本

    1,编写脚本 jdkswitch.sh #!/bin/sh # usage: . this_file [argvs] openjdk7_home=/usr/lib/jvm/java--openjdk- ...

  5. 这是一个关于Latex的测试

    这是行内公式的例子 \( c=\sqrt{a^2+b^2} \). 下面是行间公式的例子\[ c=\sqrt{a^2+b^2}. \] 注意: 行内公式不是用$分界, 而是 \( c=\sqrt{a^ ...

  6. ZooKeeper报错

    运行python 出现exception=java.io.IOException: Can't get master address from ZooKeeper; znode data == nul ...

  7. An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically

    Example is given for Pre-Query and Post-Query triggers in Oracle Forms, with using Display_Itembuilt ...

  8. [SAP ABAP开发技术总结]EXIT-COMMAND

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. C#打印条码的几种方式

    标题虽然是说C#,但是以下介绍的几种方法不是只能在C#中使用,在其它的语言里面也行. 总结一下常见的条码打印方法,其实打条码的方式很多,大概有以下几种: 1.斑马打印软件制作好模板,保存为.prn格式 ...

  10. MySQL记录操作

    说明:value的值可以为数据,DEFAULT,NULL,expr 含有ATUO_INCREMENT的列可以插入DEFAULT.NULL,或者不插入记录来实现自动增长. 插入记录的三种方法:①可以同时 ...