想让picturebox实现两种透明颜色的变换,比如我在picturebox贴了一张图片,现在想让picturebox贴一层透明蓝色也就是主色调为蓝色,然后5秒钟后再换成透明黄色,这样循环下去,代码应该怎么写

解决方案 »

  1.   

      #define   UNICODE   
      #include   <Gdiplus.h>   
      using   namespace   Gdiplus;   
        
      OnDraw(CDC*   pDC)   
      {   
       Bitmap   src(L"photo.jpg");     //原图   
       Bitmap   mask(L"mask.jpg");     //Mask图   
       Bitmap   bmp(src.GetWidth(),   src.GetHeight(),   PixelFormat32bppARGB);//结果图   
        
      //将原图和mask图设置到结果图上   
       for(int   y   =   0;   y   <   src.GetHeight();   ++y)   
        for(int   x   =   0;   x   <   src.GetWidth();   ++x)   
        {   
         Color   c1,   c2;   
         src.GetPixel(x,   y,   &c1);   
         mask.GetPixel(x,   y,   &c2);   
         bmp.SetPixel(x,   y,   Color(c2.GetB(),   c1.GetR(),   c1.GetG(),   c1.GetB()));   
        }   
       Graphics   graphics(*pDC);   
       //画到DC上   
       graphics.DrawImage(&bmp,   0,   0);   
      }   
    不过这个方法貌似很慢,楼主等我去看下PHOTOSHOP,看看颜色是咋样的还有叠加的值的变化
      

  2.   

    '获取像素  DibGet()
    '图像输出  DIBPut()
    'Copy 数组  CopyData()'用于存放从DIB输入的像素值(获取的像素数组)    ColVal()
    '注 第一维: 0= B 值  1= G 值  2= R 值  3= Alpha
    '  第二维: X 值
    '  第三维: Y 值'删除一个DC
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    '删除一个对象
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    '选择当前对象
    Private Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long
    '获取DIB
    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 timeGetTime Lib "winmm.dll" () As Long
    '输出图像
    Private Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BitMapInfo, ByVal wUsage As Long) As Long
    '内存操作
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
          
    '数据结构定义:
    Private Type BitMapInfoHeader '文件信息头——BITMAPINFOHEADER
        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 Type
      
    Private Type RGBQuad
        rgbBlue  As Byte
        rgbGreen  As Byte
        rgbRed  As Byte
        'rgbReserved  As  Byte
    End Type
      
    Private Type BitMapInfo
      bmiHeader  As BitMapInfoHeader
      bmiColors  As RGBQuad
    End Type
      
    '过程中用到的全局变量:
    Private Const Bits As Long = 32        '颜色深度,这里把所有图像都按照32位来处理
    Public Done As Boolean                  '用于标记一个过程是否结束
    Public TimeGet As Long                  '用于记录输入过程处理所花费的时间
    Public TimePut As Long                  '用于记录输出过程处理所花费的时间
    Dim ColVal() As Byte                    '用于存放从DIB输入的像素值
    Dim ColOut() As Byte                    '用于存放向DIB输出的像素值
    Dim InPutHei As Long                    '用于记录输入图像的高度
    Dim InPutWid As Long                    '用于记录输入图像的宽度
    Dim bi24BitInfo As BitMapInfo          '定义BMP信息
    Dim a As Integer'获取像素
    Public Sub DibGet(ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long)
      Dim iBitmap    As Long
      Dim iDC    As Long
      Dim I    As Long
      Dim W    As Long
      Dim H    As Long
      Dim alpha As Integer
        
      On Error GoTo ErrLine
      Done = False
      TimeGet = timeGetTime
      InPutWid = XEnd - XBegin
      InPutHei = YEnd - YBegin
      
      W = InPutWid + 1
      H = InPutHei + 1
        
      I = (Bits \ 8) - 1
      ReDim ColVal(I, InPutWid, InPutHei)  With bi24BitInfo.bmiHeader
        .biBitCount = Bits
        .biCompression = 0&
        .biPlanes = 1
        .biSize = Len(bi24BitInfo.bmiHeader)
        .biWidth = W
        .biHeight = H
      End With
        
      iBitmap = GetCurrentObject(IdSource, 7&)
      GetDIBits IdSource, iBitmap, 0&, H, ColVal(0, 0, 0), bi24BitInfo, 0&
      
      DeleteObject iBitmap
      Done = True
      TimeGet = timeGetTime - TimeGet
      Exit Sub
    ErrLine:
      MsgBox "错误号:" & Err.Number & ":" & Err.Description
    End Sub'图像输出
    Private Sub DIBPut(ByVal IdDestination As Long, Width As Long, Height As Long)
      Dim W As Long
      Dim H As Long
      Dim I, j As Long
      Dim LineBytes As Long  On Error GoTo ErrLine
        Done = False
        TimePut = timeGetTime
        
        W = Width + 1
        H = Height + 1
        
        With bi24BitInfo.bmiHeader
          .biWidth = W
          .biHeight = H
          LineBytes = ((W * Bits + 31) And &HFFFFFFE0) \ 8
          .biSizeImage = LineBytes * H
        End With
        '==================================================================
        ' 替换纯色的R 255 G 0 B 0 为 R 0 G 0 B 0
        '
        ' 不知道这个能不能满足你的要求,我搞了好半天
        '
        '==================================================================
        'For I = 0 To H - 1
        '    For j = 0 To W - 1
        '        If ColOut(2, I, j) = 255 And ColOut(0, I, j) = 0 And ColOut(1, I, j) = 0 Then
        '           ColOut(2, I, j) = 0
        '
        '        End If
        '
        '    Next j
        'Next I
        '===================================================================
        ' 图象颜色运算
        '
        ' 变换透明的颜色RGB ,这个是蒙板效果
        ' 内存操作,运算公式是:
        ' RGB(R1*x%+R2*(1-x%),B1*x%+B2*(1-x%),B1*x%+B2*(1-x%))
        '===================================================================
            For I = 0 To H - 1
            For j = 0 To W - 1
                ColOut(2, I, j) = Int(ColOut(2, I, j) * 0.5 + 104 * 0.5)
                 ColOut(1, I, j) = Int(ColOut(1, I, j) * 0.5 + 154 * 0.5)
                 ColOut(0, I, j) = Int(ColOut(0, I, j) * 0.5 + 255 * 0.5)
            Next j
        Next I
        '====================================================================
        
        SetDIBitsToDevice IdDestination, 0, 0, W, H, 0, 0, 0, H, ColOut(0, 0, 0), bi24BitInfo, 0    Done = True
        TimePut = timeGetTime - TimePut
        Exit Sub
      On Error GoTo 0
    ErrLine:
      MsgBox Err.Description
    End Sub
      

  3.   

    '==这是为你的需求特意COPY的
    '图像输出
    Private Sub YELLOWDIBPut(ByVal IdDestination As Long, Width As Long, Height As Long)
      Dim W As Long
      Dim H As Long
      Dim I, j As Long
      Dim LineBytes As Long  On Error GoTo ErrLine
        Done = False
        TimePut = timeGetTime
        
        W = Width + 1
        H = Height + 1
        
        With bi24BitInfo.bmiHeader
          .biWidth = W
          .biHeight = H
          LineBytes = ((W * Bits + 31) And &HFFFFFFE0) \ 8
          .biSizeImage = LineBytes * H
        End With
        '==================================================================
        ' 替换纯色的R 255 G 0 B 0 为 R 0 G 0 B 0
        '
        ' 不知道这个能不能满足你的要求,我搞了好半天==这个是别人提问偶帮她改的,不关你事
        '
        '==================================================================
        'For I = 0 To H - 1
        '    For j = 0 To W - 1
        '        If ColOut(2, I, j) = 255 And ColOut(0, I, j) = 0 And ColOut(1, I, j) = 0 Then
        '           ColOut(2, I, j) = 0
        '
        '        End If
        '
        '    Next j
        'Next I
        '===================================================================
        ' 图象颜色运算
        '
        ' 变换透明的颜色RGB ,这个是蒙板效果
        ' 内存操作,运算公式是:
        ' RGB(R1*x%+R2*(1-x%),B1*x%+B2*(1-x%),B1*x%+B2*(1-x%))
        '===================================================================
            For I = 0 To H - 1
            For j = 0 To W - 1
                ColOut(2, I, j) = Int(ColOut(2, I, j) * 0.5 + 255 * 0.5)
                 ColOut(1, I, j) = Int(ColOut(1, I, j) * 0.5 + 255 * 0.5)
                 ColOut(0, I, j) = Int(ColOut(0, I, j) * 0.5 + 166 * 0.5)
            Next j
        Next I
        '====================================================================
        
        SetDIBitsToDevice IdDestination, 0, 0, W, H, 0, 0, 0, H, ColOut(0, 0, 0), bi24BitInfo, 0    Done = True
        TimePut = timeGetTime - TimePut
        Exit Sub
      On Error GoTo 0
    ErrLine:
      MsgBox Err.Description
    End Sub
    'Copy 数组
    Public Sub CopyData(ByVal W As Long, ByVal H As Long)
      Dim Length As Long
      Dim I As Long
      Dim L As Long
     
      I = Bits \ 8
      L = I - 1
      Length = (W + 1) * (H + 1) * I
      ReDim ColOut(L, W, H)
      CopyMemory ColOut(0, 0, 0), ColVal(0, 0, 0), Length
    End Sub
    Private Sub Timer1_Timer()
      With Picture1
        .AutoRedraw = True
        .AutoSize = True
        .ScaleMode = 3
        DibGet .hdc, 0, 0, .ScaleWidth, .ScaleHeight
      End With
      CopyData InPutHei, InPutWid
      
      With Picture2
        .AutoRedraw = True
        .AutoSize = True
        .ScaleMode = 3
        YELLOWDIBPut .hdc, InPutWid, InPutHei
        .Refresh
      End With
      Timer2.Enabled = True
      Timer1.Enabled = False
      End SubPrivate Sub Timer2_Timer()  With Picture1
        .AutoRedraw = True
        .AutoSize = True
        .ScaleMode = 3
        DibGet .hdc, 0, 0, .ScaleWidth, .ScaleHeight
      End With
      CopyData InPutHei, InPutWid
      
      With Picture2
        .AutoRedraw = True
        .AutoSize = True
        .ScaleMode = 3
        DIBPut .hdc, InPutWid, InPutHei
        .Refresh
      End With
      Timer1.Enabled = True
      Timer2.Enabled = False
      'TIMER1和TIMER2的时间都是5000毫秒  TIMER1 设置成TRUE TIMER2设置成FALSE就可以了
      '如果你还要搞什么飞机,你就自己去改吧... 
    End Sub
      

  4.   

    你COPY了,在窗体上放两个PICTURE控件两个TIMER控件...PICTURE1里把图片加载进去一实验不就知道了
      

  5.   

    就是兰色 黄色 的效果,你自己换颜色就是了,随便换...你看都不看代码,想要别人帮你做到直接COPY...哪儿有这么好的事情啊... 这代码也花了点时间... 我以为够你看明白的... 你要的改颜色的地方都注释了