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

無法秀出圖片

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


發表:3
回覆:9
積分:2
註冊:2008-10-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-10-13 22:43:21 IP:140.127.xxx.xxx 訂閱
這是我的程式碼
不知道為什麼DATA讀不進去
一直修修改改的總算把W跟H的值讀進去
但是卻無法得知未什麼讀出來的圖是黑白的
拜託請高手幫我看看謝謝
#include
#include
#include
#include
#pragma hdrstop
#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
FILE *File;
char *froot;
BITMAPFILEHEADER bmpfile;
BITMAPINFOHEADER bmpinfo;
RGBQUAD bmpclr;
Byte *bmp_file;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
int *f,*g,w,h;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//froot = new char[256];
if(OpenDialog1->Execute())
{
froot=OpenDialog1->FileName.c_str();
File=fopen(froot,"r");
//get data
//bmp read header
fseek(File,0,SEEK_SET);
fread(&bmpfile.bfType,2,1,File);
fread(&bmpfile.bfSize,4,1,File);
fread(&bmpfile.bfReserved1,2,1,File);
fread(&bmpfile.bfReserved2,2,1,File);
fread(&bmpfile.bfOffBits,4,1,File);
//bmp read information
fread(&bmpinfo.biSize,4,1,File);
fread(&bmpinfo.biWidth,4,1,File);
fread(&bmpinfo.biHeight,4,1,File);
fread(&bmpinfo.biPlanes,2,1,File);
fread(&bmpinfo.biBitCount,2,1,File);
fread(&bmpinfo.biCompression,4,1,File);
fread(&bmpinfo.biSizeImage,4,1,File);
fread(&bmpinfo.biXPelsPerMeter,4,1,File);
fread(&bmpinfo.biYPelsPerMeter,4,1,File);
fread(&bmpinfo.biClrUsed,4,1,File);
fread(&bmpinfo.biClrImportant,4,1,File);
//bmp read Palette table
fread(&bmpclr.rgbBlue,4,1,File);
fread(&bmpclr.rgbGreen,4,1,File);
fread(&bmpclr.rgbRed,4,1,File);
fread(&bmpclr.rgbReserved,4,1,File);
//close
fclose(File);
w=bmpinfo.biWidth;
h=bmpinfo.biHeight;
bmp_file = new byte[bmpfile.bfSize];
fseek(File,bmpfile.bfOffBits,SEEK_SET);
fread(bmp_file,bmpfile.bfSize,1,File);

bitarray2bmp(bmp_file,w,h,pBitmap);
bmp_disp(pBitmap,Image1);
f=bmp2array(pBitmap,&w,&h);
Image1->Width=bmpinfo.biWidth;
Image1->Height=bmpinfo.biHeight;
//delete bmp head;
//delete bmp data;
}


//show image information
Label6 -> Caption = bmpfile.bfSize;
Label7 -> Caption = bmpfile.bfOffBits;
Label8 -> Caption = bmpinfo.biWidth;
Label9 -> Caption = bmpinfo.biHeight;
Label10 -> Caption = bmpinfo.biBitCount;
}
//---------------------------------------------------------------------------
//bmp disp
void bmp_disp(Graphics::TBitmap *bmp,TImage *Image)
{
Image->Canvas->Draw(0,0,bmp);
Image->Width=bmpinfo.biWidth;
Image->Height=bmpinfo.biHeight;
}
//---------------------------------------------------------------------------
int* bmp2array(Graphics::TBitmap *bmp,int *wp,int *hp)
{
int i,j;
int *f0,*f;
int w,h;
Byte *ptr;
w=bmp->Width;
h=bmp->Height;
*wp=w;
*hp=h;
f0=new int[w*h];
f=f0;
for(j=0;j {
ptr=(Byte *)bmp->ScanLine[j];
for(i=0;i {
*f =(int) (*ptr );
//ptr ;//pf24bits;
//ptr ;//pf24bits;
}
}
return f0;
}
//--------------------------------------------------------------------------
void bitarray2bmp(byte *g,int w,int h,Graphics::TBitmap *bmp)
{
int i,j;
int *f0,*f;
Byte *ptr;
bmp->PixelFormat = pf24bit;
bmp->Width = w;
bmp->Height = h;
if(bmpinfo.biBitCount == 8)
{
//bmp->PixelFormat = pf8bit;
for(j = 0 ; j < h ; j )
{
ptr =(Byte *)bmp->ScanLine[h-1-j];
for(i = 0;i < w;i )
{
*ptr = *g;
*ptr = *g;
*ptr = *g ;
}
}
}
else
{
for(j = 0 ; j < h ; j )
{
ptr = (Byte *)bmp->ScanLine[h-1-j];
for(i = 0 ; i < w ; i )
{
*ptr = *g ;
*ptr = *g ;
*ptr = *g ;
}
}
}
}
//---------------------------------------------------------------------------

編輯記錄
dmjohnny 重新編輯於 2008-10-14 01:42:47, 註解 無‧
taishyang
站務副站長


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-10-14 12:27:35 IP:118.169.xxx.xxx 訂閱
//close
fclose(File); <-- 已經將FILE fclose了,之後又對FILE做動作?
XXX
fseek(File,bmpfile.bfOffBits,SEEK_SET);
fread(bmp_file,bmpfile.bfSize,1,File);


另外
你new出來的記憶體都沒有回收
dmjohnny
一般會員


發表:3
回覆:9
積分:2
註冊:2008-10-01

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-10-14 16:30:34 IP:140.127.xxx.xxx 訂閱
非常感謝原來我的錯誤這麼簡單
系統時間:2024-05-15 3:20:17
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!