How do I make a bitmap version of a WMF file that is loaded into a TImage.Picture and move that to a TSpeedButton.Glyph

Warren P

For the sake of a minimal complete question, I have a WMF file loaded into a TImage control on a form. This control contains the property Picture, which is a TPicture type. I am trying to "rasterize" the WMF file that I loaded into the TImage, and store that into a TSpeedButton.Glyph.

What is interesting about this process is I am able to use this technique to create a resolution independent custom control (a button in my case) that will redraw its glyph for any resolution you like.

In real world usage, I would not have a TImage or a TSpeedButton, but this question is fundamentally about the process of moving content from TPicture to a TBitmap.

Here is the relevant semi-working code:

procedure CopyBitmap(  Source:TImage;  DestSpeedButton:TSpeedButton );
var
   bmp: TBitmap;
begin
   bmp:=TBitmap.Create;
   try
     // note: with WMF loaded, Source.Picture.Bitmap.Width and Height are 0.
     bmp.Width := Source.Width; // originally I had Source.Picture.Bitmap.Width, which didn't work.
     bmp.Height := Source.Height; //because Source.Picture.Bitmap.Height, doesn't work.
     bmp.Canvas.Draw(0,0, Source.Picture.Graphic );
     DestSpeedButton.Glyph:=bmp;
   finally
     bmp.Free;
   end;
end;

Is this the correct approach? Why does the image invert during copy?

A sample WMF file, the exact file I'm using, is found here.

enter image description here

Warren P

Thanks David, for suggesting that I draw the background. This works.

Note that in production I would change the code below to use Vcl.GraphUtils helper called ScaleImage as the results are much prettier. See the second code sample.

// Quick and Dirty : No sub-pixel anti-aliasing.
// Also does not modifies Source, so set Source's size before you 
// call this. 
procedure CopyBitmap(  Source:TImage;  DestSpeedButton:TSpeedButton );
var
   bmp: TBitmap;
begin
   bmp:=TBitmap.Create;
   try
     bmp.SetSize( Source.Width, Source.Height);
     bmp.Canvas.Pen.Style := psClear;
     bmp.Canvas.Brush.Style := bsSolid;
     bmp.Canvas.Brush.Color := clFuchsia;
     bmp.Canvas.Rectangle(0,0, Source.Width+1,Source.Height+1 );
     bmp.Canvas.Draw(0,0, Source.Picture.Graphic );
     bmp.TransparentColor := clFuchsia;
     DestSpeedButton.Glyph:=bmp;
   finally
     bmp.Free;
   end;
end;

Alternative that uses more memory, and is using the TPicture type instead of TImage because in real use I don't even have a TImage just a TPicture, also this looks nicer. Note that it is written around some custom control of my own design (or yours) that has some property type TBitmap. You have to substitute your own controls, or change TMyControlWithAGlyph to TSpeedButton if that's what you want to do:

// A Bit Better Looking. Uses Vcl.GraphUtils function ScaleImage
procedure CopyBitmap(  Source:TPicture;
                       Dest:TMyControlWithAGlyph;
                       DestType:TCopyDestTypeEnum;
                       AWidth,AHeight:Integer;
                       DoInvert:Boolean;
                       TransparentColor:TColor=clFuchsia );
var
   bmp,bmpFullSize: TBitmap;
   ARect:TRect;
   ScaleAmount:Double;
begin
   if not Assigned(Source) then
      exit;
   if not Assigned(Dest) then
      exit;

   if not Assigned(Source.Graphic) then
      exit;


   bmp:=TBitmap.Create;
   bmpFullSize := TBitmap.Create;
   try
     bmpFullSize.SetSize(  Source.Width, Source.Height );
     bmpFullSize.PixelFormat := pf24bit;
     bmpFullSize.Canvas.Pen.Style := psClear;
     bmpFullSize.Canvas.Brush.Style := bsSolid;
     bmpFullSize.Canvas.Brush.Color := TransparentColor;
     bmpFullSize.Canvas.Rectangle(0,0, Source.Width+1,Source.Height+1 );
     bmpFullSize.Canvas.Draw(0,0, Source.Graphic );


     bmp.SetSize( AWidth, AHeight);
     bmp.PixelFormat := pf24bit;

     // Vcl.GraphiUtil version needs a floating point scale.
     ScaleAmount := AWidth / Source.Width;
     ScaleImage(bmpFullSize,bmp,ScaleAmount );

     // This lets me have a white icon and turn it black if I want to
     // or vice versa
     if DoInvert then
       InvertBitmap(bmp); 

     if DestType=DestLargeGlyph then
     begin
          Dest.LargeGlyph := bmp;
     end
     else
     begin
          Dest.Glyph:=bmp;
     end;
   finally
     bmp.Free;
     bmpFullSize.Free;
   end;
end;

The above code also calls this little helper:

function InvertBitmap(ABitmap: TBitmap): TBitmap;
var
   x, y: Integer;
   ByteArray: PByteArray;
begin
   ABitmap.PixelFormat := pf24Bit;
   for y := 0 to ABitmap.Height - 1 do
   begin
      ByteArray := ABitmap.ScanLine[y];
      for x := 0 to ABitmap.Width * 3 - 1 do
      begin
         ByteArray[x] := 255 - ByteArray[x];
      end;
   end;
   Result := ABitmap;
end;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何制作加载到TImage.Picture中的WMF文件的位图版本并将其移至TSpeedButton.Glyph

来自分类Dev

How to convert a image file loaded in memory to a ID2D1Bitmap in C++

来自分类Dev

How do I efficiently crop an *indexed* Bitmap and get an *indexed* result?

来自分类Dev

How do I make this JS Turbolinks friendly?

来自分类Dev

How do I make an output for grep fail?

来自分类Dev

How can i extract the image out of 'System.Drawing.Bitmap' to file via windbg?

来自分类Dev

How to load a png image into a TImage

来自分类Dev

How do I iterate over a file?

来自分类Dev

Passing File with intent, how do i retrieve it

来自分类Dev

在C#/ WPF / WinForms中将WMF渲染到BitMap时如何启用抗锯齿?

来自分类Dev

Java: How do I make a loop which creates arrays?

来自分类Dev

How do i write the integers that in this file to the mynumbers.txt file?

来自分类Dev

How to make a node move whenever mouse is move over the scene (in JavaFX)?

来自分类Dev

How can I use sed to make thousands of substitutions in a file using a reference file?

来自分类Dev

How can I convert matplotlib path object to bitmap?

来自分类Dev

为什么在调用TImage.Picture.LoadFromFile之后代码仍继续?

来自分类Dev

是否有TSVGImage类通过TMemoryStream将SVG图像加载到TImage.Picture中?

来自分类Dev

How do I create a link to a pdf file with rails?

来自分类Dev

How do I find the path of a data file in Dancer to use in Javascript?

来自分类Dev

How do I get Ruby to search for a pattern on the tail of a local file?

来自分类Dev

How do I open a VBA file in notepad or notepad++

来自分类Dev

How do I make this javascript function wait until I've scrolled x pixels down the page?

来自分类Dev

How do I force Maven to use maven-install-plugin version 2.5?

来自分类Dev

How do I use GitHub to create version 2 of a repository, but keep both versions active?

来自分类Dev

How do I make Auto Hotkey do Alt+F4 when I press right mouse button and scroll wheel?

来自分类Dev

how to get mongodb's loaded configuration file path

来自分类Dev

How to count directories in make file

来自分类Dev

How do I make vim automatically apply c++ syntax highlight on Arduino files (.ino/.pde)?

来自分类Dev

How do we check version of Oracle

Related 相关文章

  1. 1

    如何制作加载到TImage.Picture中的WMF文件的位图版本并将其移至TSpeedButton.Glyph

  2. 2

    How to convert a image file loaded in memory to a ID2D1Bitmap in C++

  3. 3

    How do I efficiently crop an *indexed* Bitmap and get an *indexed* result?

  4. 4

    How do I make this JS Turbolinks friendly?

  5. 5

    How do I make an output for grep fail?

  6. 6

    How can i extract the image out of 'System.Drawing.Bitmap' to file via windbg?

  7. 7

    How to load a png image into a TImage

  8. 8

    How do I iterate over a file?

  9. 9

    Passing File with intent, how do i retrieve it

  10. 10

    在C#/ WPF / WinForms中将WMF渲染到BitMap时如何启用抗锯齿?

  11. 11

    Java: How do I make a loop which creates arrays?

  12. 12

    How do i write the integers that in this file to the mynumbers.txt file?

  13. 13

    How to make a node move whenever mouse is move over the scene (in JavaFX)?

  14. 14

    How can I use sed to make thousands of substitutions in a file using a reference file?

  15. 15

    How can I convert matplotlib path object to bitmap?

  16. 16

    为什么在调用TImage.Picture.LoadFromFile之后代码仍继续?

  17. 17

    是否有TSVGImage类通过TMemoryStream将SVG图像加载到TImage.Picture中?

  18. 18

    How do I create a link to a pdf file with rails?

  19. 19

    How do I find the path of a data file in Dancer to use in Javascript?

  20. 20

    How do I get Ruby to search for a pattern on the tail of a local file?

  21. 21

    How do I open a VBA file in notepad or notepad++

  22. 22

    How do I make this javascript function wait until I've scrolled x pixels down the page?

  23. 23

    How do I force Maven to use maven-install-plugin version 2.5?

  24. 24

    How do I use GitHub to create version 2 of a repository, but keep both versions active?

  25. 25

    How do I make Auto Hotkey do Alt+F4 when I press right mouse button and scroll wheel?

  26. 26

    how to get mongodb's loaded configuration file path

  27. 27

    How to count directories in make file

  28. 28

    How do I make vim automatically apply c++ syntax highlight on Arduino files (.ino/.pde)?

  29. 29

    How do we check version of Oracle

热门标签

归档