如何用API以最快速度把数组里的RGB颜色值还原为图形并绘制到PictureBox上?最好有实例。

解决方案 »

  1.   

    想快,当然是API,不过需有较厚实的图形处理基础!
      

  2.   


    Private 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
    Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Dim PicInfo As BITMAP
    Private idata() As BytePrivate Sub Command1_Click()
       Dim Elapse As Long   '记录所用时间
       GetObject Picture1.Image, Len(PicInfo), PicInfo
       ReDim idata(1 To PicInfo.bmWidth * PicInfo.bmHeight * 3) As Byte
       Randomize
       For i = 1 To PicInfo.bmWidth * PicInfo.bmHeight * 3
          idata(i) = CInt(Rnd * 255)    '生成随机图象数据
       Next
       
       Elapse = GetTickCount
       SetBitmapBits Picture1.Image, UBound(idata), idata(1)  '显示
       Picture1.Refresh
       MsgBox "用时" & GetTickCount - Elapse & "毫秒"
    End Sub
      

  3.   

    以上是用的DDB,也可以用DIB(麻烦一点,但用DDB时弄的不好会出现自动化错误)
    Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal HDC As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal HDC As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    Private Declare Function SetDIBits Lib "gdi32" (ByVal HDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Private Const BI_RGB = 0&
    Private Const DIB_RGB_COLORS = 0&
    Private Const LR_LOADFROMFILE = &H10
    Private Const IMAGE_BITMAP = 0&Private idata() As Byte           'holds bitmap data
    Private PicInfo As BITMAP         'bitmap info structurePrivate Type BITMAP
      bmType As Long
      bmWidth As Long
      bmHeight As Long
      bmWidthBytes As Long
      bmPlanes As Integer
      bmBitsPixel As Integer
      bmBits As Long
    End TypePrivate Type BITMAPINFOHEADER   '40 bytes
       biSize As Long
       biWidth As Long
       biHeight As Long
       biPlanes As Integer
       biBitCount As Integer
       biCompression As Long
       biSizeImage As Long
       biXPelsPerMeter As Long
       biYPelsPerMeter As Long
       biClrUsed As Long
       biClrImportant As Long
    End TypePrivate Type RGBQUAD
       rgbBlue As Byte
       rgbGreen As Byte
       rgbRed As Byte
       rgbReserved As Byte
    End TypePrivate Type BITMAPINFO
      bmiHeader As BITMAPINFOHEADER
      bmiColors As RGBQUAD
    End Type
    Public Sub ShowPic(ByVal pic As Long, data() As Byte)   '显示位图
      Dim hdcNew As Long
      Dim oldhand As Long
      Dim ret As Long
      Dim BytesPerScanLine As Long    '一个扫描行的长度
      Dim PadBytesPerScanLine As Long
      Dim DIBInfo As BITMAPINFO      'Device Ind. Bitmap info structure
      Call GetObject(pic, Len(PicInfo), PicInfo)         '取得对指定对象进行说明的一个结构,hobject为位图,刷子等的句柄,                                                      'count欲取回的字节数。通常是由lpObject定义的那个结构的长度
      hdcNew = CreateCompatibleDC(0&)                    '创建一个与屏幕兼容的设备场景
      oldhand = SelectObject(hdcNew, pic)
      With DIBInfo.bmiHeader
        .biSize = 40                                       'bmp3.0
        .biWidth = PicInfo.bmWidth
        .biHeight = -PicInfo.bmHeight     '从下往上扫描
        .biPlanes = 1
        .biBitCount = 24                 '24位位图,默认情况下Windows不会处理最高8位,可以将它作为自己的Alpha通道
        .biCompression = BI_RGB          '无压缩
        .biSizeImage = .biWidth * .biHeight * 3    '图像大小
      End With
      ret = SetDIBits(hdcNew, pic, 0, PicInfo.bmHeight, data(1, 1, 1), DIBInfo, DIB_RGB_COLORS)
      SelectObject hdcNew, oldhand
      DeleteDC hdcNew
    End Sub
    Private Sub Command1_Click()
    Dim Elapse As LongCall GetObject(Picture1.Image, Len(PicInfo), PicInfo)          '取得对指定对象进行说明的一个结构,hobject为位图,刷子等的句柄,                                                      'count欲取回的字节数。通常是由lpObject定义的那个结构的长度
    ReDim idata(1 To PicInfo.bmWidth, 1 To PicInfo.bmHeight, 1 To 3) As Byte
    Randomize
    For i = 1 To PicInfo.bmWidth
       For j = 1 To PicInfo.bmHeight
          For k = 1 To 3
            idata(i, j, k) = CInt(Rnd * 255)   ''生成随机图象数据,此处用一三维数组,分别保存RGB值
          Next
      Next
    NextElapse = GetTickCount
    Call ShowPic(Picture1.Image, idata)
    Picture1.Refresh
    MsgBox "用时" & GetTickCount - Elapse & "毫秒"
    End Sub