调整从内存阵列创建的HBITMAP的大小 [英] Resizing HBITMAP created from memory array
问题描述
我需要将位图的大小从local_member_buffer_1调整为prgb,并在prgb中制作2张图像
这两个都是RGB 24位每像素缓冲区.
这是代码的新变体:
I need to resize bitmap from local_member_buffer_1 to prgb and make 2 images in prgb
This both are RGB 24 bit per pixel buffers.
Here is the new variant of code:
{
HBITMAP handle_bit_map_source = NULL;
handle_bit_map_source = CreateBitmap(
cxImage,
cyImage,
1,
24,
local_member_buffer_1);
if(handle_bit_map_source==NULL)
{
goto stereo_horizontal_finish;
}
HBITMAP handle_bit_map_resulting = (HBITMAP)CopyImage
(
handle_bit_map_source,
IMAGE_BITMAP,
cxImage/2,
cyImage/2,
LR_COPYDELETEORG
);
if(handle_bit_map_resulting==NULL)
{
DeleteObject(handle_bit_map_source);
goto stereo_horizontal_finish;
}
ZeroMemory(local_member_buffer_0,cxImage/2*cyImage/2*sizeof(RGBTRIPLE));
LONG local_bytes_copied = GetBitmapBits(handle_bit_map_resulting, cxImage/2*cyImage/2*sizeof(RGBTRIPLE), local_member_buffer_0);
if (local_bytes_copied!=cxImage/2*cyImage/2*sizeof(RGBTRIPLE))
{
DeleteObject(handle_bit_map_resulting);
goto stereo_horizontal_finish;
}
ZeroMemory(prgb,cxImage*cyImage*sizeof(RGBTRIPLE));
// Ëåâîå èçîáðàæåíèå
for(local_counter_width=0;local_counter_width<cxImage/2;local_counter_width++)
{
for(int local_counter=0;local_counter<cyImage/2;local_counter++)
{
prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtRed
=
local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtRed
;
prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtGreen
=
local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtGreen
;
prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtBlue
=
local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtBlue
;
}
}
// Ïðàâîå èçîáðàæåíèå
for(local_counter_width=0;local_counter_width<cxImage/2;local_counter_width++)
{
for(int local_counter=0;local_counter<cyImage/2;local_counter++)
{
prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtRed
=
local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtRed
;
prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtGreen
=
local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtGreen
;
prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtBlue
=
local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtBlue
;
}
}
DeleteObject(handle_bit_map_resulting);
// memcpy(prgb,local_member_buffer_2,cxImage*cyImage*sizeof(RGBTRIPLE));
}
stereo_horizontal_finish:
我在GetBitmapBits之后得到带有零的图像.
I get image with zeros after GetBitmapBits.
What is wrong here?
推荐答案
为什么不直接剪切位图位?
Why dont you clip the bitmap bits directly?
int cxDest = cxImage/2;
int cyDest = cyImage/2;
int bpls = ((cxImage*sizeof(RGBTRIPLE))+3)&~3;
int bpld = ((cxDest *sizeof(RGBTRIPLE))+3)&~3;
unsigned char* lps = (unsigned char*)local_member_buffer_1 + (bpls*(cyImage-cyDest-1));
unsigned char* lpd = (unsigned char*)prgb + (bpld*(cyDest-1));
unsigned char* lpe = lps - (bpls*cyDest);
for(;lps>lpe;lps-=bpls,lpd-=bpld)
{
memcpy(lpd,lps,sizeof(RGBTRIPLE)*cxDest);
}
问候.
[edit]
我想您想用左右相同的图像创建立体图像:
Regards.
[edit]
i suppose you want to create a stereo image with same image left and right:
int cxDest = cxSource/2;
int cyDest = cySource;
int bpls = ((cxSource*3)+3)&~3;
int bpld = ((cxDest *3)+3)&~3;
unsigned char* lps = local_member_buffer_1;
unsigned char* lpd = prgb;
unsigned char* lpe = local_member_buffer_1+ + (bpls*cySource);
for(;lps<lpe;lps+=bpls,lpd+=bpld)
{
// copy each line to left and right
memcpy(lpd,lps,sizeof(RGBTRIPLE)*cxSource);
memcpy(lpd+(sizeof(RGBTRIPLE)*cxSource),lps,sizeof(RGBTRIPLE)*cxSource);
}
祝你好运.
科兹洛夫·谢尔盖:
是的.但是它只调整高度.
我也需要宽度.
[edit]
具有缩小的原始像素,这会大一些:
Good luck.
Kozlov Sergey:
Yes. But it resizes only height.
I need also width.
[edit]
with shrink original pixel, this is a little bit larger:
int cxDest = cxSource;
int cyDest = cySource/2;
int bpls = ((cxSource*3)+3)&~3;
int bpld = ((cxDest *3)+3)&~3;
unsigned char* lps = local_member_buffer_1;
unsigned char* lpd = prgb;
unsigned char* lpe = local_member_buffer_1+ + (bpls*cySource);
unsigned char* lppd;
unsigned char* lppe;
unsigned char* lpps1;
unsigned char* lpps2;
typedef struct
{
static void shrink4x4(RGBTRIPLE* a,RGBTRIPLE* b,RGBTRIPLE* c,RGBTRIPLE* d,RGBTRIPLE* r)
{
r->rgbtRed = ((unsigned int)a->rgbtRed +
(unsigned int)b->rgbtRed +
(unsigned int)c->rgbtRed +
(unsigned int)d->rgbtRed)>>2;
r->rgbtGreen = ((unsigned int)a->rgbtGreen +
(unsigned int)b->rgbtGreen +
(unsigned int)c->rgbtGreen +
(unsigned int)d->rgbtGreen)>>2;
r->rgbtBlue = ((unsigned int)a->rgbtBlue +
(unsigned int)b->rgbtBlue +
(unsigned int)c->rgbtBlue +
(unsigned int)d->rgbtBlue)>>2;
}
}_;
for(;lps<lpe;lps+=bpls*2,lpd+=bpld)
{
lppe = lpd + sizeof(RGBTRIPLE)*(cxDest/2);
for(lppd=lpd,lpps1=lps,lpps2=lps+bpls;lppd<lppe;lppd+=sizeof(RGBTRIPLE))
{
_::shrink4x4
(
(RGBTRIPLE*)lpps1,
(RGBTRIPLE*)(lpps1+sizeof(RGBTRIPLE)),
(RGBTRIPLE*)lpps2,
(RGBTRIPLE*)(lpps2+sizeof(RGBTRIPLE)),
(RGBTRIPLE*)lppd
);
lpps1 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
lpps2 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
}
lppe = lpd + sizeof(RGBTRIPLE)*(cxDest);
for(lpps1=lps,lpps2=lps+bpls;lppd<lppe;lppd+=sizeof(RGBTRIPLE))
{
_::shrink4x4
(
(RGBTRIPLE*)lpps1,
(RGBTRIPLE*)(lpps1+sizeof(RGBTRIPLE)),
(RGBTRIPLE*)lpps2,
(RGBTRIPLE*)(lpps2+sizeof(RGBTRIPLE)),
(RGBTRIPLE*)lppd
);
lpps1 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
lpps2 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
}
}
您好,
我认为您需要使用CreateCompatibleBitmap
而不是CreateBitmap
才能使用SelectObject
.
也可以尝试使用CopyImage
函数.
http://msdn.microsoft.com/en-us/library/ms648031(VS .85).aspx [ ^ ]
这可能对您有用...
祝您好运!
Hello,
I think you need to useCreateCompatibleBitmap
instead ofCreateBitmap
, in order toSelectObject
.
Also try theCopyImage
function.
http://msdn.microsoft.com/en-us/library/ms648031(VS.85).aspx[^]
It may be useful for you...
Good Luck!
确实,您需要使用CreateCompatibleBitmap,以便它使用设备上下文设置,而不是自己弄清楚它们.否则,选择将失败,因为由于上下文不正确而无法选择对象.
Indeed, You need to use CreateCompatibleBitmap so that it uses the device context settings rather than trying to figure them out yourself. Other wise the select will fail as it cannot select the object due to the context being incorrect.
这篇关于调整从内存阵列创建的HBITMAP的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!