Mega Code Archive

 
Categories / Delphi / Graphic
 

Save clipboard as JPG

Title: Save clipboard as JPG Question: I needed a way to copy each graphic in a Word document into a JPG file. Here's a quick and easy solution using a tiny Delphi program. Answer: Word doesn't do this natively (without jumping through some silly hoops) and API's were a lot trickier than I needed. So I wrote a VBA module (not included) that cycles through each graphic in a document. Each graphic gets pasted to the clipboard and then calls the Delphi program that creates a JPG file according to the name that is provided as a parameter. I'm very grateful to anyone that would like to improve what I have done here. The coding was pieced together from books and other websites and seems to work well enough, but I'm certain it could be improved. program CLP2JPG; {$APPTYPE CONSOLE} uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ClipBRD, Jpeg; var myBitmap: TBitmap; myJpegImg: TJpegImage; i: Integer; TargetFile: string; begin myBitmap := TBitmap.Create; myJpegImg := TJpegImage.Create; for i := 0 to ParamCount do Write('Parameter '+IntToStr(i)+' = '+ParamStr(i)+#13#10); if paramstr(1) = '' then begin Write('A file name is required'); Exit; end; try if Clipboard.HasFormat(cf_Bitmap) then begin myBitmap.Assign(clipboard); myJpegImg.Assign(myBitmap); myJpegImg.SaveToFile(ParamStr(1)); end else Write('No graphic on the clipboard'); finally myBitmap.FreeImage; myJpegImg.Free; end; end.