Michał Turecki
Michał Turecki

Reputation: 3177

Storing TBitmap in TMetaFile for smooth scaling in RTF doubles the image size

The RTF image support is very limited (Windows support, not just in Delphi), other formats than bitmaps and metafiles work but are not displayed correctly by RichEdit control.

One thing I noticed is when Microsoft Word image is copied and pasted into RTF it scales smoothly as opposed to pasting image manually (as a bitmap). The reason for that is that Word keeps internally a scaled preview of an image in Metafile format (along with original image) and this scaled version is copied and pasted to RTF and apparently RTF renders MetaFile images smoothly when scaled in RichText editor.

Seems like a good workaround and after implementing embedding BMP in WPF function I noticed one problem I cannot get around: the resulting WMF is double the size of a bitmap. It looks like WMF stores paint buffer or a second copy of the image.

The code:

  procedure DoCopyImage(AGraphic: TGraphic; AWidth, AHeight: Integer);
  var
    mf: TMetafile;
    mfc: TMetafileCanvas;
    r: Cardinal;
  begin
    mf := TMetafile.Create;
    try
      mf.Enhanced := True;
      mf.SetSize(AWidth, AHeight);
      mfc := TMetafileCanvas.Create(mf, 0);
      try
        // set clipping region to a whole image
        r := CreateRectRgn(0, 0, AWidth, AHeight);
        try
          SelectClipRgn(mfc.Handle, r)
        finally
          DeleteObject(r);
        end;

        if (AGraphic.Width = AWidth) and (AGraphic.Height = AHeight) then
          mfc.Draw(0, 0, AGraphic)
        else
          mfc.StretchDraw(Rect(0, 0, AWidth, AHeight), AGraphic);
      finally
        mfc.Free;
      end;
      // Clipboard.Assign(mf);
      mf.SaveToFile('C:\4MB_MetaFile_Why.wmf');
    finally
      mf.Free;
    end;
  end;

I call it using TBitmap as TGraphic:

  pic := TPicture.Create;
  pic.LoadFromFile('C:\2MB_24bpp_Bitmap.bmp');
  bmp := Graphics.TBitmap.Create;
  bmp.Assign(pic.Graphic);
  bmp.Dormant; // experimentation
  bmp.FreeImage; // experimentation
  DoCopyImage(bmp, bmp.Width, bmp.Height);

Can somebody find an explanation for this behaviour? Is WMF storing paint buffer along with bitmap? How to prevent it?

Upvotes: 0

Views: 1902

Answers (2)

Mark Elder
Mark Elder

Reputation: 4127

As to why your sizes may be different - once you Draw to the metafile you are turning over the responsibility to the Metafile for saving out the data. Your bit depth could be different in the output. I don't know for sure but I would also wonder if the StretchDraw call ends up saving the original input or if it saves a bitmap with the new number of pixels. If the image is smaller than the size stretch draw call that could explain the difference. You will also have some overhead in the metafile that will not be in the saved bitmap although that should be minimal.

You may want to look at the MetaFile Explorer. It is a tool that will show you the different draw commands that are embedded in the metafile. I tried your code and looked at the resulting image in MetaFileExplorer. The size of the embedded image looked OK to me. Look at the EMR_STRETCHBLT.cbBitsSrc size. However, I do see the size difference you are reporting so something is taking that space.

You stated:

The reason for that is that Word keeps internally a scaled preview of an image in Metafile format (along with original image) and this scaled version is copied and pasted to RTF and apparently RTF renders MetaFile images smoothly when scaled in RichText editor.

I question this assumption a little bit. A bitmap (any raster image) will show different scaling artifacts depending on how the scaling is performed. The Image Scaling Wikipedia page has some good examples of the differences.

When you write a bitmap into a metafile you are then letting the metafile perform the scaling when the image is drawn to the screen. Different algorithms used by the metafile drawing code vs. the RTF drawing code will cause the output to look different. However there is nothing special about a metafile that can't be done with a normal bitmap. In both cases the original pixels need to be resized/resampled.

Metafiles were really intended for something different than just embedding a single bitmap image. Here is a good Windows Metafile FAQ that explains some of the differences. Basically if you define lines, shapes, and text you can get really smooth scaling because you are describing the drawing, not storing individual pixels.

The key part to getting a good looking scaled image is to pick the right scaling routine. I have used the Graphics32 library for this in my application. The standard StretchDraw routines are designed to be fast - not high quality. When getting ready to draw to the screen I pick a Graphics32 resampler that gives me the results I want. This can change depending on the sizes. For example is your final output larger or smaller than the input image. I resize the image to the final output size and then just Draw instead of StretchDraw.

Upvotes: 2

bummi
bummi

Reputation: 27385

add this code before your mf.SaveToFile('C:\4MB_MetaFile_Why.wmf');

  mf.MMHeight := Round(mf.Height / Screen.PixelsPerInch * 2540);
  mf.MMWidth := Round(mf.Width / Screen.PixelsPerInch * 2540);

Upvotes: 1

Related Questions