Board index » delphi » Metafile to Bitmap conversion quality

Metafile to Bitmap conversion quality

I need to convert a metafile to a bitmap prior to showing the image on
the screen. I can show the metafile directly in a TImage, but for other
reasons I don't want to. The trouble is that by first converting the
image the quality is considerably reduced compared to when I draw the
metafile directly. Somehow rendering directly to the screen produces a
significantly better image than what I'm doing. Does anyone have a
suggestion as to how to retain the quality during the conversion
process.

This is a sample of the code I'm using:

    if (AnsiUpperCase(Ext) = '.WMF') or (AnsiUpperCase(Ext) = '.EMF')
then
    begin
        Meta := nil;
        Bmp  := nil;
        try
            Meta        := TMetaFile.Create;
            Meta.Width  := 1500;
            Meta.Height := 1000;

            try
                MapFile := nil;
                try
                    MapFile := TFileStream.Create(MapPath +
PageQryFileName.AsString, fmOpenRead or fmShareDenyNone);
                    Meta.LoadFromStream(MapFile);
                finally
                    if Assigned(MapFile) then
                        MapFile.Free;
                end;
//...
                try
                    Bmp             := TBitmap.Create;
                    Bmp.Width   := Meta.Width;
                    Bmp.Height  := Meta.Height;

                    Rect.Left   := 0;
                    Rect.Top    := 0;
                    Rect.Right  := Bmp.Width;
                    Rect.Bottom := Bmp.Height;

                    Bmp.Canvas.Brush.Color := clTeal;       // set up
the background colour
                    Bmp.Canvas.FillRect(Rect);

                    Bmp.Canvas.Draw(0,0,Meta);              // put the
metafile onto the bitmap canvas

                    MapImage.Picture.Assign(Bmp);           // finally
assign the bitmap to the MapImage
                except
                    on E: Exception do
                        MessageDlg(pChar('Trouble converting Metafile to
Bitmap : ' + MapPath + PageQryFileName.AsString + Chr(13) + 'Error : ' +
E.Message), mtWarning, [mbOk], 0);
                end;
            except
                on E: Exception do
                    MessageDlg(pChar('Unable to open Map file : ' +
MapPath + PageQryFileName.AsString + Chr(13) + 'Error : ' + E.Message),
mtWarning, [mbOk], 0);
            end;
        finally
            if Assigned(Meta) then
                Meta.Free;

            if Assigned(Bmp) then
                Bmp.Free;
        end;
    end
//...

 

Re:Metafile to Bitmap conversion quality


Quote
Colin Attwell wrote:

> I need to convert a metafile to a bitmap prior to showing the image on
> the screen. I can show the metafile directly in a TImage, but for
> other reasons I don't want to.

Okay guys, found the problem ! actually nothing to do with the
conversion process but what happens after that. The problem was that the
size of the TImage to display the picture was somewhat smaller than the
bitmap used as the target of the conversion, so when assigning the
bitmap to the TImage it was getting scaled to fit, hence the loss of
resolution. Setting the bitmap to the same size os the TImage and using
StretchDraw yielded to correct result - no loss of quality at all.

Other Threads