線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2713
推到 Plurk!
推到 Facebook!

在Button中,動態產生灰階Bitmap問題

答題得分者是:taishyang
shiuan0610
一般會員


發表:15
回覆:13
積分:5
註冊:2007-05-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-30 11:44:12 IP:61.56.xxx.xxx 訂閱
第一次按下面這個Button,Image_temp很順利的存檔了一個灰階圖
但按第二次後,Image_temp就變成存彩色圖了
請問為什麼會這樣,這個問題已經搞了我很久了....

[code cpp]
void __fastcall TForm_Main::ButtonClick(TObject *Sender)
{
//產生灰階調色盤
LOGPALETTE GrayPalette;
PALETTEENTRY pal[1];
HPALETTE GrayPaletteHandle;
GrayPalette.palVersion = 0x300;
GrayPalette.palNumEntries = 256;
GrayPaletteHandle=CreatePalette(&GrayPalette);
for(int ii=0;ii<256;ii )
{
pal[0].peRed=ii;
pal[0].peGreen=ii;
pal[0].peBlue=ii;
SetPaletteEntries(GrayPaletteHandle,ii,1,pal);
}

Graphics::TBitmap *LCMBitmap;
LCMBitmap=new Graphics::TBitmap();
LCMBitmap->Width=1024;
LCMBitmap->Height=768;
LCMBitmap->PixelFormat=pf8bit;
LCMBitmap->Palette=GrayPaletteHandle;

Byte *ptr;
vector TheCaptureImage(1024*768);
for(int y=0; y<768; y )
{
ptr=(Byte*)Image1->Picture->Bitmap->ScanLine[y];
for(int x=0; x<1024; x )
TheCaptureImage[y*1024 x]=ptr[x];
}

//do something
for(int y=0; y<768; y )
{
ptr=(Byte*)LCMBitmap->ScanLine[y];
for(int x=0; x<1024; x )
ptr[x]=TheCaptureImage[y*1024 x];
}
Image_temp->Picture->Assign(LCMBitmap);
Image_temp->Repaint();
Image_temp->Picture->SaveToFile("Image_Test01.bmp");
//delete LCMBitmap;
}
[/code]
編輯記錄
shiuan0610 重新編輯於 2007-11-30 11:44:54, 註解 無‧
shiuan0610 重新編輯於 2007-11-30 14:55:55, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-11-30 14:43:37 IP:122.124.xxx.xxx 訂閱
你程式的目的是?
shiuan0610
一般會員


發表:15
回覆:13
積分:5
註冊:2007-05-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-11-30 15:12:51 IP:61.56.xxx.xxx 訂閱
我的程式是要取得Image1裡的影像資料到一個vector裡,再處理
處理完後,影像大小會隨機改變,所以我才new了一個新的Bitmap來用
上面PO的程式是已經有所刪減的了,前後影像大小也都設成一樣了
主要是能表現出我的問題就好
所以您由上面的程式碼中可能會看不出我在幹嘛
只是把data由Image1裡取出,放到一個vector,再把vector內容轉存到Image_temp裡
或您覺得這程式看起來似乎是無意義的,但這不是重點
雖然處理過程我刪減了
但這簡化後的程式,執行起來,我的問題點還是存在的
也證明第一次執行會存灰階
重複執行後就會存成彩色影像的問題,應該不是我的影像處理過程造成的

麻煩大家幫我看看問題何在了~
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-11-30 15:36:23 IP:122.124.xxx.xxx 訂閱
我自己裁剪出一張100x100的圖片做測試是OK的
其中我把
TheCaptureImage[y*1024 x]=ptr[x];
改成
TheCaptureImage[y*1024 x]=ptr[x*3]; //因為圖片是24bit bmp

測試的程式放在:

http://delphi.ktop.com.tw/board.php?cid=31&fid=97&tid=91518

shiuan0610
一般會員


發表:15
回覆:13
積分:5
註冊:2007-05-08

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-11-30 15:46:45 IP:60.244.xxx.xxx 訂閱
哈哈
我自己解決問題了
因為再試一下發現其實我上面PO的程式碼沒問題

我原來像下面程式碼這樣用的:
在FormCreate就先做好灰階調色盤:全域的GrayPaletteHandle
但這會產生我上述的問題

如果像上面程式在Button裡重新做灰階調色盤,就沒問題了
怎麼存都不會變成彩色圖

不知有沒高手能解釋為什麼會這樣

[code cpp]
HPALETTE GrayPaletteHandle;
void __fastcall TForm_Main::FormCreate(TObject *Sender)
{
//產生灰階調色盤
LOGPALETTE GrayPalette;
PALETTEENTRY pal[1];

GrayPalette.palVersion = 0x300;
GrayPalette.palNumEntries = 256;
GrayPaletteHandle=CreatePalette(&GrayPalette);

for(int ii=0;ii<256;ii )
{
pal[0].peRed=ii;
pal[0].peGreen=ii;
pal[0].peBlue=ii;
SetPaletteEntries(GrayPaletteHandle,ii,1,pal);
}
}
void __fastcall TForm_Main::Button7Click(TObject *Sender)
{
Graphics::TBitmap *LCMBitmap;
LCMBitmap=new Graphics::TBitmap();
LCMBitmap->Width=1024;
LCMBitmap->Height=768;
LCMBitmap->PixelFormat=pf8bit;
LCMBitmap->Palette=GrayPaletteHandle;
Byte *ptr;
vector TheCaptureImage(1024*768);
for(int y=0; y<768; y )
{
ptr=(Byte*)Image1->Picture->Bitmap->ScanLine[y];
for(int x=0; x<1024; x )
TheCaptureImage[y*1024 x]=ptr[x];
}
for(int y=0; y<768; y )
{
ptr=(Byte*)LCMBitmap->ScanLine[y];
for(int x=0; x<1024; x )
ptr[x]=TheCaptureImage[y*1024 x];
}
Image_temp->Picture->Assign(LCMBitmap);
Image_temp->Repaint();
Image_temp->Picture->SaveToFile("Image_Teat01.bmp");
delete LCMBitmap;
}
[/code]
shiuan0610
一般會員


發表:15
回覆:13
積分:5
註冊:2007-05-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-11-30 15:52:06 IP:60.244.xxx.xxx 訂閱
感謝taishyang兄幫我試,我下載您的程式了也的確沒問題
抱歉我一時不查浪費了您寶貴時間
所以還是點數給您
系統時間:2024-05-20 2:31:57
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!