Board index » delphi » 'Metafile is not valid'

'Metafile is not valid'

Hi,

I have writen the procedure below to shrink an image (jpg) so it will fit
into a list on a report.  However when I run the report I get the following
message on some of the images:-

Project NewArtist.exe raised exception class EInvalidGraphic with message
'Metafile is not valid' Process stopped...

What realy puzzles me is that the image(s) show OK in the main program and a
thumbnail view using similar code.

I am using D5 Pro with service pack added.

Can anyone help/advise.

Regards

Dave
_______________________________________________
procedure TSReport.SizeImages;
const
  maxW: double = 100; maxH: double= 70;
var
  fw,fh,f1,f2,fd : double;
Begin
  s:=D.Q_PI.Fieldbyname('PictureLoc').asString;
  If s='' then s:='y:\Pictures\nopic.jpg';
  S_picture.Picture.LoadFromFile(s);
  fw:= Round(S_picture.Picture.Width);
  fh:= Round(S_picture.Picture.Height);
  f1:= fw/maxW;
  f2:= fh/maxH;
  if f1 > f2 then fd:=f1 else fd:=f2;
  if fd=0 then fd:=10;
  S_picture.Width := Round(fw/fd);
  S_picture.Height:= Round(fh/fd);
  S_picture.Stretch:= true;
end;

procedure TSReport.DetailBand1BeforePrint(Sender: TQRCustomBand;
  var PrintBand: Boolean);
begin
  SizeImages;
end;
_______________________________________________

 

Re:'Metafile is not valid'


"David Gollings" <d...@bullart.com> skrev i en meddelelse
news:V7AL5.162$kn2.291224@newsr1.u-net.net...

Quote
> I have writen the procedure below to shrink an image (jpg) so it will fit
> into a list on a report.  However when I run the report I get the
following
> message on some of the images:-
> Project NewArtist.exe raised exception class EInvalidGraphic with message
> 'Metafile is not valid' Process stopped...

I have seen the same problem on a report with images containing palettes.
When I modified the project so that the images were converted to images
without palette information, the problem went away.

Finn Tolderlund

Re:'Metafile is not valid'


Hi Fin,

Firstly thanks for the reply :)

Please excuse my ignorance but can you explain a bit more, I don't
understand what a "palette" is and do I use code to do remove it?

Kind Regards

Dave

PS The files are in JPG format

"Finn Tolderlund" <XnospamYfinn.tolderlu...@Ymobilixnet.dkXnospamY> wrote in
message news:CZBL5.2380$n3.183944@news0.mobilixnet.dk...

Quote

> "David Gollings" <d...@bullart.com> skrev i en meddelelse
> news:V7AL5.162$kn2.291224@newsr1.u-net.net...
> > I have writen the procedure below to shrink an image (jpg) so it will
fit
> > into a list on a report.  However when I run the report I get the
> following
> > message on some of the images:-
> > Project NewArtist.exe raised exception class EInvalidGraphic with
message
> > 'Metafile is not valid' Process stopped...

> I have seen the same problem on a report with images containing palettes.
> When I modified the project so that the images were converted to images
> without palette information, the problem went away.

> Finn Tolderlund

Re:'Metafile is not valid'


In 2, 16, hi- and true-color images every pixel contains color information.
In 256 color images every pixel contains an index value for the pallette.
The palette contains the definition for the 256 colors.
The problem with images with palettes is that Windows can only display 1
palette at a time, so the new palette has to be 'mapped' on the current
palette.
Try if converting your image to 16 colors or hi-color solves the problem...

"David Gollings" <d...@bullart.com> schreef in bericht
news:VPUL5.195$kn2.348927@newsr1.u-net.net...

Quote
> Hi Fin,

> Firstly thanks for the reply :)

> Please excuse my ignorance but can you explain a bit more, I don't
> understand what a "palette" is and do I use code to do remove it?

> Kind Regards

> Dave

> PS The files are in JPG format

> "Finn Tolderlund" <XnospamYfinn.tolderlu...@Ymobilixnet.dkXnospamY> wrote
in
> message news:CZBL5.2380$n3.183944@news0.mobilixnet.dk...

> > "David Gollings" <d...@bullart.com> skrev i en meddelelse
> > news:V7AL5.162$kn2.291224@newsr1.u-net.net...
> > > I have writen the procedure below to shrink an image (jpg) so it will
> fit
> > > into a list on a report.  However when I run the report I get the
> > following
> > > message on some of the images:-
> > > Project NewArtist.exe raised exception class EInvalidGraphic with
> message
> > > 'Metafile is not valid' Process stopped...

> > I have seen the same problem on a report with images containing
palettes.
> > When I modified the project so that the images were converted to images
> > without palette information, the problem went away.

> > Finn Tolderlund

Re:'Metafile is not valid'


Ok, I think I get it... The only problem is the pictures are of paintings
and to drop the colour resolution down to 16 would show a bad image. :(:(

The other alternative that I have would be to trap the error and bypass the
image but I don't know how to do this.  I have tried a try...except
statement, but to no joy  If I can do this then I can see which images are
causing the problem and then get a better idea of how to deal with it.

A thought... would changing the video drivers colour in the settings down to
a lower colour palette solve the problem, I will try.

Regards

Dave

Quote
"M.H. Avegaart" <avegaartNOS...@mccomm.nl> wrote in message

news:8tpb7u$o7n$1@porthos.nl.uu.net...
Quote
> In 2, 16, hi- and true-color images every pixel contains color
information.
> In 256 color images every pixel contains an index value for the pallette.
> The palette contains the definition for the 256 colors.
> The problem with images with palettes is that Windows can only display 1
> palette at a time, so the new palette has to be 'mapped' on the current
> palette.
> Try if converting your image to 16 colors or hi-color solves the
problem...

> "David Gollings" <d...@bullart.com> schreef in bericht
> news:VPUL5.195$kn2.348927@newsr1.u-net.net...
> > Hi Fin,

> > Firstly thanks for the reply :)

> > Please excuse my ignorance but can you explain a bit more, I don't
> > understand what a "palette" is and do I use code to do remove it?

> > Kind Regards

> > Dave

> > PS The files are in JPG format

> > "Finn Tolderlund" <XnospamYfinn.tolderlu...@Ymobilixnet.dkXnospamY>
wrote
> in
> > message news:CZBL5.2380$n3.183944@news0.mobilixnet.dk...

> > > "David Gollings" <d...@bullart.com> skrev i en meddelelse
> > > news:V7AL5.162$kn2.291224@newsr1.u-net.net...
> > > > I have writen the procedure below to shrink an image (jpg) so it
will
> > fit
> > > > into a list on a report.  However when I run the report I get the
> > > following
> > > > message on some of the images:-
> > > > Project NewArtist.exe raised exception class EInvalidGraphic with
> > message
> > > > 'Metafile is not valid' Process stopped...

> > > I have seen the same problem on a report with images containing
> palettes.
> > > When I modified the project so that the images were converted to
images
> > > without palette information, the problem went away.

> > > Finn Tolderlund

Re:'Metafile is not valid'


"David Gollings" <d...@bullart.com> skrev i en meddelelse
news:_8cM5.232$kn2.395083@newsr1.u-net.net...

Quote
> Ok, I think I get it... The only problem is the pictures are of paintings
> and to drop the colour resolution down to 16 would show a bad image. :(:(

Just convert them in code to 16 or 24 bit images.

Quote
> The other alternative that I have would be to trap the error and bypass
the
> image but I don't know how to do this.  I have tried a try...except
> statement, but to no joy  If I can do this then I can see which images are
> causing the problem and then get a better idea of how to deal with it.

I don't think it is a specific image that causes the error.
It seems that you only get the error if you have a lot of images with
palettes on the report.
So the solution is to only use images with 16 bit colours or more.
That was the solution I came to when I got the problem.

Finn Tolderlund

Re:'Metafile is not valid'


OK, will look into it...

Thanks for the imput.

Regards

Dave

"Finn Tolderlund" <XnospamYfinn.tolderlu...@Ymobilixnet.dkXnospamY> wrote in
message news:WVdM5.2507$n3.197978@news0.mobilixnet.dk...

Quote

> "David Gollings" <d...@bullart.com> skrev i en meddelelse
> news:_8cM5.232$kn2.395083@newsr1.u-net.net...
> > Ok, I think I get it... The only problem is the pictures are of
paintings
> > and to drop the colour resolution down to 16 would show a bad image.
:(:(

> Just convert them in code to 16 or 24 bit images.

> > The other alternative that I have would be to trap the error and bypass
> the
> > image but I don't know how to do this.  I have tried a try...except
> > statement, but to no joy  If I can do this then I can see which images
are
> > causing the problem and then get a better idea of how to deal with it.

> I don't think it is a specific image that causes the error.
> It seems that you only get the error if you have a lot of images with
> palettes on the report.
> So the solution is to only use images with 16 bit colours or more.
> That was the solution I came to when I got the problem.

> Finn Tolderlund

Re:'Metafile is not valid'


In article <_8cM5.232$kn2.395...@newsr1.u-net.net>, "David Gollings"

Quote
<d...@bullart.com> writes:
>Ok, I think I get it... The only problem is the pictures are of paintings
>and to drop the colour resolution down to 16 would show a bad image. :(:(

>The other alternative that I have would be to trap the error and bypass the
>image but I don't know how to do this.  I have tried a try...except
>statement, but to no joy  If I can do this then I can see which images are
>causing the problem and then get a better idea of how to deal with it.

>A thought... would changing the video drivers colour in the settings down to
>a lower colour palette solve the problem, I will try.

>Regards

>Dave

Can I suggest trying something. I have a suspicion that on some machines
selecting a palette into a device that doesn't need one (>256 cols) causes
problems. TMetafile.Draw selects the palette into the dc without any checks to
see if it's appropriate.

I'd try it myself but I don't have access to a machine that raises any errors.

Create your own metafile TMyMetafile = class(TMetafile) and replace the Draw
method with the one below it may well tell you if this is the source of the
problem.

Alternatively, temporarily copy Graphics.Pas to  your compile directory and
'Add' it to your project. Then modify the source as below.

PaletteDevice can also be supplimented with the following as a double check but
I'm not sure it's neccessary.

if GetDeviceCaps(aCanvas.Handle, RASTERCAPS) = RC_PALETTE then
    //it's a palette device

I've not written and checked this in Delphi so there could be syntax errors

procedure TMetafile.Draw(ACanvas: TCanvas; const Rect: TRect);
var
  MetaPal, OldPal: HPALETTE;
  R: TRect;
  PaletteDevice: boolean;
begin
  if FImage = nil then Exit;
//the check for color depth
  PaletteDevice:= (GetDeviceCaps(ACanvas.Handle, PLANES) *
GetDeviceCaps(ACanvas.Handle, BITSPIXEL)) <= 8;
  MetaPal := Palette;
  OldPal := 0;
//apply color depth check
  if (MetaPal <> 0) and PaletteDevice) then
  begin
    OldPal := SelectPalette(ACanvas.Handle, MetaPal, True);
    RealizePalette(ACanvas.Handle);
  end;
  R := Rect;
  Dec(R.Right);  // Metafile rect includes right and bottom coords
  Dec(R.Bottom);
  PlayEnhMetaFile(ACanvas.Handle, FImage.FHandle, R);
//check again
  if (MetaPal <> 0) and PaletteDevice) then
    SelectPalette(ACanvas.Handle, OldPal, True);
end;

Gordon
~~~~~~~~~~~~~~~~~~~~~~~~
Equation Illustrator V Graphics and Equation Editor http://www.mgcsoft.com/
MGCSoft
"Where graphics meets mathematics"
http://members.aol.com/delphistuf/delphstf.htm   (Delphi bits and bobs)

Other Threads