本文介绍了从HBITMAP获取字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给我一个HBITMAP指针,并且我的应用程序是控制台应用程序,如何从hbitmap获取图像字节.我尝试使用GetDIBits,它需要诸如HDC之类的参数,但我无法获取.

How can I get image bytes from hbitmap if I am given an HBITMAP pointer, and my application is console application.I tryed using GetDIBits which require such parameter as HDC, which I can't get.

我从文件加载位图:

HBITMAP bm = 0;
BITMAP Bitmap;
bm = (HBITMAP)LoadImage (0, TEXT("C:\\img1.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

我将HBITMAP传递给该函数,并希望获得另一个已处理图像的HBITMAP:

I pass the HBITMAP to the function and expect to get another HBITMAP of processed image:

HBITMAP out1 = func(bm);

函数在哪里:

HBITMAP func(HBITMAP im);

问题是如何从HBITMAP获取图像字节.

And the problem is how to get image bytes from HBITMAP.

推荐答案

查看新答案,因为问题已被编辑...

See new answer since question was edited...

没有设备上下文(HDC)的句柄,您将无法执行此操作.这是因为 GetDIBits 期望的是HBITMAP

You cannot do this without a handle to the device context (HDC). This is because GetDIBits expects an HBITMAP which is

DDB是 与设备有关的位图 (与DIB或设备无关位图相对).这意味着:

A DDB is a Device-Dependent Bitmap, (as opposed to a DIB, or Device-Independent Bitmap). That means:

这就是为什么GetDIBits需要HDC的原因.否则它将无法获取颜色信息.

Which is why GetDIBits requires an HDC. Otherwise it cannot get the color information.

也许一个好问题是,您是从哪里得到没有随附HDCHBITMAP的?

Perhaps a good question is, where did you get an HBITMAP without an accompanying HDC?

如果您尝试在内存中创建此位图,则首先可以调用 CreateCompatibleDC 创建与某些设备兼容的内存DC,然后 CreateCompatibleBitmap 与该DC.然后,您可以根据需要使用HBITMAPHDC.否则,如果您不知道HBITMAP指向的内容,就不能指望对其做任何有用的事情.

If you're trying to create this bitmap in memory, first you can call CreateCompatibleDC to create an in-memory DC compatible with some device, then CreateCompatibleBitmap with that DC. Then you have an HBITMAP and HDC to work with as you please. Otherwise, if you don't know what your HBITMAP is pointing to, you can't expect to do anything useful with it.

这篇关于从HBITMAP获取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 18:29