AlphaBlend中目标DC的矩形范围设置错误,应为0,0,nWidth,nHeight;

另外参考微软MSDN源码:

Alpha Blending a Bitmap

This topic has not yet been rated Rate this topic

The following code sample divides a window into three horizontal areas. Then it draws an alpha-blended bitmap in each of the window areas as follows:

  • In the top area, constant alpha = 50% but there is no source alpha.
  • In the middle area, constant alpha = 100% (disabled) and source alpha is 0 (transparent) in the middle of the bitmap and 0xff (opaque) elsewhere.
  • In the bottom area, constant alpha = 75% and source alpha changes.
void DrawAlphaBlend (HWND hWnd, HDC hdcwnd)
{HDC hdc;               // handle of the DC we will create  BLENDFUNCTION bf;      // structure for alpha blending HBITMAP hbitmap;       // bitmap handle BITMAPINFO bmi;        // bitmap header VOID *pvBits;          // pointer to DIB section ULONG   ulWindowWidth, ulWindowHeight;      // window width/height ULONG   ulBitmapWidth, ulBitmapHeight;      // bitmap width/height RECT    rt;            // used for getting window dimensions UINT32   x,y;          // stepping variables UCHAR ubAlpha;         // used for doing transparent gradient UCHAR ubRed;        UCHAR ubGreen;UCHAR ubBlue;float fAlphaFactor;    // used to do premultiply // get window dimensions GetClientRect(hWnd, &rt);// calculate window width/height ulWindowWidth = rt.right - rt.left;  ulWindowHeight = rt.bottom - rt.top;  // make sure we have at least some window size if ((!ulWindowWidth) || (!ulWindowHeight))return;// divide the window into 3 horizontal areas ulWindowHeight = ulWindowHeight / 3;// create a DC for our bitmap -- the source DC for AlphaBlend  hdc = CreateCompatibleDC(hdcwnd);// zero the memory for the bitmap info ZeroMemory(&bmi, sizeof(BITMAPINFO));// setup bitmap info  // set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);bmi.bmiHeader.biWidth = ulBitmapWidth = ulWindowWidth - (ulWindowWidth/5)*2;bmi.bmiHeader.biHeight = ulBitmapHeight = ulWindowHeight - (ulWindowHeight/5)*2;bmi.bmiHeader.biPlanes = 1;bmi.bmiHeader.biBitCount = 32;         // four 8-bit components bmi.bmiHeader.biCompression = BI_RGB;bmi.bmiHeader.biSizeImage = ulBitmapWidth * ulBitmapHeight * 4;// create our DIB section and select the bitmap into the dc hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);SelectObject(hdc, hbitmap);// in top window area, constant alpha = 50%, but no source alpha // the color format for each pixel is 0xaarrggbb  // set all pixels to blue and set source alpha to zero for (y = 0; y < ulBitmapHeight; y++)for (x = 0; x < ulBitmapWidth; x++)((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x000000ff; bf.BlendOp = AC_SRC_OVER;bf.BlendFlags = 0;bf.SourceConstantAlpha = 0x7f;  // half of 0xff = 50% transparency bf.AlphaFormat = 0;             // ignore source alpha channel if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))return;                     // alpha blend failed // in middle window area, constant alpha = 100% (disabled), source  // alpha is 0 in middle of bitmap and opaque in rest of bitmap  for (y = 0; y < ulBitmapHeight; y++)for (x = 0; x < ulBitmapWidth; x++)if ((x > (int)(ulBitmapWidth/5)) && (x < (ulBitmapWidth-ulBitmapWidth/5)) &&(y > (int)(ulBitmapHeight/5)) && (y < (ulBitmapHeight-ulBitmapHeight/5)))//in middle of bitmap: source alpha = 0 (transparent). // This means multiply each color component by 0x00. // Thus, after AlphaBlend, we have a, 0x00 * r,  // 0x00 * g,and 0x00 * b (which is 0x00000000) // for now, set all pixels to red ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x00ff0000; else// in the rest of bitmap, source alpha = 0xff (opaque)  // and set all pixels to blue  ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0xff0000ff; endif;bf.BlendOp = AC_SRC_OVER;bf.BlendFlags = 0;bf.AlphaFormat = AC_SRC_ALPHA;  // use source alpha  bf.SourceConstantAlpha = 0xff;  // opaque (disable constant alpha) if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5+ulWindowHeight, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))return;// bottom window area, use constant alpha = 75% and a changing // source alpha. Create a gradient effect using source alpha, and  // then fade it even more with constant alpha ubRed = 0x00;ubGreen = 0x00;ubBlue = 0xff;for (y = 0; y < ulBitmapHeight; y++)for (x = 0; x < ulBitmapWidth; x++){// for a simple gradient, base the alpha value on the x  // value of the pixel  ubAlpha = (UCHAR)((float)x / (float)ulBitmapWidth * 255);//calculate the factor by which we multiply each component fAlphaFactor = (float)ubAlpha / (float)0xff; // multiply each pixel by fAlphaFactor, so each component  // is less than or equal to the alpha value. ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = (ubAlpha << 24) |                       //0xaa000000 ((UCHAR)(ubRed * fAlphaFactor) << 16) |  //0x00rr0000 ((UCHAR)(ubGreen * fAlphaFactor) << 8) | //0x0000gg00 ((UCHAR)(ubBlue   * fAlphaFactor));      //0x000000bb }bf.BlendOp = AC_SRC_OVER;bf.BlendFlags = 0;bf.AlphaFormat = AC_SRC_ALPHA;   // use source alpha  bf.SourceConstantAlpha = 0xbf;   // use constant alpha, with  // 75% opaqueness AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5+2*ulWindowHeight, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);// do cleanup DeleteObject(hbitmap);DeleteDC(hdc);}


转载于:https://www.cnblogs.com/bigbigtree/archive/2012/02/13/2349554.html

Alpha Blending a Bitmap 失败 原因相关推荐

  1. CBinsight | 分析101个创业失败案例,我们总结了20大失败原因

    从缺乏产品与市场的相配到团队成员的不和,通过分析101个创业失败案例,我们总结了创业失败的前20大原因. 在我们列出创业失败案例清单后,我们收到最频繁的请求之一是我们能否从这些失败案例中提取出他们创业 ...

  2. Android应用如何开机自启动、自启动失败原因

    2019独角兽企业重金招聘Python工程师标准>>> Android应用如何开机自启动.自启动失败原因 本文主要介绍Android应用如何开机自启动.自启动失败的原因.adb命令发 ...

  3. oracle导入dmp报无效的sql,oracle施用pl/sql导入数据库备份文件dmp导入失败原因

    oracle使用pl/sql导入数据库备份文件dmp导入失败原因 利用PL/SQL导入数据库备份失败: Tools→Import →Tables Import Executable选择导入工具路径 D ...

  4. wordcloud安装失败原因和解决方法

    wordcloud安装失败原因和解决方法 参考文章: (1)wordcloud安装失败原因和解决方法 (2)https://www.cnblogs.com/Lynn123/p/11907440.htm ...

  5. 【Android 逆向】启动 DEX 字节码中的 Activity 组件 ( 使用 DexClassLoader 获取组件类失败 | 失败原因分析 | 自定义类加载器没有加载组件类的权限 )

    文章目录 一.使用 DexClassLoader 获取组件类失败报错 二.失败原因分析 一.使用 DexClassLoader 获取组件类失败报错 在上一篇博客 [Android 逆向]启动 DEX ...

  6. ssh免密登陆失败原因总结(Linux)

    转载自  ssh免密登陆失败原因总结(Linux) 1. SSH公钥认证(免密码)配置 登录到本机服务器A,切换到响应的操作系统用户,执行命令,生成秘钥文件[按照提示直接回车] ssh-keygen ...

  7. python jieba库下载_Python中jieba库安装步骤及失败原因解析

    Python 中 jieba 库安装步骤及失败原因解析 作为计算机小白, Python 的流行也让我蠢蠢欲动, 在请教计算机 专业同学后,开始上网课自学 Python 基础知识.今天老师简单的一 句话 ...

  8. java代码ftp重命名未生效_java使用apache commons连接ftp修改ftp文件名失败原因

    今天被ftp上中文名修改坑了好久 项目用的是 apache commons 里的 FtpClient 实现的对ftp文件的上传下载操作,今天增加了业务要修改ftp上的文件名,然后就一直的报错,问题是它 ...

  9. 腾讯企业邮箱外域邮件发送失败原因及其解决方案

    腾讯企业邮箱外域邮件发送失败原因及其解决方案 参考文章: (1)腾讯企业邮箱外域邮件发送失败原因及其解决方案 (2)https://www.cnblogs.com/chen110xi/p/465067 ...

最新文章

  1. BPF Tools 参考链接
  2. GridView 使用方法总结
  3. matlab simulink_运用MATLAB和Simulink开发自动驾驶控制系统
  4. C#中Bitmap类实现对图像操作的一些方法(转)
  5. redis缓存数据表
  6. 【PAT】B1070 结绳(25 分)
  7. java简单的事务单元_junit 单元测试事务自动回滚(亲测有效)
  8. 软件需求模式阅读笔记三
  9. 入微:探究文档中找不到的12c并行索引扫描新特性
  10. 对领域驱动设计的理解与实践
  11. 线性时间选择_马鞍山非线性分析工具了解详情_南京凯安软件
  12. html网页静态时钟代码,网页时钟实现代码html5
  13. Ubuntu16.04 安装火狐浏览器(中国版)
  14. Axure原型图(以微信作为参考)
  15. int转byte数组以及相关原理
  16. 海思芯片中VI的DEV和chan的概念
  17. 2017安徽阜阳、亳州移动计算机类面试+合肥二面
  18. Seccon-ctf-2016-pwn-cheer_msg 题解
  19. 【安卓】音视频开发入门
  20. 十二种值得深交的男生和十一种值得深交的女生

热门文章

  1. Windows 平台的 Markdown 编辑器 : Typora
  2. Maven 打包时不执行测试用例
  3. 火车头采集php源码不同,防火车头采集的功能
  4. 向xxxhub发了一个数据包,发现了···
  5. 中国已消失的九所世界级大学
  6. 比OCR更强大的PPT图片一键转文档重建技术
  7. java bean工厂_java-将Service用作“工厂”以返回不同的Bean...
  8. tcpip路由技术卷一_减少与开发的撕战,结合容器化技术轻松重构运维平台
  9. adb实时获取屏幕_实时数仓 | 你需要的是一款合适且强大的OLAP数据库(上)
  10. java字节码常量池_java字节码常量池处理说明