Link to home
Start Free TrialLog in
Avatar of Roger Alcindor
Roger Alcindor

asked on

Pixel image reversal problem

I have copied an example of a mirror image generator from Delphi.com with a view to using it in a C++ builder project.

I get the following error in the iteration of the for loop:

First chance exception at $74CBC5AF. Exception class EInvalidOperation with message 'Can only modify an image if it contains a bitmap'. Process BitmapMirror.exe (7408)

The code is as follows:

void __fastcall TForm1::Button1Click(TObject *Sender)
{   // load an image file into Image1
	// reverse the horizontal image (make a mirror image) and display in Image2
	TBitmap *bm;
	int i,j;
	TColor c;

	if(OpenDialog1->Execute())
	{
		Image1->Picture->LoadFromFile(OpenDialog1->FileName);
	}
	bm = new TBitmap();
	bm->Height = Image1->Height;
	bm->Width = Image1->Width;
	for(i=0;i< Image1->Width;i++)
	for(j=0;j<Image1->Height;j++)
		bm->Canvas->Pixels[Image1->Width-i][j] = Image1->Canvas->Pixels[i][j];  // <<<<< This line produces the run-time error
	Image2->Picture->Assign(bm);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger Alcindor
Roger Alcindor

ASKER

Thanks for your help, The problem was due to my loading jpeg images. When I loaded bitmap images then the code worked as expected. the issue was due to my lack of understanding as I haven't had much experience in graphics  programming.