ayuen
一般會員
發表:19 回覆:34 積分:10 註冊:2003-07-31
發送簡訊給我
|
把一張灰階512*512圖檔(Image1)選取出256*256的區域放在Image2
但是兩張圖的同一點的灰階值竟然不同
且在complement(反白)的時候只做到四分之一寬度的圖
如果把寬度改成1024則可以將整張256*256的圖反白
唉~一直找不出原因
大家幫幫我吧~~謝謝摟
--------------------Unit.h------------------------
public: // User declarations
__fastcall TForm1(TComponent* Owner);
int StartX,StartY, EndX, EndY;
int TempW;
bool Masking,Pasting,Pasting_Enable;
bool Ok;
Graphics::TBitmap *OrgBitmap,*PasteBitmap;
Graphics::TBitmap *TheBitmap1;
------------------------------------------------
#include
#pragma hdrstop
#include
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
OrgBitmap= new Graphics::TBitmap();
PasteBitmap= new Graphics::TBitmap();
Pasting=false;
ScaleFactor=1.0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Open1Click(TObject *Sender)
{
if (OpenPictureDialog1->Execute())
{
Image1->AutoSize=true;
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
OrgBitmap->Assign(Image1->Picture->Bitmap);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
Graphics::TBitmap *TheBitmap;
TheBitmap=Image1->Picture->Bitmap;
//-------------------------------------------------------------------
if (Pasting)
{
TheBitmap->Canvas->Draw(0,0,OrgBitmap);
X=(int)X*ScaleFactor;
Y=(int)Y*ScaleFactor;
TheBitmap->Canvas->Draw(X-StartX,Y-StartY,PasteBitmap);
TheBitmap->Canvas->Pen->Mode=pmBlack;
TheBitmap->Canvas->Pen->Width=2;
TheBitmap->Canvas->Brush->Style=bsClear;
TheBitmap->Canvas->Rectangle(X-StartX,Y-StartY,X-StartX PasteBitmap->Width,Y-StartY PasteBitmap->Height);
}
lbl1->Caption="(X,Y)=(" AnsiString(Y) "," AnsiString(X) ")";
lbl2->Caption="RGB=(" AnsiString(GetRValue(Image1->Canvas->Pixels[X][Y])) ","
AnsiString(GetGValue(Image1->Canvas->Pixels[X][Y])) ","
AnsiString(GetBValue(Image1->Canvas->Pixels[X][Y])) ")";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Graphics::TBitmap *TheBitmap;
StartX=(int)X*ScaleFactor;
StartY=(int)Y*ScaleFactor;
if(Pasting_Enable)
{
Pasting=true;
Pasting_Enable=false;
}
else
{
Pasting=false;
TheBitmap=Image1->Picture->Bitmap;
TheBitmap->Canvas->Draw(0,0,OrgBitmap);
}
Ok=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
Graphics::TBitmap *TheBitmap,*TheBitmap1,*TempBitmap;
int Width, Height;
int TempW;
int TFX, TFY, BRX, BRY;
TRect SrcRect, DestRect;
EndX=(int)X*ScaleFactor;
EndY=(int)Y*ScaleFactor;
if (Pasting)
{
TheBitmap=Image1->Picture->Bitmap;
TheBitmap->Canvas->Draw(0,0,OrgBitmap);
TheBitmap->Canvas->Draw(EndX-StartX,EndY-StartY,PasteBitmap);
OrgBitmap->Assign(TheBitmap);
}
Pasting=false;
if(Ok)
{
TheBitmap=Image1->Picture->Bitmap;
TheBitmap->Canvas->Draw(0,0,OrgBitmap);
TempBitmap=new Graphics::TBitmap();
Width = abs(EndX - StartX);
Height = abs(EndY - StartY);
TempW=max(Width,Height);
if(TempW<=256)
{TempW=256;
Width=256;
Height=256;}
TFX= min(EndX, StartX);
BRX=TFX Width;
TFY= min(EndY, StartY);
BRY=TFY Height;
TempBitmap->Width =Width;
TempBitmap->Height=Height;
TempBitmap->PixelFormat=pf24bit;
TempBitmap->Canvas->CopyMode=cmSrcCopy;
SrcRect=Rect(TFX, TFY, BRX, BRY);
DestRect=Rect(0, 0, Width, Height);
TempBitmap->Canvas->CopyRect(DestRect, TheBitmap->Canvas, SrcRect);
Clipboard()->Assign(TempBitmap);
TheBitmap->Canvas->Pen->Style=psSolid;
TheBitmap->Canvas->Pen->Mode=pmWhite;
TheBitmap->Canvas->Pen->Width=1;
TheBitmap->Canvas->Brush->Style=bsSolid;
TheBitmap->Canvas->Brush->Style=bsClear;//
TheBitmap->Canvas->Rectangle(TFX,TFY,BRX,BRY);
Image2->AutoSize=true;
Image2->Stretch=false;
TheBitmap1=Image2->Picture->Bitmap;
if(TempW<=256)
{
TempW=256;
TheBitmap1->Width=256;
TheBitmap1->Height=256;
}
if (Clipboard()->HasFormat(CF_BITMAP))
{
PasteBitmap->Assign(Clipboard());
TheBitmap1->Canvas->Draw(0,0,PasteBitmap);
TheBitmap1->Canvas->Pen->Width=2;
TheBitmap1->Canvas->Pen->Mode=pmBlack;
TheBitmap1->Canvas->Brush->Style=bsClear;
TheBitmap1->Canvas->Rectangle(0,0,PasteBitmap->Width,PasteBitmap->Height);
}
}// if(Ok)
Image2->Picture->SaveToFile("D:\\333.bmp");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComplementClick(TObject *Sender)
{
Byte *ptr;
Graphics::TBitmap *TheBitmap;
Image2->Picture->LoadFromFile("D:\\333.bmp");
TheBitmap=Image2->Picture->Bitmap;
Image2->Picture->Bitmap->Assign(TheBitmap);
for(int y=0;yHeight;y )
{ ptr=(Byte*)TheBitmap->ScanLine[y];
for(int x=0;xWidth;x )
ptr[x]=(Byte)(255-ptr[x]);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
lbl3->Caption="(X,Y)=(" AnsiString(Y) "," AnsiString(X) ")";
lbl4->Caption="RGB=(" AnsiString(GetRValue(Image1->Canvas->Pixels[X][Y])) ","
AnsiString(GetGValue(Image1->Canvas->Pixels[X][Y])) ","
AnsiString(GetBValue(Image1->Canvas->Pixels[X][Y])) ")";
}
//--------------------------------------------------------------------------- 發表人 - ayuen 於 2003/09/21 16:50:39
|