就是我有个Bitmap数据,想设置它的透明度Alpha值,怎么设置?

解决方案 »

  1.   

    如果Bitmap有alpha通道,直接修改。
    如果没有,应该可以在渲染时指定一个aplha值.
      

  2.   

    这个Bitmap是通过HBITMAP创建的:Bitmap mybitmap=Bitmap(HBITMAP,NULL)。应该没有alpha值。能通过GDI+的DrawImage指定一个aplha值吗?
      

  3.   

    GDI+ 实现aphablend很简单,就是用colormatrix(修改第4行4列的值,0.5就代表apha值为128)
    你还可以参考哈我才发的帖子,用directx实现的colormatrix就知道他原理了。
    代码如下:// Create a Bitmap object and load it with the texture image.
    Bitmap bitmap(L"Texture1.jpg");
    Pen pen(Color(255, 0, 0, 0), 25);
    // Initialize the color matrix.
    // Notice the value 0.8 in row 4, column 4.
    ColorMatrix colorMatrix = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
                               0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
                               0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
                               0.0f, 0.0f, 0.0f, 0.8f, 0.0f,
                               0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
    // Create an ImageAttributes object and set its color matrix.
    ImageAttributes imageAtt;
    imageAtt.SetColorMatrix(&colorMatrix, ColorMatrixFlagsDefault,
       ColorAdjustTypeBitmap);
    // First draw a wide black line.
    graphics.DrawLine(&pen, Point(10, 35), Point(200, 35));
    // Now draw the semitransparent bitmap image.
    INT iWidth = bitmap.GetWidth();
    INT iHeight = bitmap.GetHeight();
    graphics.DrawImage(
       &bitmap, 
       Rect(30, 0, iWidth, iHeight),  // Destination rectangle
       0,                             // Source rectangle X 
       0,                             // Source rectangle Y
       iWidth,                        // Source rectangle width
       iHeight,                       // Source rectangle height
       UnitPixel, 
       &imageAtt);
      

  4.   

    谢谢gameatp,这个问题已经解决,
    但是我用了imageAttr的SetColorKey来去除底色,再用你这个方法的话,我就去除不了底色了。
      

  5.   

    GDI+不能直接同时支持aphablend和aphatest,你要底色透明建议使用png图像格式,或者用directx做渲染,他可以同时支持两种
      

  6.   

    我刚试了哈,可以实现透明啊,直接用ImageAttributes类方法SetColorKey来实现
    hdc = BeginPaint(hWnd, &ps);
    Graphics graphics(hdc);
    Bitmap bitmap(L"32.jpg");
    Pen pen(Color(255, 0, 0, 0), 25);
    // Initialize the color matrix.
    // Notice the value 0.8 in row 4, column 4.
    SolidBrush solidBrush(Color(255, 255, 0, 0));
    graphics.FillRectangle(&solidBrush, 0, 0, 500, 600);
    ColorMatrix colorMatrix = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.5f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
    // Create an ImageAttributes object and set its color matrix.
    ImageAttributes imageAtt;
    imageAtt.SetColorMatrix(&colorMatrix, ColorMatrixFlagsDefault,
    ColorAdjustTypeBitmap);
    imageAtt.SetColorKey(0,10,ColorAdjustTypeBitmap);
    // First draw a wide black line.
    //graphics.DrawLine(&pen, Point(10, 35), Point(200, 35));
    // Now draw the semitransparent bitmap image.
    INT iWidth = bitmap.GetWidth();
    INT iHeight = bitmap.GetHeight();
    graphics.DrawImage(
    &bitmap, 
    Rect(30, 0, iWidth, iHeight),  // Destination rectangle
    0,                             // Source rectangle X 
    0,                             // Source rectangle Y
    iWidth,                        // Source rectangle width
    iHeight,                       // Source rectangle height
    UnitPixel, 
    &imageAtt);
    EndPaint(hWnd, &ps);
    你找个背景为黑色的就可以了我设置的是
    imageAtt.SetColorKey(0,10,ColorAdjustTypeBitmap);
      

  7.   

    多谢了,我使用的一个中转Bitmap来实现!还是gameatp效率高