13,825
社区成员
发帖
与我相关
我的任务
分享
void __fastcall TMainForm::CreateMDIChild(String Name)
{
TMDIChild *Child;
TJPEGImage *JPEGImage;
//--- create a new MDI child window ----
Child = new TMDIChild(Application);
Child->Caption = Name;
Child->Image1->AutoSize = true;
if ( FileExists (Name) )
{
if( OpenPictureDialog1->FilterIndex == 1 ) // Open Bitmap image
{
Child->Image1->Picture->LoadFromFile(Name);
Child->ImageType = 1;
}
else // Open JPEG image
{
JPEGImage = new TJPEGImage;
JPEGImage->LoadFromFile( Name );
Child->Image1->Picture->Bitmap->Assign(JPEGImage);
Child->ImageType = 2;
}
Child->ClientHeight = Child->Image1->Picture->Bitmap->Height;
Child->ClientWidth = Child->Image1->Picture->Bitmap->Width;
Child->Image1->Height = Child->Image1->Picture->Bitmap->Height;
Child->Image1->Width = Child->Image1->Picture->Bitmap->Width;
FileSaveItem->Enabled = true;
Child->FileName = Name; // Save the original filename
}
}
void __fastcall TMainForm::FileOpen1Execute(TObject *Sender)
{
if (OpenPictureDialog1->Execute())
CreateMDIChild(OpenPictureDialog1->FileName);
}
void __fastcall TMainForm::GrayClick(TObject *Sender)
{
TMDIChild *Child;
Byte* ptr;
Byte grey;
Child = ((TMDIChild*) ActiveMDIChild);
for(int i=0; i< Child->Image1->Picture->Bitmap->Height; i++)
{
// Get the pointers of Image1
ptr = (Byte*) Child->Image1->Picture->Bitmap->ScanLine[i];
for(int j=0; j<Child->Image1->Picture->Bitmap->Width; j++)
{
// Convert color image to grey tone image using average RGB
grey = (ptr[j*3] + ptr[j*3+1] + ptr[j*3+2])/3;
// Update the pixels in image2 using the pointer of image2
ptr[j*3] = ptr[j*3+1] = ptr[j*3+2] = grey;
}
}
Child->Image1->Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileSaveItemClick(TObject *Sender)
{
TMDIChild *Child;
TJPEGImage *JPEGImage;
Child = ((TMDIChild*) ActiveMDIChild);
if(SavePictureDialog1->Execute())
if( Child->ImageType == 1 )
Child->Image1->Picture->Bitmap->SaveToFile( Child->FileName );
else if( Child->ImageType == 2 )
{
JPEGImage = new TJPEGImage;
JPEGImage->Assign( Child->Image1->Picture->Bitmap );
JPEGImage->SaveToFile( Child->FileName );
}
}
void __fastcall TMDIChild::Image1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
HDC hdc = ::GetDC(Handle);
COLORREF pcolor = ::GetPixel(hdc,X,Y);
unsigned int r = GetRValue(pcolor);
unsigned int g = GetGValue(pcolor);
unsigned int b = GetBValue(pcolor);
MainForm->StatusBar->Panels->Items[0]->Text = = "X = "+AnsiString(X)+" ,Y = "+AnsiString(Y)+" , R: "+AnsiString(r)+" G: "+AnsiString(g)+" B: "+AnsiString(b);
ReleaseDC(0,hdc);
}