最近项目开发, 要用到线程中对图像进行缩放和二值化处理

为了省事, 图像缩放用的WICImage.ImagingFactory接口, 二值化用的是bitmap.PixelFormat := pf1bit后直接bitmap.Canvas.Draw(0, 0, wicimage)(这么做是因为bitmap在处理透明通道时会强制变为黑色, 而项目需要为白色, 所以只能draw)

运行时发现, 经常性的出现图像数据丢失, 经过排查发现数据丢失都是出现在wicimage向bitmap做数据转换时出现的

跟踪源码, 发现wicimage中的数据处理全部使用32位RGBA格式, 与外部进行图像对象交换时, 都要先吧数据存放到一个bitmap中, 再转换为其他格式

但是, 由于TBitmap不是线程安全的, 在线程中进行绘画操作要先canvas.lock才行, 而wicimage没有相关处理, 所以, 问题出现了

解决方案是, 自己吧wicimage中, 转换为中间bitmap的方法复制出来成为独立函数, 直接吧数据输出到目的bitmap, 省略draw那一步

这样就可以自己在外面随意控制bitmap的lock了

其实还有更好的办法是, 直接吧wicimage数据输出为1bit的位图, 但是我没搞定, 只要不是1bit的都可以, 唯独1bit的总是失败, 所以还是使用默认的32bit外面再使用bitmap的PixelFormat做转换了

代码如下:

function WICBitmap2Bitmap(AWICBitmap: IWICBitmap; ABMP: TBitmap): Boolean;
var
nLWicBitmap: IWICBitmapSource;
nStride: Cardinal;
nBuffer: array of Byte;
nBitmapInfo: TBitmapInfo;
nWidth, nHeight: UInt32;
begin
Result := False;
if AWICBitmap = nil then
Exit;
if ABMP = nil then
Exit; AWICBitmap.GetSize(nWidth, nHeight);
nStride := nWidth * ;
SetLength(nBuffer, nStride * nHeight); WICConvertBitmapSource(GUID_WICPixelFormat32bppBGRA, AWICBitmap, nLWicBitmap);
nLWicBitmap.CopyPixels(nil, nStride, Length(nBuffer), @nBuffer[]); FillChar(nBitmapInfo, sizeof(nBitmapInfo), );
with nBitmapInfo.bmiHeader do
begin
biSize := SizeOf(nBitmapInfo);
biWidth := nWidth;
biHeight := -nHeight;
biPlanes := ;
biBitCount := ;
end; with ABMP do
begin
PixelFormat := pf32bit;
SetSize(nWidth, nHeight);
{DC par not used (ABMP.Canvas.Handle) since Usage = DIB_RGB_COLORS}
SetDIBits(, Handle, , nHeight, @nBuffer[], nBitmapInfo, DIB_RGB_COLORS);
AlphaFormat := afDefined;
end;
Result := True;
end; function Bitmap2WICBitmap(ABMP: TBitmap; var AWICBitmap: IWicBitmap): Boolean;
var
nPixelFormat: TGUID;
nBitmapInfo: TBitmapInfo;
nBuffer: array of byte;
nWidth, nHeight: Int32;
begin
Result := False; if ABMP.AlphaFormat = afDefined then
nPixelFormat := GUID_WICPixelFormat32bppBGRA
else
nPixelFormat := GUID_WICPixelFormat32bppBGR; ABMP.PixelFormat := pf32bit; nWidth := ABMP.Width;
nHeight := ABMP.Height; SetLength(nBuffer, nWidth * * nHeight); FillChar(nBitmapInfo, sizeof(nBitmapInfo), );
with nBitmapInfo.bmiHeader do
begin
biSize := SizeOf(nBitmapInfo);
biWidth := nWidth;
biHeight := -nHeight;
biPlanes := ;
biBitCount := ;
end;
// Forces evaluation of Bitmap.Handle before Bitmap.Canvas.Handle
GetDIBits(ABMP.Canvas.Handle, ABMP.Handle, , nHeight, @nBuffer[],
nBitmapInfo, DIB_RGB_COLORS); TWICImage.ImagingFactory.CreateBitmapFromMemory(nWidth, nHeight, nPixelFormat,
nWidth * , Length(nBuffer), @nBuffer[], AWICBitmap);
end;

线程中WICImage与Bitmap数据转换的更多相关文章

  1. Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据

    Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14   阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...

  2. Android 实现在线程中联网

    其实我们要牢记的是,对数据流的操作都是阻塞的,在一般情况下,我们是不需要考虑这个问题的,但是在Android 实现联网的时候,我们必须考虑到这个问题.比如:从网络上下载一张图片: Java代码: pu ...

  3. 老问题:Android子线程中更新UI的3种方法

    在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...

  4. 【多线程补充】SimpleDateFormat非线程安全与线程中、线程组中异常的处理

    1.SimpleDateFormat非线程安全的问题 类SimpleDateFormat主要负责日期的转换与格式化,但在多线程环境中,使用此类容易造成数据转换及处理的不正确,因为SimpleDateF ...

  5. Android中高效的显示图片之二——在非UI线程中处理图片

    在“加载大图”文章中提到的BitmapFactory.decode*方法,如果源数据是在磁盘.网络或其它任何不是在内存中的位置,那么它都不应该在UI线程中执行.因为它的加载时间不可预测且依赖于一系列因 ...

  6. 线程中更新ui方法汇总

    一.为何写作此文   你是不是经常看到很多书籍中说:不能在子线程中操作ui,不然会报错.你是不是也遇到了如下的疑惑(见下面的代码): @Override protected void onCreate ...

  7. Android子线程中更新UI的4种方法

    方法一:用Handler 1.主线程中定义Handler: Handler mHandler = new Handler() { @Override public void handleMessage ...

  8. 在非UI线程中自制Dispatcher

    在C#中,Task.Run当然是一个很好的启动新并行任务的机制,但是因为使用这个方法时,每次新的任务都会在一个新的线程中(其实就是线程池中的线程)运行 这样会造成某些情形下现场调度的相对困难,即使我隔 ...

  9. 线程中调用python win32com

    在python的线程中,调用win32com.client.Dispatch 调用windows下基于COM组件的应用接口, 需要在调用win32com.client.Dispatch前,调用pyth ...

随机推荐

  1. 记录一下从懵懂到理解RESTful的过程

    前言 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)R ...

  2. PHP计算上个月的开始时间和结束时间戳

    $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); $t = date('t',strtotime($m)); //上个月共多少天 $ ...

  3. jdbc学习总结

    jdbc学习总结:   一.简介: jdbc,直译为java连接数据库.实际为java为很好的操作数据库而提供的一套接口,接口的实现(即驱动)由各个数据库厂商提供.   二.知识要点: 连接5要素,3 ...

  4. 翻译连载 | 第 10 章:异步的函数式(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  5. 扩展jquery.validate自定义验证,自定义提示,本地化

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  6. 将 C# 枚举反序列化为 JSON 字符串 基础理论

    该转换过程需要引用 Newtonsoft.JSON,这其中的转换过程还是蛮有意思的. 一.定义枚举 /// <summary> /// 托寄物品枚举 /// </summary> ...

  7. 【学习】原生js学习笔记1:添加class和使input为不可用

    <input type="checkbox" id="new_check" onChange="noUse()" checked> ...

  8. BootStrap教程完整版

    http://www.runoob.com/bootstrap/bootstrap-navbar.html

  9. BZOJ-3709-[PA2014]Bohater(贪心)

    Description 在一款电脑游戏中,你需要打败n只怪物(从1到n编号).为了打败第i只怪物,你需要消耗d[i]点生命值,但怪物死后会掉落血药,使你恢复a[i]点生命值.任何时候你的生命值都不能降 ...

  10. java遍历hashMap、hashSet、Hashtable

    一.遍历HashMap Map<Integer, String> map = new HashMap<Integer, String>(); 方法一:效率高 for(Entry ...